[TIP] (no subject)

holger krekel holger at merlinux.eu
Sun Mar 29 03:22:17 PDT 2015


First of all, if questions become very pytest specific please consider
writing to pytest-dev instead:

    http://mail.python.org/mailman/listinfo/pytest-dev

As to your issue, pytest does not allow a TestClass.__init__ function and will
skip it with a warning.  Rather, the usual way to work with test state
is through fixtures:

    http://pytest.org/latest/fixture.html#fixture

Moreover, i don't see the point of monkeypatching test class instance state.
But that's probably because i don't know what kind of problem you are trying.
to solve.  Or is this just for understanding the mechanics behind py.test's 
test execution?  If so, pytest creates a new TestClass instance for each
test method it executes.  During per-method setup and test methods the
same "self" is used.  Different test methods will see different "self"
instances.  This is similar to how it is handled with unittest and nose.  

cheers,
holger



On Sun, Mar 29, 2015 at 05:14 -0400, dpb dpb wrote:
> 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
> >>
> >
> >

> _______________________________________________
> testing-in-python mailing list
> testing-in-python at lists.idyll.org
> http://lists.idyll.org/listinfo/testing-in-python


-- 
about me:    http://holgerkrekel.net/about-me/
contracting: http://merlinux.eu



More information about the testing-in-python mailing list