[TIP] mark tests

Chris Jerdonek chris.jerdonek at gmail.com
Thu Jul 12 19:29:55 PDT 2012


On Thu, Jul 12, 2012 at 9:08 AM, andrea crotti
<andrea.crotti.0 at gmail.com> wrote:
>
> Now I would like to have a way to mark them, for example I might want to
> run certain things only on Linux, other only on Windows, maybe on Linux
> I want to use the real database, on Windows the fake one..
>
> What might be a good way to achieve this (I use nosetest + mock + coverage)?
>
> Subclassing unittest.TestCase?
> Or maybe using decorators?
> Any other ideas?

I have found the load_tests protocol to be suitable for this purpose
and highly flexible.  The load_tests() function accepts a loader:

load_tests(loader, standard_tests, None)

What I have done is add a "test_config" attribute to the loader before
calling unittest.main() (or whatever variant you might use).  The
test_config attribute carries whatever runtime information you'd like
the tests to depend on.  Then in each module, the load_tests function
can modify the standard tests based on test_config, or even pass
test_config to the test cases themselves.  In the latter case, you can
write your unittest.TestCase test methods to depend on test_config
wherever needed.

--Chris



More information about the testing-in-python mailing list