[TIP] pytest: markers as functions
holger at merlinux.eu
Sat Jan 26 06:07:41 PST 2013
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
> 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:
> 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
"Mark a function."
assert test_1.mymarker.getarg("arg1") == 42
assert test_2.mymarker.getarg("arg1") == 17
assert test_3.mymarker.getarg("arg1") == 22
reprec = testdir.inline_run()
result = testdir.runpytest("--markers")
*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?
what you r
I in fact have a very similar patch
> testing-in-python mailing list
> testing-in-python at lists.idyll.org
More information about the testing-in-python