Package: pytest-choose
pytest-chooseEnglish| 简体中文为pytest提供基于文本文件收集用例的能力v0.1.0升级说明：v0.0.x版本升级至v0.1.0版本时，请将原--fc-path参数更改为--fc-allow-path升级后规则文件不强制要求完全包含classfunction块，按需编写即可1.安装pipinstallpytest-choose2.参数说明ParameterDescription--fc默认'off'，关闭文件选择。可选项'off', 'on'--fc-coding文件编码, 默认 'utf-8'--fc-allow-path白名单文件路径, 支持多次传入以应用多个规则文件--fc-block-path黑名单文件路径, 支持多次传入以应用多个规则文件3.过滤文件模板3.1.JSON{"@说明":["@注释以‘@’符开头声明"],"path":["@下方填写用例路径","TestClassName"],"marker":["@下方填写需要选中的测试tag","test_function_name"],"class":["@下方填写需要执行的测试类","TestClassName"],"function":["@下方填写需要执行的测试方法","test_function_name"]}4.示例创建测试文件：# test_demo.pyimportpytestclassTestDemo:deftest_demo_1(self):print(1)@pytest.mark.testtdeftest_demo_2(self):print(2)@pytest.mark.testdeftest_demo_3():print(3)deftest_demo_4():print(4)deftest_demo_5():print(5)deftest_demo_6():print(6)deftest_demo_7():print(7)4.1.基本使用创建文件choose.json，用于选择用例:{"class":["TestDemo"],"function":["test_demo_2","test_demo_3","test_demo_4","test_demo_5"]}执行测试:pytest--fc="on"--fc-allow-path="./choose.json"--fc-coding="utf-8"测试结果如下:=======================testsessionstarts=======================platformwin32--Python3.9.6,pytest-7.4.3,pluggy-1.3.0
rootdir:D:\Project\PytestDev\pytest-choose
plugins:choose-0.1.0
collecting...[pytest-choose]<Allowlist>:choose.json->Successfuluseofrules[pytest-choose]Filter5casesandcollect2cases
collected7items

cases\test_choose.py..[100%]========================2passedin0.03s========================4.2.黑名单过滤创建文件filter.json，用于过滤用例:{"function":["test_demo_3","test_demo_4","test_demo_5"]}执行测试:pytest--fc="on"--fc-block-path="./filter.json"--fc-coding="utf-8"测试结果如下:=======================testsessionstarts=======================platformwin32--Python3.9.6,pytest-7.4.3,pluggy-1.3.0
rootdir:D:\Project\PytestDev\pytest-choose
plugins:choose-0.1.0
collecting...[pytest-choose]<Blocklist>:filter.json->Successfuluseofrules[pytest-choose]Filter3casesandcollect4cases
collected7items

cases\test_choose.py....[100%]========================4passedin0.05s========================4.3.黑白名单过滤当同时使用黑白名单时，过滤结果为两个规则的差集（{x∣x∈白名单,且x∉黑名单}）当黑白名单无重合项时，默认黑名单失效。创建文件choose.json，用于选择用例:{"function":["test_demo_2","test_demo_3"]}创建文件filter.json，用于过滤用例:{"function":["test_demo_3","test_demo_4","test_demo_5"]}执行测试:pytest--fc="on"--fc-allow-path="./choose.json"--fc-block-path="./filter.json"--fc-coding="utf-8"测试结果如下:=======================testsessionstarts=======================platformwin32--Python3.9.6,pytest-7.4.3,pluggy-1.3.0
rootdir:D:\Project\PytestDev\pytest-choose
plugins:choose-0.1.0[pytest-choose]<Allowlist>:choose.json->Successfuluseofrules[pytest-choose]<Blocklist>:filter.json->Successfuluseofrules[pytest-choose]Filter6casesandcollect1cases
collected7items

cases\test_choose.py.[100%]========================1passedin0.04s========================4.4.多规则文件过滤当传入规则文件不存在时，仅会抛出响应日志，不会中止测试当传入同类型的多个规则文件时，生效规则为该类型所有规则文件的并集创建文件choose.json，choose_1.json，用于选择用例:{"function":["test_demo_2","test_demo_3"]}{"function":["test_demo_4"]}创建文件filter.json，用于过滤用例:{"function":["test_demo_3","test_demo_4","test_demo_5"]}同时传入不存在的文件filter_1.json，执行测试:pytest--fc="on"--fc-allow-path="./choose.json"--fc-allow-path="./choose_1.json"--fc-block-path="./filter.json"--fc-block-path="./filter_1.json"--fc-coding="utf-8"测试结果如下:=======================testsessionstarts=======================platformwin32--Python3.9.6,pytest-7.4.3,pluggy-1.3.0
rootdir:D:\Project\PytestDev\pytest-choose
plugins:choose-0.1.0[pytest-choose]<Allowlist>:choose.json->Successfuluseofrules[pytest-choose]<Allowlist>:choose_1.json->Successfuluseofrules[pytest-choose]<Blocklist>:filter.json->Successfuluseofrules[pytest-choose]<Blocklist>:filter_1.json->Nosuchfileordirectory[pytest-choose]Filter6casesandcollect1cases
collected7items

cases\test_choose.py.[100%]========================1passedin0.04s========================4.5.用例路径及Tag过滤当传入path参数中路径不存在时会在用例收集阶段触发对应异常Tag参数以and方式拼接创建文件choose.json用于选择用例:{"path":["./cases/"],"marker":["test","not testt"],"function":["test_demo_2","test_demo_3"]}测试结果如下:=======================testsessionstarts=======================platformwin32--Python3.9.6,pytest-7.4.3,pluggy-1.3.0
rootdir:D:\Project\PytestDev\pytest-choose
plugins:choose-0.2.0
collecting...[pytest-choose]<Allowlist>:choose.json->Successfuluseofrules[pytest-choose]Filter5casesandcollect2cases[pytest-choose]Usemarker:testandnottestt[pytest-choose]Usecasepath:['./cases/']collected7items/1deselected/6selectedcases\test_choose.py.[100%]========================1passedin0.04s========================许可证pytest-choose使用 GPLv3 许可证Copyright © 2023 by Azusa.