[TIP] decorators in testing

Michael Foord fuzzyman at voidspace.org.uk
Sat Apr 4 05:48:19 PDT 2009


Noah Gift wrote:
>
>
>     […]
>
>     > Does anyone have an implementation of this for Python?
>
>     ‘nose’ allows setup and teardown functions to be written, then used to
>     decorate test functions:
>
>     >From nose import with_setup
>
>     def blivet_setup():
>        # ...
>
>     def blivet_teardown():
>        # ...
>
>     @with_setup(blivet_setup, blivet_teardown)
>     def test_frobnicates_the_blivet():
>        # ...
>
>
> But this only works with tests that are functions correct?

And also requires each test to be separately decorated. Grouping tests 
with the same setUp and tearDown into common classes with unittest is 
useful.

I'm intrigued about using decorators to mark tests and the setUp and 
tearDown - of course this is configuration over convention and actually 
marginally more work. I would still want test classes to inherit from 
TestCase to inherit the functionality.

It is possible that the xUnit frameworks for statically typed languages 
use decorators for tests rather than naming conventions because 
introspection is so much harder in these languages. Pure speculation on 
my part of course.

Any other useful decorators provided by the xUnit frameworks?

Michael


>
>
>     --
>      \             “I put contact lenses in my dog's eyes. They had
>     little |
>      `\   pictures of cats on them. Then I took one out and he ran
>     around |
>     _o__)                                      in circles.” —Steven
>     Wright |
>     Ben Finney
>
>     _______________________________________________
>     testing-in-python mailing list
>     testing-in-python at lists.idyll.org
>     <mailto:testing-in-python at lists.idyll.org>
>     http://lists.idyll.org/listinfo/testing-in-python
>
>
>
>
> -- 
> Cheers,
>
> Noah
> ------------------------------------------------------------------------
>
> _______________________________________________
> testing-in-python mailing list
> testing-in-python at lists.idyll.org
> http://lists.idyll.org/listinfo/testing-in-python
>   


-- 
http://www.ironpythoninaction.com/
http://www.voidspace.org.uk/blog





More information about the testing-in-python mailing list