Python py.test:如何从设置方法中获取当前测试的名称?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17726954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
py.test: how to get the current test's name from the setup method?
提问by Dr. Jan-Philip Gehrcke
I am using py.test and wonder if/how it is possible to retrieve the name of the currently executed test within the setup
method that is invoked before running each test. Consider this code:
我正在使用 py.test 并想知道是否/如何setup
在运行每个测试之前调用的方法中检索当前执行的测试的名称。考虑这个代码:
class TestSomething(object):
def setup(self):
test_name = ...
def teardown(self):
pass
def test_the_power(self):
assert "foo" != "bar"
def test_something_else(self):
assert True
Right before TestSomething.test_the_power
becomes executed, I would like to have access to this name in setup
as outlined in the code via test_name = ...
so that test_name
== "TestSomething.test_the_power"
.
就在TestSomething.test_the_power
执行之前,我想setup
通过test_name = ...
使test_name
==访问代码中概述的名称"TestSomething.test_the_power"
。
Actually, in setup
, I allocate some resource for each test. In the end, looking at the resources that have been created by various unit tests, I would like to be able to see which one was created by which test. Best thing would be to just use the test name upon creation of the resource.
实际上,在 中setup
,我为每个测试分配了一些资源。最后,查看各种单元测试创建的资源,我希望能够看到哪个测试创建了哪个。最好的办法是在创建资源时使用测试名称。
采纳答案by danielfrg
You can also do this using the Request Fixturelike this:
您也可以使用Request Fixture执行此操作,如下所示:
def test_name1(request):
testname = request.node.name
assert testname == 'test_name1'
回答by kindall
Try type(self).__name__
perhaps?
试试type(self).__name__
吧?
回答by ejk314
You might have multiple tests, in which case...
您可能有多个测试,在这种情况下...
test_names = [n for n in dir(self) if n.startswith('test_')]
...will give you all the functions and instance variables that begin with "test_" in self
. As long as you don't have any variables named "test_something" this will work.
...会给你所有以“test_”开头的函数和实例变量self
。只要您没有任何名为“test_something”的变量,这将起作用。
You can also define a method setup_method(self, method)
instead of setup(self)
and that will be called before each test method invocation. Using this, you're simply given each method as a parameter. See: http://pytest.org/latest/xunit_setup.html
您还可以定义一个方法,setup_method(self, method)
而不是setup(self)
在每个测试方法调用之前调用该方法。使用它,您只需将每个方法作为参数。请参阅:http: //pytest.org/latest/xunit_setup.html
回答by Robert Caspary
You could give the inspect module are try.
你可以给检查模块试试。
import inspect
def foo():
print "My name is: ", inspect.stack()[0][3]
foo()
Output: My name is: foo
输出: My name is: foo
回答by Dr. Jan-Philip Gehrcke
The setup
and teardown
methods seem to be legacy methods for supporting tests written for other frameworks, e.g. nose. The native pytest
methods are called setup_method
as well as teardown_method
which receive the currently executed test method as an argument. Hence, what I want to achieve, can be written like so:
在setup
与teardown
方法似乎是为支持其他框架笔试,例如鼻子的传统方法。pytest
调用本地方法setup_method
以及teardown_method
接收当前执行的测试方法作为参数。因此,我想要实现的目标可以这样写:
class TestSomething(object):
def setup_method(self, method):
print "\n%s:%s" % (type(self).__name__, method.__name__)
def teardown_method(self, method):
pass
def test_the_power(self):
assert "foo" != "bar"
def test_something_else(self):
assert True
The output of py.test -s
then is:
然后的输出py.test -s
是:
============================= test session starts ==============================
platform linux2 -- Python 2.7.3 -- pytest-2.3.3
plugins: cov
collected 2 items
test_pytest.py
TestSomething:test_the_power
.
TestSomething:test_something_else
.
=========================== 2 passed in 0.03 seconds ===========================
回答by Joao Coelho
You can also use the PYTEST_CURRENT_TEST
environment variable set by pytest for each test case.
您还可PYTEST_CURRENT_TEST
以为每个测试用例使用pytest 设置的环境变量。
PYTEST_CURRENT_TEST environment variable
To get just the test name:
只获取测试名称:
os.environ.get('PYTEST_CURRENT_TEST').split(':')[-1].split(' ')[0]
回答by matt3o
Try my little wrapper function which returns the full name of the test, the file and the test name. You can use whichever you like later. I used it within conftest.py where fixtures do not work as far as I know.
试试我的小包装函数,它返回测试的全名、文件和测试名称。您可以稍后使用任何您喜欢的。据我所知,我在 conftest.py 中使用了它,其中固定装置不起作用。
def get_current_test():
full_name = os.environ.get('PYTEST_CURRENT_TEST').split(' ')[0]
test_file = full_name.split("::")[0].split('/')[-1].split('.py')[0]
test_name = full_name.split("::")[1]
return full_name, test_file, test_name