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

前端 未结 7 2160
心在旅途
心在旅途 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: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

提交回复
热议问题