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參數並使用它我們能夠找出測試方法名稱和測試方法路徑。
請幫忙!