我已经阅读了 pytest 文档。第 7.4.3 节给出了注册标记的说明。我已完全按照说明进行操作,但它似乎对我不起作用。
我正在使用 Python 2.7.2 和 pytest 2.5.1。
我的项目根目录下有一个 pytest.ini 文件。以下是该文件的全部内容:
[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
提供一些背景知识:创建我正在处理的自动化框架的人不再为公司工作。他们创建了一个自定义插件,扩展了默认 pytest.mark 的功能。据我了解,自定义插件所做的唯一一件事就是制作它,以便我可以像这样为测试添加标记:
@pytest.marks(CompeteMarks.MAMMOTH, CompeteMarks.QUICK_TEST_A, CompeteMarks.PROD_BVT)
def my_test(self):
而不是像这样:
@pytest.mark.mammoth
@pytest.mark.quick_test_a
@pytest.mark.prod_bvt
def my_test(self):
自定义插件代码保留在代码库中。我不知道这对尝试注册商标是否有任何负面影响,但我认为如果有人知道的话值得一提。
我遇到的问题是,当我在命令行上执行以下命令时,我没有看到我的 mammoth 标记列在其他注册标记中。
py.test --markers
运行上述命令后返回的输出是这样的:
@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.
我哪里做错了,我怎样才能注册我的商标?
还有一条信息,当我运行 py.test --markers 命令时,我已将猛犸象标记应用于单个测试(如下所示):
@pytest.mark.mammoth
def my_test(self):
最佳答案
如果我正确理解您的意见,项目布局如下:
~/customersites/
~/customersites/automation/
~/customersites/automation/pytest.ini
然后调用 py.test 如下:
~/customersites$ py.test --markers
将使 py.test 在 ~/customersites/
中查找配置文件,随后查找所有父级:~/
、/home/
, /
。在这种情况下,这不会让它找到 pytest.ini
。
然而,当您使用一个或多个参数调用它时,py.test 将尝试将每个参数解释为一个文件或目录,并开始从该目录及其父目录中查找配置文件。然后它按顺序遍历所有参数,直到找到第一个配置文件。
因此使用上面的目录布局调用 py.test 如下将使其找到 pytest.ini
并显示其中注册的标记:
~/customersites$ py.test automation --markers
现在 py.test 将首先在 ~/customersites/automation/
中查找配置文件,然后再进入目录树并在 ~/customersites/
中查找。但是因为它在 ~/customersites/automation/pytest.ini
中找到了一个,所以它停在那里并使用它。
关于python - 如何在 pytest 2.5.1 中注册标记?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21557263/