南京网站制作网页,河北省最新消息,石家庄网络关键词排名,网络电话免费版场景
执行自动化用例时#xff0c;希望日志按用例生成一个文件#xff0c;并且按用例所在文件生成目录#xff0c;用例失败时便于查看日志记录
实现方式
pytest.ini文件 在pytest.ini配置文件中设置配置项#xff08;定义日志输出级别和格式#xff09;
log_clitrue
l…场景
执行自动化用例时希望日志按用例生成一个文件并且按用例所在文件生成目录用例失败时便于查看日志记录
实现方式
pytest.ini文件 在pytest.ini配置文件中设置配置项定义日志输出级别和格式
log_clitrue
log_cli_level DEBUG
log_file_date_format %Y-%m-%d %H:%M:%S
log_file_format %(asctime)s %(filename)s:%(funcName)s:%(lineno)d %(levelname)s:%(message)sconftest.py文件 设置日志文件路径
import loggingpytest.hookimpl(hookwrapperTrue, tryfirstTrue)
def pytest_runtest_setup(item): #pytest_runtest_setup是pytest中hook函数用例执行前都会执行file os.path.basename(item.path) # 获取case所在文件logging.info(file)config item.configlogging_plugin config.pluginmanager.get_plugin(logging-plugin) #获取logging-plugin在_pytest/logging.py文件中可以看到此plugin的实现full_fname os.path.join(logs/, file, item._request.node.name .log) #日志文件路径logging_plugin.set_log_path(full_fname) #设置日志路径yieldtest_case.py 测试文件 如下所示编写测试用例, 会在logs文件下生成目录test_case.py 并且此目录下会有执行的两个用例的日志文件
import logging
test_data [{id: 11111}, {id: 2222}]pytest.mark.parametrize(data, test_data,ids[test1, test2])
class TestCase:def test_case(self,data):logging.inf(test info)pass