[py-dev] Setup and teardown functions

holger krekel holger at merlinux.eu
Tue May 11 15:33:07 CEST 2010


Hi Simon, 

i attach an initial solution as a tar.gz file: 

* tests/conftest.py : the per-project file to put your configuration 
  and test setup machinery in 
* tests/test_module1.py a test module with a test function and a test method
* tests/test_module2.py another test module for demo purposes 

If you untar and run "py.test", a 'setup.log' file gets created
which visualizes setup and teardown invocations:

    global_setup()
      group_setup('tests.test_module1')
        setup()
          executing function 'test_function'
        teardown()
        setup()
          executing method 'test_method'
        teardown()
      group_teardown('tests.test_module1')
      group_setup('tests.test_module2')
        setup()
          executing function 'test_function'
        teardown()
        setup()
          executing method 'test_method'
        teardown()
      group_teardown('tests.test_module2')
    global_teardown()

A few notes which you might find helpful for understanding the solution: 

* each test functions needs to note down the 'mysetup' argument. 
  It gives each test function to global/group/per-test function
  setup state in case you acually do anything in the setup/teardown
  functions other than logging and want the tests to access it :)
  If you don't specify the 'mysetup' argument your tests will still 
  run but not trigger any setup/teardown functions - might be useful
  for pure unit-tests that don't depend on prepared state. 

* funcargs, the request object and the cached_setup() method are documented 
  in http://pytest.org/funcargs.html

* you may choose to optionally call "inlined" setup/teardown methods, i.e. 
  ones that are defined in the test module; this can be done easily 
  because the setup functions in the conftest.py file have a reference  
  to the module object (request.module) or test functions (request.function).

  You may alternatively choose to declare test configuration options in a test
  module or per decorators to test functions and use this to drive the
  setup/teardown machinery, i.e. put into a test module a variable like: 

    group_config = dict(key1=value1, ...)
 
  and interpret it from the hooks in the conftest.py. 

And maybe a final note: the mechanism i noted down requires conftest.py writers
to understand py.test internals but test module writers can remain 
ignorant about it. 

Please let me know if this makes sense to you.  

best,

holger

On Tue, May 11, 2010 at 11:32 +0100, Simon Callan wrote:
> Hi, 
> 
> > i think i can whip up an example.  One question though: Do you already
> have a
> > view on how you want to to group tests in your source code?  In principle
> you
> > can do this by classes/modules or by decorators - or by having custom
> ways. 
> 
> We're still at the early stages of laying out our tests, so we're pretty
> free-form at the moment, however, I think the easiest way to do it would be
> to have all the test functions in a module to be considered part of a group.
> 
> Simon
> 
> Infoshare Ltd
> Millennium House
> 21 Eden Street
> Kingston upon Thames
> Surrey
> KT1 1BL
> United Kingdom
> 
> Phone: 		+ 44 (0) 20 8541 0111
> Support:	+ 44 (0) 20 8481 4760
> Web:		www.infoshare-is.com
> E-mail:		info at infoshare-is.com
> 
> Infoshare Ltd is registered in England and Wales.
> Registered Office as above.
> Registered Number 2877612
> VAT Number GB 678 1443 10
> 
> The content of this e-mail (and any attachment to it) is confidential. 
> Any views or opinions do not represent the views or opinions 
> of Infoshare Ltd.
> If you have received this e-mail in error please notify the sender 
> and delete it. You may not use, copy or disclose the information 
> in any way. 
> 
> Infoshare Ltd monitors incoming and outgoing e-mails.
> 
> Please consider the environment. Do you really need to print 
> this email?
> 
> 
> _______________________________________________
> py-dev mailing list
> py-dev at codespeak.net
> http://codespeak.net/mailman/listinfo/py-dev
> 

-- 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: setup-example.tar.gz
Type: application/octet-stream
Size: 722 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/pytest-dev/attachments/20100511/23eb2a9b/attachment.obj>


More information about the Pytest-dev mailing list