[TIP] (no subject)
dpb dpb
dpb.mediamath at gmail.com
Sun Mar 29 02:14:41 PDT 2015
I thought for a while that this was what monkeypatch.setattr was intended
for, but its effects to not seem to persist between test-functions:
import pytest
> # test_pytest_class_attributes_monkeypatch.py
> """Test the setting of class attributes with monkeypatch."""
> class TestExample():
> def setup(self):
> self.attribute = 1
> def test_changing_attr(self, monkeypatch):
> """Change attribute on object."""
> monkeypatch.setattr(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_monkeypatch.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_monkeypatch.py::TestExample::test_changing_attr
> PASSED
> test_pytest_class_attributes_monkeypatch.py::TestExample::test_attr_is_changed
> FAILED
> test_pytest_class_attributes_monkeypatch.py::TestExample::test_attr_is_unchanged
> PASSED
> =================================== FAILURES
> ===================================
> _______________________ TestExample.test_attr_is_changed
> _______________________
> self = <test_pytest_class_attributes_monkeypatch.TestExample object at
> 0x10846acc0>
> def test_attr_is_changed(self):
> """Assume attribute is changed."""
> > assert self.attribute == 2
> E assert 1 == 2
> E + where 1 =
> <test_pytest_class_attributes_monkeypatch.TestExample object at
> 0x10846acc0>.attribute
> test_pytest_class_attributes_monkeypatch.py:18: AssertionError
> ====================== 1 failed, 2 passed in 0.02 seconds
> ======================
> $
- dpb
On Sun, Mar 29, 2015 at 4:59 AM, dpb dpb <dpb.mediamath at gmail.com> wrote:
> Sorry, I hit send too soon. Though it's not essential to the question, the
> second program should read:
>
> # 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
>> print('self.attribute == 2:', self.attribute == 2)
>
>
>> def test_attr_is_changed(self):
>> """Assume attribute is changed."""
>> print('self.attribute == 2:', self.attribute == 2)
>
>
>> def test_attr_is_unchanged(self):
>> """Assume attribute is unchanged."""
>> print('self.attribute == 1:', self.attribute == 1)
>
>
>> 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
>> $
>
>
> - dpb
>
> On Sun, Mar 29, 2015 at 4:57 AM, dpb dpb <dpb.mediamath at gmail.com> wrote:
>
>> 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/a8b039e2/attachment-0001.htm>
More information about the testing-in-python
mailing list