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

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

提交回复
热议问题