[TIP] pytest: markers as functions

holger krekel holger at merlinux.eu
Sat Jan 26 06:07:41 PST 2013


Dear Alexander,

On Sat, Jan 26, 2013 at 14:08 +0100, Alexander Steinert wrote:
> Dear pytest users and maintainers,
> 
> my ideal custom marker definition/registration would look like this in
> conftest.py:
> 
> @pytest.marker
> def name(request, parg0, ..., kwarg0="default", ...):
>     """marker documentation"""
>     assert ..., "Mark args not used correctly"
> 
> The decorator would be in charge of registration (instead of pytest.ini).
> 
> The function would be called at collection time for each marking.
> 
> The request param could be used for collecting meta data that could be
> retrieved later. Example:
> 
> @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.

In any case, here is a test taken from the unifinished patch which showcases
the intended API. The "testdir" argument is a functional fixture used by
pytest's own test suite to help construct examples and run pytest
against them:

    def test_newmarker(testdir):
        testdir.makeconftest("""
            import pytest
    
            @pytest.markdefinition
            def mymarker(arg1=42):
                "Mark a function."
        """)
        testdir.makepyfile("""
            import pytest
            @pytest.mark.mymarker
            def test_1():
                assert test_1.mymarker.getarg("arg1") == 42

            @pytest.mark.mymarker(arg1=17)
            def test_2():
                assert test_2.mymarker.getarg("arg1") == 17

            @pytest.mark.mymarker(22)
            def test_3():
                assert test_3.mymarker.getarg("arg1") == 22
        """)
        reprec = testdir.inline_run()
        reprec.assertoutcome(passed=3)
        result = testdir.runpytest("--markers")
        result.stdout.fnmatch_lines("""
            *mymarker(arg1=42)*
            *Mark a function.*
        """)

A "markedefinition" function is called for each application
of the marker.  If it doesn't like the parsed in arguments,
it can raise an Exception.  The patch also sponsors a marker.getarg(argname)
function which allows easy access to the parametrization values, independently
if they were specified via keyword arguments or positional.  In the current
situation and API, you have to yourself deal with args/kwargs.

Would this fit your use case?  If not, can you write an example
of what you would need additionally?

best,
holger





what you r
I in fact have a very similar patch
> TIA
> Stony

> _______________________________________________
> 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