因为项目测试中但愿重复执行,因此使用pytest-repeat插件 --count=2, 指定测试的次数,默认是函数级别的测试
--repeat-scope, 指定重复执行的域.
html
pytest test1.py -s --count=5 --repeat-scope=session
@pytest.mark.repeat(5) def test_02(start, open_baidu): print("测试用例test_02")
pytest test.py -n auto
指示根据当前cpu信息自动分配合理的核数运行用例,一样可使用pytest test.py -n 3
指定具体的运行核数,并发进程数@pytest.mark.run(order=7) def test_delete_port(self, host_ip, module_dict, cfg): if "test_create_port" not in module_dict: # skip pytest.skip("do not have test_create_port result") self.delete_port(host_ip, module_dict) @pytest.mark.run(order=8) def test_delete_subnet(self, host_ip, module_dict, cfg, db_session): if "test_create_vpc_subnet" not in module_dict: # skip pytest.skip("do not have test_create_port result") self.delete_subnet(host_ip, module_dict, db_session)
两段代码会先执行7在执行8,若是不指定,则按照名字排序执行。python
# pytest_addoption是pytest已经定义好的fixture(固件)能够直接使用,指定要解析的字段,action表示存储格式 def pytest_addoption(parser): parser.addoption("--cmdopt", action="store", default="dev.ini", help="my option: type1 or type2") parser.addoption("--concurrency", action="store", default='False', help="my option: concurrency") # 定义本身的固件,即其余测试用例经过哪一个名字获取这个定义的--cmdopt参数 @pytest.fixture(scope="module") def cmdopt(request): return request.config.getoption("--cmdopt")
使用allure工具,界面好看,功能全,安装复杂web
def run(i): file_name = '--html=./report/report_{}.html'.format(i[0]) pytest.main(["-v", "-s", '--log-format="%(asctime)s %(levelname)s %(message)s"', '--log-date-format="%Y-%m-%d %H:%M:%S"', file_name, '', '--alluredir', './report/result', i[1]]) # pytest.main(['-v', '--alluredir', 'report/result']) if __name__ == "__main__": import sys if len(sys.argv) > 1: p1 = '--cmdopt={}'.format(sys.argv[1]) num = int(sys.argv[2]) else: p1 = '--cmdopt=dev.ini' num = 1 import multiprocessing pool = multiprocessing.Pool(4) _list = [(x, p1) for x in range(num)] pool.map(run, _list) pool.close() pool.join()
开启日志功能使用log_cli=true浏览器
[pytest] ; addopts = -s -v --html=report/report.html --alluredir ./report/result --count=1 -n auto testpaths = ./ #python_files = test_*.py #python_classes = Test* log_cli=true log_cli_date_format = %Y-%m-%d %H:%M:%S log_cli_format = %(asctime)s %(levelname)s %(message)s log_cli_level = INFO #log_level=NOTSET log_file = ./logs/pytest-logs.txt python_functions = test*
参考:
https://www.jianshu.com/p/ddb...
https://www.cnblogs.com/yoyok... 使用skip跳过用例
https://blog.csdn.net/qq_42610167/article/details/101204066?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-tasksession