pytest执行测试前置和后置环境信息处理

    科技2022-07-11  112

    测试框架结构

    ├─config ----配置文件目录 ├─report ----测试报告目录 │ ├─allure ----allure测试报告 │ │ ├─allure_report │ │ ├─assets │ │ └─tmp │ └─htmlcov ----覆盖率测试报告 ├─run_interface_test ----接口测试运行入口 ├─service ----封装业务操作 │ ├─api │ │ ├─Business │ │ ├─common │ │ ├─department │ │ │ ├─accounts ├─test_case ----测试用例目录 │ ├─Manager │ │ ├─Organization │ │ │ ├─Accounts │ │ │ │ ├─ConstractsType │ │ │ │ │ ├─Contarcts ├─test_data ----测试数据目录 └─utils ----常用库封装 ├─ApiUtils ├─CommonUtils ├─WebLib

    一、 测试环境初始化

    1、测试人口:执行测试

    # run_interface.py # 运行整个测试文件目录 pytest.main(["-s", "../test_case", "--reruns=2", "--reruns-delay=1", "--html=../report/allure/user.html", "--junitxml=../report/allure/user.xml", "--alluredir", "../report/allure/tmp/", "-vv", "--cov=..\\test_case", "--cov-report=html"]) # 测试代码覆盖率 os.system("allure generate ../report/allure/tmp -o ../report/allure/allure_report --clean") os.system(f"move htmlcov {rootPath}\\report")

    2、Fixture操作:获取相关库信息,写入environment.properties文件中

    @pytest.fixture(scope="session", autouse=True) def init_env_info(): # 置前操作 with allure.step("删除allure报告"): print("删除allure报告") os.system(r"rd /s /q ..\report\allure\tmp") os.system(r"rd /s /q ..\report\allure\allure_report") with allure.step("删除测试覆盖率测试报告"): print("删除测试覆盖率测试报告") os.system(r"rd /s /q ..\report\htmlcov") with allure.step("创建allure测试报告tmp文件目录"): os.system(r"mkdir ..\report\allure\tmp") os.system(r"mkdir ..\report\allure\allure_report") yield # 置后操作 with allure.step("环境配置文件信息"): lib_versions = { "python": "python -V", "Browser": "Chrome 138" } f = open(f"{sys.path[0]}/report/allure/tmp/environment.properties", "a+") with allure.step("获取相关库的版本信息"): lib_versions_list = ["pytest","selenium","xlrd","requests"] for lib_version_one in lib_versions_list: result = os.popen(f"pip3 list|findstr {lib_version_one}").read() for line in result.split("\n"): if line: lib_info = line.split(" ") lib_str = lib_info[0] lib_version = lib_info[-1] f.write(f"{lib_str}={lib_version}\n") for K, V in lib_versions.items(): try: stdout, stderr = subprocess.Popen(V, stdout=subprocess.PIPE).communicate() result = stdout.decode("gbk") f.write(f"{K}={result}") except EnvironmentError: f.write(f"{K}={V}") f.close()
    Processed: 0.025, SQL: 8