【发布时间】:2016-12-30 18:12:14
【问题描述】:
我想为每个测试方法创建一个单独的日志文件。我想在 conftest.py 文件中执行此操作并将日志文件实例传递给测试方法。这样,每当我在测试方法中记录某些内容时,它都会记录到单独的日志文件中,并且非常容易分析。
我尝试了以下方法。 在 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 没有能力返回任何东西给测试方法。至少,我不知道。
所以,我想在 conftest.py 文件中创建一个带有 scope="function" 的夹具方法,并从测试方法中调用这个夹具。但是,fixture 方法不知道 Pytest.Item 对象。对于 pytest_runtest_setup 方法,它接收 item 参数并使用它可以找出测试方法名称和测试方法路径。
请帮忙!
【问题讨论】:
标签: pytest