Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在pytest 2.5.1中注册标记?_Python_Pytest - Fatal编程技术网

Python 如何在pytest 2.5.1中注册标记?

Python 如何在pytest 2.5.1中注册标记?,python,pytest,Python,Pytest,我已经阅读了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

我已经阅读了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):
而不是像这样:

@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):
自定义插件代码仍然存在于代码库中。我不知道这对注册商标是否有任何负面影响,但我认为如果有人知道其他情况,这是值得一提的

我遇到的问题是,当我在命令行上执行以下命令时,我没有看到我的猛犸象标记列在其他注册标记中

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):
你试过了吗

从文档中:

标记相关对象的API参考

类标记生成器[源]

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

Example:
将在test_函数对象上设置最慢的MarkInfo对象

然后可以作为装饰器应用于测试函数:


如果我正确理解您的意见,项目布局如下:

~/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中找到了一个,所以它停在那里并使用它。

好的,我找到了。我不知道这些东西是如何工作的,但事实就是这样:这是我正在处理的项目的文件结构:
/Users/user/projects/fh/consumersites/
然而,自动化框架就住在这里:
/Users/user/projects/fh/consumersites/automation
,当我运行
py.test--markers
时,我从顶级consumersites目录运行它,并收到我在问题中粘贴的输出。但是,由于某种原因,当我在自动化目录中运行命令时,我看到我的猛犸象标记确实已注册。有人知道为什么我在两个不同的目录中运行命令时会得到两个不同的输出吗?自动化是consumersites的一个子目录,所以我不明白为什么会得到不同的输出。谢谢重要的是,您没有说明pytest.ini所在的位置,如果它位于
consumersites/automation/
子目录中,那么在
consumersites/
中调用py.test而不带任何参数将意味着(我认为)py.test找不到pytest.ini文件。如果我是对的,在
consumersites/
中调用
py.test automation
应该可以工作。@flub,非常感谢!我刚试过你的建议,它确实有效!我的问题非常冗长,因此我可以看出您可能错过了对pytest.ini位置的引用<代码>我的项目**的根目录下有一个pytest.ini文件**。下面是该文件的全部内容:
当我第一次问这个问题时,我没有意识到运行命令的位置是决定我做错了什么的一个因素。再次感谢您的回复!!在这一条被彻底打倒之前,再多说一句。我仍然觉得py.test无法从
consumersites
目录中找到pytest.ini文件并不直观。随着
自动化
成为
消费者网站的子目录
难道它不应该正常工作吗?或者这种设计是故意的,这样人们就可以在不同的地方拥有多个pytest.ini文件?为什么有人会在他们的自动化框架代码库中有超过1个pytest.ini文件?是的!100%正确。我一定是在文档中遗漏了它,但我不知道py.test在查找pytest.ini文件时会查找目录结构。谢谢大家帮我解决了这么一个小而乏味的问题!!像我这样的人真的很感激这种帮助!
~/customersites$ py.test automation --markers