参考链接
1、pytest执行用例规则
| pytest fileName.py::className |
| pytest fileName.py::className::methodName |
| pytest -v -s fileName.py |
| //s是带控制台输出结果,也是输出详细运行日志 |
| pytest -v -k "one" fileName.py |
| |
| pytest -v -k "not one" fileName.py |
| |
| pytest -v -k "one or two" fileName.py |
| |
- --maxfail=num错误个数达到指定数量停止测试
| pytest fileName.py --maxfail=1 |
| |
2、setup和teardown
参考链接
unittest |
pytest |
setup() setupClass() teardown() teardownClass() |
模块级(开始于模块始末,全局的):setup_module()、teardown_module() 函数级(只对函数用例生效,不在类中):setup_function()、teardown_function() 类级(只在类中前后运行一次,在类中):setup_class()、teardown_class() 方法级(开始于方法始末,在类中):setup_method()、teardown_method() 方法细化级(运行在调用方法的前后):setup()、teardown() |
| |
| |
| import pytest |
| |
| def setup_module(): |
| print("----all py module start run once setup_module eg. open safari----") |
| |
| def teardown_module(): |
| print("----all py module end run once teardown_module eg. close safari----") |
| |
| def setup_function(): |
| print("----every functional testcase start run everytime setup_function----") |
| |
| def teardown_function(): |
| print("----every functional testcase end run everytime teardown_function----") |
| |
| def test_one(): |
| print("one") |
| |
| def test_two(): |
| print("two") |
| |
| class TestCase(): |
| def setup_class(self): |
| print("====all test class start run once setup_class====") |
| |
| def teardown_class(self): |
| print("====all test class end run once teardown_class====") |
| |
| def setup_method(self): |
| print("====class testcase run start run setup_method====") |
| |
| def teardown_method(self): |
| print("====class testcase run end run teardown_method====") |
| |
| def setup(self): |
| print("====class testcase run start run setup") |
| |
| def teardown(self): |
| print("====class testcase run end run teardown") |
| |
| def test_three(self): |
| print("three") |
| |
| def test_four(self): |
| print("four") |
| |
| if __name__ == '__main__': |
| pytest.main(["-q", "-s", "-ra", "test_setup_teardown.py"]) |
3、fixture
参考链接
fixture的优势:
- 命名方式灵活,不局限于 setup 和 teardown 这几个命名
- conftest.py 配置里可以实现数据共享,不需要 import 就能自动找到 fixture
- scope="module" 可以实现多个 .py 跨文件共享前置
- scope="session" 可以实现多个 .py 跨文件使用一个 session 来完成多个用例
| @pytest.fixture(scope="function", params=None, autouse=False, ids=None, name=None) |
| def test(): |
| print("fixture初始化的参数列表") |
- scope:可以理解成 fixture 的作用范围,默认:function,还有 class、module、package、session
- function 的作用域:每一个函数或方法都会调用
- class 的作用域:每一个类调用一次,一个类中可以有多个方法
- module 的作用域:每一个 .py 文件调用一次,该文件内又有多个 function 和 class
- session 的作用域:是多个文件调用一次,可以跨 .py 文件调用,每个 .py 文件就是 module
- params:一个可选的参数列表,它将导致多个参数调用 fixture 功能和所有测试使用它
- autouse:默认:False,需要用例手动调用该 fixture;如果是 True,所有作用域内的测试用例都会自动调用该 fixture
- ids:每个字符串 id 的列表,每个字符串对应于 params,这样他们就是测试ID的一部分。如果没有提供ID,它们将从 params 自动生成
- name:默认:装饰器的名称,同一模块的 fixture 相互调用建议写不同的名称
| |
| |
| import pytest |
| |
| |
| @pytest.fixture |
| def login(): |
| print("input id login, passport login") |
| |
| def test_s1(login): |
| print("case 1: after login do test_s1") |
| |
| def test_s2(): |
| print("case 2: no login, do test_s2") |
| |
| |
| @pytest.fixture |
| def login2(): |
| print("input id login2, passport login") |
| |
| @pytest.mark.usefixtures("login2", "login") |
| def test_s3(): |
| print("case 3: after login do test_s3") |
| |
| |
| @pytest.fixture(autouse=True) |
| def login3(): |
| print("====login3====") |
| |
| |
| @pytest.mark.usefixtures("login2") |
| def login4(): |
| print("====login4====") |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)