因爲項目測試中但願重複執行,因此使用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