py.test: how to get the current test's name from the setup method?

前端 未结 7 2085
心在旅途
心在旅途 2020-12-17 07:49

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 e

相关标签:
7条回答
  • 2020-12-17 08:27

    Try type(self).__name__ perhaps?

    0 讨论(0)
  • 2020-12-17 08:30

    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

    0 讨论(0)
  • 2020-12-17 08:35

    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.

    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
    
    0 讨论(0)
  • 2020-12-17 08:38

    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]
    
    0 讨论(0)
  • 2020-12-17 08:40

    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:

    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:

    ============================= 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 ===========================
    
    0 讨论(0)
  • 2020-12-17 08:40

    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.

    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

    0 讨论(0)
提交回复
热议问题