Domanda

I've the read pytest documentation. Section 7.4.3 gives instructions for registering markers. I have followed the instructions exactly, but it doesn't seem to have worked for me.

I'm using Python 2.7.2 and pytest 2.5.1.

I have a pytest.ini file at the root of my project. Here is the entire contents of that file:

[pytest]
python_files=*.py
python_classes=Check
python_functions=test
rsyncdirs = . logs
rsyncignore = docs archive third_party .git procs
markers =
    mammoth: mark a test as part of the Mammoth regression suite

A little background to give context: The folks that created the automation framework I am working on no longer work for the company. They created a custom plugin that extended the functionality of the default pytest.mark. From what I understand, the only thing the custom plugin does is make it so that I can add marks to a test like this:

@pytest.marks(CompeteMarks.MAMMOTH, CompeteMarks.QUICK_TEST_A, CompeteMarks.PROD_BVT)
def my_test(self):

instead of like this:

@pytest.mark.mammoth
@pytest.mark.quick_test_a
@pytest.mark.prod_bvt
def my_test(self):

The custom plugin code remains present in the code base. I do not know if that has any negative effect on trying to register a mark, but thought it was worth mentioning if someone knows otherwise.

The problem I'm having is when I execute the following command on a command-line, I do NOT see my mammoth mark listed among the other registered marks.

py.test --markers

The output returned after running the above command is this:

@pytest.mark.skipif(condition): skip the given test function if eval(condition) results in a True value.  Evaluation happens within the module global context. Example: skipif('sys.platform == "win32"') skips the test if we are on the win32 platform. see http://pytest.org/latest/skipping.html

@pytest.mark.xfail(condition, reason=None, run=True): mark the the test function as an expected failure if eval(condition) has a True value. Optionally specify a reason for better reporting and run=False if you don't even want to execute the test function. See http://pytest.org/latest/skipping.html

@pytest.mark.parametrize(argnames, argvalues): call a test function multiple times passing in different arguments in turn. argvalues generally needs to be a list of values if argnames specifies only one name or a list of tuples of values if argnames specifies multiple names. Example: @parametrize('arg1', [1,2]) would lead to two calls of the decorated test function, one with arg1=1 and another with arg1=2.see http://pytest.org/latest/parametrize.html for more info and examples.

@pytest.mark.usefixtures(fixturename1, fixturename2, ...): mark tests as needing all of the specified fixtures. see http://pytest.org/latest/fixture.html#usefixtures

@pytest.mark.tryfirst: mark a hook implementation function such that the plugin machinery will try to call it first/as early as possible.

@pytest.mark.trylast: mark a hook implementation function such that the plugin machinery will try to call it last/as late as possible.

What am I doing wrong and how can I get my mark registered?

One more piece of info, I have applied the mammoth mark to a single test (shown below) when I ran the py.test --markers command:

@pytest.mark.mammoth
def my_test(self):
È stato utile?

Soluzione

If I understand your comments correctly the project layout is the following:

~/customersites/
~/customersites/automation/
~/customersites/automation/pytest.ini

Then invoking py.test as follows:

~/customersites$ py.test --markers

will make py.test look for a configuration file in ~/customersites/ and subsequently all the parents: ~/, /home/, /. In this case this will not make it find pytest.ini.

However when you invoke it with one or more arguments, py.test will try to interpret each argument as a file or directory and start looking for a configuration file from that directory and it's parents. It then iterates through all arguments in order until it found the first configuration file.

So with the above directory layout invoking py.test as follows will make it find pytest.ini and show the markers registered in it:

~/customersites$ py.test automation --markers

as now py.test will first look in ~/customersites/automation/ for a configuration file before going up the directory tree and looking in ~/customersites/. But since it finds one in ~/customersites/automation/pytest.ini it stops there and uses that.

Altri suggerimenti

Have you tried here?

From the docs:

API reference for mark related objects

class MarkGenerator[source]

Factory for MarkDecorator objects - exposed as a pytest.mark singleton
instance.

Example:
import py
@pytest.mark.slowtest
def test_function():
    pass

will set a slowtest MarkInfo object on the test_function object.

class MarkDecorator(name, args=None, kwargs=None)[source]

 A decorator for test functions and test classes. When applied it will
 create MarkInfo objects which may be retrieved by hooks as item keywords.
 MarkDecorator instances are often created like this:

 mark1 = pytest.mark.NAME              # simple MarkDecorator
 mark2 = pytest.mark.NAME(name1=value) # parametrized MarkDecorator

and can then be applied as decorators to test functions:

@mark2
def test_function():
    pass
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top