PyTest核心教程(从入门到精通,实战版)

张开发
2026/6/6 16:42:31 15 分钟阅读
PyTest核心教程(从入门到精通,实战版)
分享一个大牛的人工智能教程。零基础通俗易懂风趣幽默希望你也加入到人工智能的队伍中来请轻击人工智能教程​​https://www.captainai.net/troubleshooter这是一份最实用、最精简、可直接落地项目的 PyTest 核心教程覆盖 90% 日常测试需求适合自动化测试、单元测试、接口测试使用。一、安装与基础运行1. 安装pip install pytest2. 命名规则必须遵守PyTest自动识别测试文件test_*.py或*_test.py测试函数 / 方法test_*测试类Test*类内不能有__init__3. 运行命令最常用pytest # 运行所有测试 pytest -v # 详细输出推荐 pytest -s # 显示 print 内容 pytest test_demo.py # 运行单个文件 pytest test_demo.py::test_func # 运行单个用例二、最简单的测试用例示例test_demo.py# 最简单的测试函数形式 def test_add(): assert 1 2 3 # 测试类形式 class TestDemo: def test_sub(self): assert 5 - 2 3运行pytest test_demo.py -v✅PyTest 最大优势只用原生 assert不用任何复杂语法三、断言核心PyTest 断言超级简单全部用 Python 自带assert。1. 常用断言assert 1 1 # 相等 assert 2 1 # 大小 assert a in abc # 包含 assert True # 布尔判断 assert [1,2] [1,2] # 列表相等2. 异常断言必学验证代码是否抛出预期异常import pytest def test_zero_division(): with pytest.raises(ZeroDivisionError): 1 / 03. 浮点数断言必学assert 0.1 0.2 pytest.approx(0.3)四、Fixture测试神器Fixture 前置 后置 共享数据替代传统setup/teardown是 PyTest 最核心功能。1. 基础 Fixtureimport pytest # 定义 fixture pytest.fixture def user(): print(\n前置准备用户数据) yield {name: test, age: 18} # 返回数据给测试用例 print(\n后置清理数据) # 测试用例直接使用传参即可 def test_user(user): assert user[age] 182. Fixture 作用域重点pytest.fixture(scopefunction) # 默认每个函数执行一次 pytest.fixture(scopeclass) # 每个类一次 pytest.fixture(scopemodule) # 每个文件一次 pytest.fixture(scopesession) # 整个测试会话一次全局3. 自动执行 Fixturepytest.fixture(autouseTrue) # 自动运行不用传参 def setup(): print(每个用例自动执行前置)4. conftest.py全局共享项目根目录创建conftest.py所有测试文件都能使用里面的 fixture无需导入。五、参数化测试数据驱动一组测试逻辑 多组测试数据 批量运行大幅减少重复代码。1. 基础参数化import pytest pytest.mark.parametrize(a, b, expected, [ (1, 2, 3), (4, 5, 9), (0, 0, 0), ]) def test_add(a, b, expected): assert a b expected2. 给用例起名字清晰pytest.mark.parametrize(a, b, expected, [ (1, 2, 3), (4, 5, 9), ], ids[正常1, 正常2])六、标记用例skip/xfail/ 自定义1. 跳过用例pytest.mark.skip(暂时不执行) def test_skip(): assert 1 22. 条件跳过import sys pytest.mark.skipif(sys.platform win32, reasonWindows不支持) def test_linux_only(): pass3. 预期失败已知 Bugpytest.mark.xfail(reasonBug未修复) def test_known_fail(): assert False4. 自定义标记冒烟 / 回归pytest.mark.smoke # 冒烟用例 def test_login(): assert True运行pytest -m smoke七、常用插件必装1. 生成美观 HTML 报告pip install pytest-html pytest --htmlreport.html2. 测试覆盖率pip install pytest-cov pytest --cov3. 多进程并行运行加速pip install pytest-xdist pytest -n auto八、标准项目结构企业级项目/ ├── conftest.py # 全局 fixture ├── pytest.ini # 配置文件 ├── src/ # 业务代码 └── tests/ # 测试目录 ├── test_api.py ├── test_db.py └── test_user.pypytest.ini 示例[pytest] testpaths tests python_files test_*.py python_classes Test* python_functions test_* markers smoke: 冒烟测试 api: 接口测试九、完整实战示例可直接复制import pytest # Fixture pytest.fixture def calc(): class Calc: def add(self, a, b): return a b def div(self, a, b): return a / b return Calc() # 参数化 pytest.mark.parametrize(a,b,expected, [(1,2,3), (4,5,9)]) def test_add(calc, a, b, expected): assert calc.add(a, b) expected # 异常测试 def test_div_zero(calc): with pytest.raises(ZeroDivisionError): calc.div(1, 0) # 跳过用例 pytest.mark.skip def test_skip(): assert 1 1核心总结记住这 6 点就够了文件 / 函数以 test_ 开头自动识别只用 assert断言简单强大Fixture做前置 / 后置 / 数据共享parametrize实现数据驱动mark标记skip/xfail/smoke插件报告、覆盖率、并行执行

更多文章