[py-dev] utest docs; questions

Ian Bicking ianb at colorstudy.com
Thu Oct 14 19:57:25 CEST 2004


Reading through the utest docs, I have a few questions:


"""
Managing test state across test modules, classes and methods
------------------------------------------------------------

Often you want to create some test files, db-connections or
other state in order to run tests in a certain environment.
With ``py.test`` there are three scopes for which you can
provide hooks to manage such state.  These hooks will get
called by the driver::
"""

Where are these functions put?  At the module level?  If so, why
do they receive a module argument?  I guess I can imagine::

   from common import setup_module

So that the setup_module function can work on multiple modules.  Is this
the intention?  Maybe the example below (corrected if necessary) would 
make this all more clear.

"""
Another ``PyCollector`` then recurses into the test files and
collects functions and methods that have a leading ``test_``.
By default, methods are only collected if their class starts
with ``Test``.
"""

It's not clear here how classes and  methods work.  Is the class
instantiated with no arguments, then all the test_* methods run?  E.g.:

# in test_math.py

class TestMath:

     def test_plus(self):
         n = self.num
         for i in range(self.count-1):
             n = self.num + n
         assert n == self.num * self.count

     def setup_class(cls):
         self.num = 5
         self.count = 3
     setup_class = classmethod(setup_class)

     def setup_method(self, method):
         # Sigh, I can't think of a good example...
         pass

What happens here?  E.g.:

import test_math
# if setup_module existed, call test_math.setup_module(test_math)
TestMatch.setup_class()
instance = TestMath()
instance.setup_method(instance.test_plus)
instance.test_plus()
# instance.teardown_method(instance.test_plus)
# TestMatch.teardown_class()
# teardown_module(test_math)


Obviously this can be customized by using different Items, but I'm
thinking of the default action.


-- 
Ian Bicking  /  ianb at colorstudy.com  /  http://blog.ianbicking.org



More information about the Pytest-dev mailing list