[TIP] pytest test collection problem with markers and test parametrization?

holger krekel holger at merlinux.eu
Tue Apr 9 02:54:18 PDT 2013


On Mon, Apr 08, 2013 at 14:38 -0400, Pella,Chris wrote:
> I asked this question in stackoverflow but didn't get any answers, so I'll narrowcast it here.
> We have a marked test that we expect to be not executed because py.test was invoked with another marker, yet the test is getting executed.
> e.g.
> @pytest.mark.stress
> def test_one(some_fixture):
>      pass
> 
> @pytest.mark.sa
> def test_two(some_fixture):
>      pass


I put this into a test file and "-m sa" or "-m stress" selects/deselects
as expected.  There must be something more going on than just this example.
Could you try to get a better reproducable file/session?

best,
holger

> If we run with pytest -collectonly -m "sa" it will show that both tests are collected, but we expected test_one to not be collected for execution. The only way to avoid the stress test is by invoking pytest --collectonly -m "sa and (not stress)".
> We inspect the original argument list in the pytest_generate_tests hook to grab the markers so we can use the platform (e.g. sa in this case) marker in the fixture to set up the test resources, but otherwise don't manipulate the arguments.  Is the test parametrization hook below causing pytest to think test_one is marked with the 'sa' marker?
> 
> def pytest_generate_tests(metafunc):
>     """
>     Generate the parameterization list for the source device
>     based on the markers set in the command line.
>     @param metafunc - meta object for a specific function.
>     """
>     source    = [ 'unsupported' ]
> 
>     if '-m' in metafunc.config._origargs:
>         index = metafunc.config._origargs.index('-m')
>         markers = metafunc.config._origargs[index + 1]
>         markers = markers.split(',')
> 
>         source = []
>         if 'sa' in markers:
>             source.append( 'sa' )
>         if 'g5' in markers:
>             source.append( 'g5' )
>         if 'pci' in markers:
>             source.append( 'pci' )
>         if len(source) == 0:
>             source = [ 'unsupported' ]
> 
>     if 'source' in metafunc.funcargnames:
>         metafunc.parametrize("source", source, indirect=True)
>     if 'rped_source_only' in metafunc.funcargnames:
>         metafunc.parametrize("rped_source_only", source, indirect=True)
> 
> 
> 
> 
> Chris
> 
> 
> The information contained in this electronic mail transmission 
> may be privileged and confidential, and therefore, protected 
> from disclosure. If you have received this communication in 
> error, please notify us immediately by replying to this 
> message and deleting it from your computer without copying 
> or disclosing it.
> 
> 


> _______________________________________________
> testing-in-python mailing list
> testing-in-python at lists.idyll.org
> http://lists.idyll.org/listinfo/testing-in-python




More information about the testing-in-python mailing list