<html>
  <head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    If you have a long series of operations you want to test, I'm not
    sure how you can do better than simply making one long test.  Use
    many assertions in the test.  If one fails, the test will stop.  If
    you were to make a cascade of tests as you describe, the later tests
    couldn't run if an earlier one failed anyway, and the test runner
    wouldn't know to skip the remaining tests.<br>
    <br>
    Another option is to put the state-building code in helper functions
    that you can call from your tests.<br>
    <br>
    --Ned.<br>
    <br>
    <div class="moz-cite-prefix">On 3/29/15 12:38 PM, dpb dpb wrote:<br>
    </div>
    <blockquote
cite="mid:CAJukCzdsL+c0RcQbh8jnMfnS3c3VHuy8PykXr4yYrumKNZuHsA@mail.gmail.com"
      type="cite">
      <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">&lt;<a moz-do-not-send="true"
              href="mailto:ned@nedbatchelder.com" target="_blank">ned@nedbatchelder.com</a>&gt;</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 =
                          &lt;test_pytest_class_attributes.TestExample
                          object at 0x10dd98ef0&gt;</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">&gt;  
                              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 =
                            &lt;test_pytest_class_attributes.TestExample
                            object at 0x10dd98ef0&gt;.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>
                        &gt; $ python display_class_attributes.py<br>
                        &gt;<br>
                        &gt; self.attribute == 2: True <br>
                        &gt;<br>
                        &gt; self.attribute == 2: True<br>
                        &gt;<br>
                        &gt; self.attribute == 1: False<br>
                        &gt;<br>
                        &gt; $
                        <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 moz-do-not-send="true" href="mailto:testing-in-python@lists.idyll.org" target="_blank">testing-in-python@lists.idyll.org</a>
<a moz-do-not-send="true" 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 moz-do-not-send="true"
              href="mailto:testing-in-python@lists.idyll.org">testing-in-python@lists.idyll.org</a><br>
            <a moz-do-not-send="true"
              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>
    </blockquote>
    <br>
  </body>
</html>