Source From Here
Question
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:
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".
HowTo
You can also use the PYTEST_CURRENT_TEST environment variable set by pytest for each test case. (PYTEST_CURRENT_TEST environment variable) To get just the test name:
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:
- 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
HowTo
You can also use the PYTEST_CURRENT_TEST environment variable set by pytest for each test case. (PYTEST_CURRENT_TEST environment variable) To get just the test name:
- os.environ.get('PYTEST_CURRENT_TEST').split(':')[-1].split(' ')[0]
沒有留言:
張貼留言