[TIP] (no subject)

dpb dpb dpb.mediamath at gmail.com
Sun Mar 29 01:57:03 PDT 2015


Something I do not see addressed explicitly in the Pytest docs is this
behavior: Unlike normal class writing in Python 3, changing the value of a
class attribute in one function does not leave it changed in test functions
that are called subsequently:

# test_pytest_class_attributes.py
> """Test the setting of class attributes."""
> class TestExample():
>     def setup(self):
>         self.attribute = 1


>     def test_changing_attr(self):
>         """Change attribute on object."""
>         self.attribute = 2
>         assert self.attribute == 2


>     def test_attr_is_changed(self):
>         """Assume attribute is changed."""
>         assert self.attribute == 2


>     def test_attr_is_unchanged(self):
>         """Assume attribute is unchanged."""
>         assert self.attribute == 1


Output:

$ py.test test_pytest_class_attributes.py -v
> ============================= test session starts
> ==============================
> platform darwin -- Python 3.4.1 -- py-1.4.25 -- pytest-2.6.3 --
> /Users/dpb/py34/bin/python3.4
> collected 3 items
> test_pytest_class_attributes.py::TestExample::test_changing_attr PASSED
> test_pytest_class_attributes.py::TestExample::test_attr_is_changed FAILED
> test_pytest_class_attributes.py::TestExample::test_attr_is_unchanged PASSED
> =================================== FAILURES
> ===================================
> _______________________ TestExample.test_attr_is_changed
> _______________________
> self = <test_pytest_class_attributes.TestExample object at 0x10dd98ef0>
>     def test_attr_is_changed(self):
>         """Test whether attribute is changed."""
> >       assert self.attribute == 2
> E       assert 1 == 2
> E        +  where 1 = <test_pytest_class_attributes.TestExample object at
> 0x10dd98ef0>.attribute



test_pytest_class_attributes.py:14: AssertionError
> ====================== 1 failed, 2 passed in 0.02 seconds
> ======================
> $


Is there special syntax or some special structure to make class attributes
behave In Pytest as they do in an ordinary Python class? Example:

# display_class_attributes.py
> """Test the setting of class attributes."""


> class TestExample():
>     def __init__(self):
>         self.attribute = 1


>     def test_changing_attr(self):
>         """Change attribute on object."""
>         self.attribute = 2
>         assert self.attribute == 2
>         print('Finished test_changing_attr.\n')


>     def test_attr_is_changed(self):
>         """Test whether attribute is changed."""
>         assert self.attribute == 2
>         print('Finished test_attr_is_changed.\n')


>     def test_attr_is_unchanged(self):
>         """Test whether attribute is unchanged."""
>         assert self.attribute == 1
>         print('Finiahed test_attr_is_unchanged.\n')


> t = TestExample()
> t.test_changing_attr()
> t.test_attr_is_changed()
> t.test_attr_is_unchanged()


Output:

> $ python display_class_attributes.py
>
> self.attribute == 2: True
>
> self.attribute == 2: True
>
> self.attribute == 1: False
>
> $

Thanks.

- dpb
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20150329/d8754da2/attachment.html>


More information about the testing-in-python mailing list