[TIP] pytest: markers as functions

Alexander Steinert stony8de at gmail.com
Sat Mar 16 17:56:50 PDT 2013


Sorry for the late feedback. It took me some weeks to get back to this
subject.

holger krekel, Mon 2013-01-28 10:21 CET:
> On Mon, Jan 28, 2013 at 06:48 +0100, Alexander Steinert wrote:
> > On Jan 26, 2013 3:07 PM, "holger krekel" <holger at merlinux.eu> wrote:
> > > On Sat, Jan 26, 2013 at 14:08 +0100, Alexander Steinert wrote:
> > > > @pytest.marker
> > > > def verifies(request, requirement):
> > > >     ....
> > > >
> > > > ....
> > > >
> > > > @verifies("Requirement 42")
> > > > def test_42():
> > > >     assert False
> > > >
> > > > ....
> > > >
> > > > $ py.test . --show-verifications
> > > > test_42 vierifies Requirement 42
> > > >
> > > > By reading the fine manual I assume that this would be a feature
> > request.
> > > > What would come closest to it using the current pytest?
> > >
> > > I have an unfinished patch which allows roughly the above.  However, it's
> > > not providing "request" and i am not sure how you would use it above.
> > > Certainly at collection time it cannot be the "request" object that's used
> > > during fixture setup.
> > 
> > I see. I need some object to collect which test verifies which
> > requirement. That could be done without pytest help using a module level
> > dict in my conftest.py. But I think that the marker decorated by
> > pytest.marker (or pytest.markdefinition) would benefit from getting the
> > marked test function (or something else) as its first argument so that
> > it can get/derive the test name unique to the test session.
> 
> I see.  We could think about passing in "metafunc" which is also passed
> to the pytest_generate_tests hook (called at collection time to implement
> parametrization, also responsible for acting on the @pytest.mark.parametrize 
> decorator).  It contains context (class/module/test function/...) information.
> I am not sure if that's a good idea, though.  Let's see to play with it.
> Can you create an enhancement issue so we can interact on this further?
> Any help much appreciated, of course.

With your hints I was able to find out that I do not need access to the
context at mark time. All I need can be done in pytest_collection_finish
and accessed via session.items. Thank you very much!

For my original request, I created
<https://bitbucket.org/hpk42/pytest/issue/277/markers-as-functions>

/Stony




More information about the testing-in-python mailing list