0
我想为每个测试方法创建一个单独的日志文件。我想在conftest.py文件中执行此操作,并将日志文件实例传递给测试方法。这样,每当我在测试方法中记录某些东西时,它就会记录到一个单独的日志文件中,并且将很容易分析。pytest按照测试方法实现日志文件
我尝试了以下。 里面conftest.py文件添加此:
logs_dir = pkg_resources.resource_filename("test_results", "logs")
def pytest_runtest_setup(item):
test_method_name = item.name
testpath = item.parent.name.strip('.py')
path = '%s/%s' % (logs_dir, testpath)
if not os.path.exists(path):
os.makedirs(path)
log = logger.make_logger(test_method_name, path) # Make logger takes care of creating the logfile and returns the python logging object.
这里的问题是,pytest_runtest_setup没有任何东西返回测试方法的能力。至少,我不知道它。
所以,我想在scope =“function”的conftest.py文件中创建一个fixture方法,并从测试方法中调用这个fixture。但是,fixture方法并不知道Pytest.Item对象。在pytest_runtest_setup方法的情况下,它接收item参数并使用它我们能够找出测试方法名称和测试方法路径。
请帮忙!