[TIP] py.test, flagging remaining tests in a class/file as skip depending on preconditions

holger krekel holger at merlinux.eu
Wed Feb 1 09:07:10 PST 2012

Hi James, Ronny,

On Tue, Jan 31, 2012 at 00:03 +0100, Ronny Pfannschmidt wrote:
> On 01/30/2012 11:42 PM, James Bohnert wrote:
> > Working on a proof of concept for a testing structure with py.test
> > 2.2.1 - I want to split up tests into different files covering
> > different areas of a product, and probably use test classes to further
> > organize within the files.
> > 
> > I would like to use some convention to establish a 'prerequisite' test
> > case in each file or class, upon failure of which the rest of the
> > tests in that class/file would be Skipped, but otherwise test
> > collection and running continues as normal.  I really dont want to
> > have to put conditionals with skips in every test case or other
> > methods that would require boilerplate throughout the test cases.
> > 
> > It looks like the solution involves setting up the
> > pytest_runtest_setup hook, and skip() on the testing of some value
> > that would be toggled in the first test of the file/class (the
> > 'prereq' test), but Im not sure if there is a nice way to flag the
> > condition from inside one test, have it affect the remainder of a
> > group of tests, but then cleanly reset on the next collection/class of
> > tests.
> > 
> > Any pointers?
> > 
> you could use marks to mark prerequisite tests at the wanted scope
> (class/module)
> and always run those, while running normal tests only if all
> prerequisites for the scope have executed first and passed

There might be a simple solution involving pytest_runtest_setup, indeed.
I don't think you neccessarily need to use any marking or extra 
information on the tests.    

More information about the testing-in-python mailing list