<div dir="ltr">Thanks to both of you. <div><br></div><div>I understand that it's normally the case to want complete isolation between tests.</div><div><br></div><div>But on occasion it's useful to run tests on a series of cumulative changes to state. That means forfeiting test independence, or anyway building it into the sequence in which tests are run. The alternatives seem to be placing many asserts into each test function, or running tests based on simulated data. </div><div><br></div><div>I see that I can keep track cumulative changes in state pretty simply, using a non-test class (rather than the test class) to define the state-bearing object.</div><div><br></div><div>- dpb</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Mar 29, 2015 at 12:01 PM, Ned Batchelder <span dir="ltr"><<a href="mailto:ned@nedbatchelder.com" target="_blank">ned@nedbatchelder.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div bgcolor="#FFFFFF" text="#000000"><span class="">
On 3/29/15 4:57 AM, dpb dpb wrote:<br>
<blockquote type="cite">
<div dir="ltr">
<div><font face="arial, helvetica, sans-serif">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: </font></div>
<div><font face="monospace, monospace"><br>
</font></div>
</div>
</blockquote></span>
As Holger mentioned, a new TestExample instance is created for each
test. This is how all the test runners work, because you want
isolation between your tests. One of the principles of the xUnit
style of testing is that each test is independent of all other
tests. You want to be able to run a single test and not have it
depend on the results of previous tests.<span class="HOEnZb"><font color="#888888"><br>
<br>
--Ned.<br>
<br>
</font></span><blockquote type="cite"><div><div class="h5">
<div dir="ltr">
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><font face="monospace, monospace">#
test_pytest_class_attributes.py</font><font face="monospace,
monospace"><br>
</font><font face="monospace, monospace">"""Test the setting
of class attributes."""</font><font face="monospace,
monospace"><br>
</font><font face="monospace, monospace">class TestExample():<br>
</font><font face="monospace, monospace"> def setup(self):<br>
</font><font face="monospace, monospace">
self.attribute = 1</font> </blockquote>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><font face="monospace, monospace"><br>
</font><font face="monospace, monospace"> def
test_changing_attr(self):<br>
</font><font face="monospace, monospace"> """Change
attribute on object."""<br>
</font><font face="monospace, monospace">
self.attribute = 2<br>
</font><font face="monospace, monospace"> assert
self.attribute == 2</font> </blockquote>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><font face="monospace, monospace"><br>
</font><font face="monospace, monospace"> def
test_attr_is_changed(self):<br>
</font><font face="monospace, monospace"> """Assume
attribute is changed."""<br>
</font><font face="monospace, monospace"> assert
self.attribute == 2</font> </blockquote>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><font face="monospace, monospace"><br>
</font><font face="monospace, monospace"> def
test_attr_is_unchanged(self):<br>
</font><font face="monospace, monospace"> """</font><span style="font-family:monospace,monospace">Assume</span><font face="monospace, monospace"> attribute is unchanged."""<br>
</font><font face="monospace, monospace"> assert
self.attribute == 1</font></blockquote>
<div><font face="monospace, monospace"><br>
</font></div>
<div><font face="arial, helvetica, sans-serif">Output:</font></div>
<div><font face="monospace, monospace"><br>
</font></div>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><font face="monospace, monospace">$ py.test
test_pytest_class_attributes.py -v<br>
</font><font face="monospace, monospace">=============================
test session starts ==============================<br>
</font><font face="monospace, monospace">platform darwin --
Python 3.4.1 -- py-1.4.25 -- pytest-2.6.3 --
/Users/dpb/py34/bin/python3.4<br>
</font><font face="monospace, monospace">collected 3 items </font><font face="monospace, monospace"><br>
</font><font face="monospace, monospace">test_pytest_class_attributes.py::TestExample::test_changing_attr
PASSED<br>
</font><font face="monospace, monospace">test_pytest_class_attributes.py::TestExample::test_attr_is_changed
<font color="#ff0000">FAILED</font><br>
</font><font face="monospace, monospace">test_pytest_class_attributes.py::TestExample::test_attr_is_unchanged
PASSED</font><font face="monospace, monospace"><br>
</font><font face="monospace, monospace">===================================
FAILURES ===================================<br>
</font><font face="monospace, monospace">_______________________
TestExample.test_attr_is_changed _______________________</font><font face="monospace, monospace"><br>
</font><font face="monospace, monospace">self =
<test_pytest_class_attributes.TestExample object at
0x10dd98ef0></font><font face="monospace, monospace"><br>
</font><font face="monospace, monospace"> def
test_attr_is_changed(self):<br>
</font><font face="monospace, monospace"> """Test
whether attribute is changed."""<br>
</font><font face="monospace, monospace">> assert
self.attribute == 2<br>
</font><font color="#ff0000"><font face="monospace, monospace">E
assert 1 == 2<br>
</font><font face="monospace, monospace">E + where 1
= <test_pytest_class_attributes.TestExample object at
0x10dd98ef0>.attribute</font></font> </blockquote>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"> </blockquote>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><font color="#000000" face="monospace, monospace">test_pytest_class_attributes.py:14:
AssertionError<br>
</font><font color="#ff0000" face="monospace, monospace">======================
1 failed, 2 passed in 0.02 seconds ======================<br>
</font><font face="monospace, monospace">$</font></blockquote>
<div><font face="monospace, monospace"><br>
</font></div>
<div><font face="arial, helvetica, sans-serif">Is there special
syntax or some special structure to make class attributes
behave In Pytest as they do in an ordinary Python class?
Example:</font></div>
<div><font face="monospace, monospace"><br>
</font></div>
<div>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><font face="monospace, monospace"># display_class_attributes.py</font><font face="monospace, monospace"><br>
</font><font face="monospace, monospace">"""Test the setting
of class attributes."""</font> </blockquote>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><font face="monospace, monospace"><br>
</font><font face="monospace, monospace">class
TestExample():<br>
</font><font face="monospace, monospace"> def
__init__(self):<br>
</font><font face="monospace, monospace">
self.attribute = 1</font> </blockquote>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><font face="monospace, monospace"><br>
</font><font face="monospace, monospace"> def
test_changing_attr(self):<br>
</font><font face="monospace, monospace"> """Change
attribute on object."""<br>
</font><font face="monospace, monospace">
self.attribute = 2<br>
</font><font face="monospace, monospace"> assert
self.attribute == 2<br>
</font><font face="monospace, monospace">
print('Finished test_changing_attr.\n')</font> </blockquote>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><font face="monospace, monospace"><br>
</font><font face="monospace, monospace"> def
test_attr_is_changed(self):<br>
</font><font face="monospace, monospace"> """Test
whether attribute is changed."""<br>
</font><font face="monospace, monospace"> assert
self.attribute == 2<br>
</font><font face="monospace, monospace">
print('Finished test_attr_is_changed.\n')</font> </blockquote>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><font face="monospace, monospace"><br>
</font><font face="monospace, monospace"> def
test_attr_is_unchanged(self):<br>
</font><font face="monospace, monospace"> """Test
whether attribute is unchanged."""<br>
</font><font face="monospace, monospace"> assert
self.attribute == 1<br>
</font><font face="monospace, monospace">
print('Finiahed test_attr_is_unchanged.\n')</font> </blockquote>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><font face="monospace, monospace"><br>
</font><font face="monospace, monospace">t = TestExample()<br>
</font><font face="monospace, monospace">t.test_changing_attr()<br>
</font><font face="monospace, monospace">t.test_attr_is_changed()<br>
</font><font face="monospace, monospace">t.test_attr_is_unchanged()</font></blockquote>
<div><font face="monospace, monospace"><br>
</font></div>
<div><font face="arial, helvetica, sans-serif">Output:</font></div>
<div><font face="monospace, monospace"><br>
</font></div>
> $ python display_class_attributes.py<br>
><br>
> self.attribute == 2: True <br>
><br>
> self.attribute == 2: True<br>
><br>
> self.attribute == 1: False<br>
><br>
> $
<div><br>
</div>
<div>Thanks. </div>
</div>
<div><font face="monospace, monospace"><br>
</font></div>
<div><font face="monospace, monospace">- dpb</font></div>
</div>
<br>
<fieldset></fieldset>
<br>
</div></div><span class=""><pre>_______________________________________________
testing-in-python mailing list
<a href="mailto:testing-in-python@lists.idyll.org" target="_blank">testing-in-python@lists.idyll.org</a>
<a href="http://lists.idyll.org/listinfo/testing-in-python" target="_blank">http://lists.idyll.org/listinfo/testing-in-python</a>
</pre>
</span></blockquote>
<br>
</div>
<br>_______________________________________________<br>
testing-in-python mailing list<br>
<a href="mailto:testing-in-python@lists.idyll.org">testing-in-python@lists.idyll.org</a><br>
<a href="http://lists.idyll.org/listinfo/testing-in-python" target="_blank">http://lists.idyll.org/listinfo/testing-in-python</a><br>
<br></blockquote></div><br></div>