CodeRoadMap
首页路线题库资源库文章社区
CodeRoadMap

程序员的学习成长路线图。内容免费开放,登录后可同步路线图与课时进度。

由 程序员柏风 独立维护 · 关于作者

学习

  • 路线图
  • 课程
  • 面试题
  • 文章
  • 我

更多

  • 资源
  • 社区
  • 关于作者
  • 内容说明

© 2026 CodeRoadMap

津ICP备2026012044号-1·津公网安备12011002026155号·coderoadmap@126.com
Python 进阶/课时/测试框架
RawBlame
课时目录12 节

Python 进阶

  1. 01Python 进阶地图
  2. 02类型与数据结构
  3. 03OOP 深入
  4. 04函数式特性
  5. 05并发与异步
  6. 06Web 框架
  7. 07包管理与虚拟环境
  8. 08静态类型与校验
  9. 09测试框架
  10. 10代码质量工具
  11. 11算法与数据结构
  12. 12综合练习

测试框架

pytest、unittest、doctest、tox。

30 分钟

测试与 pytest

可靠软件离不开自动化测试。pytest 以简洁断言、fixture 和参数化著称,是 Python 项目事实标准。模块组织见 python-fundamentals;Agent 工具链也需测试,参考 ai-agents-essentials;/roadmaps/python。

学习目标

  • 编写 pytest 测试函数与 assert 语句
  • 使用 fixture 管理测试数据与临时目录
  • 了解 mock、覆盖率与 CI 集成要点

基础测试

# tests/test_stats.py
from pathlib import Path
from lesson_tools.stats import count_chars

def test_count_chars(tmp_path: Path) -> None:
    (tmp_path / "a.md").write_text("hello", encoding="utf-8")
    (tmp_path / "b.md").write_text("world!", encoding="utf-8")
    result = count_chars(tmp_path)
    assert result == {"a.md": 5, "b.md": 6}
pytest -v
pytest tests/test_stats.py::test_count_chars

fixture 与参数化

import pytest

@pytest.fixture
def sample_draft(tmp_path: Path) -> Path:
    (tmp_path / "pya-testing.md").write_text("# Testing\n", encoding="utf-8")
    return tmp_path

@pytest.mark.parametrize("text,expected", [
    ("", 0),
    ("abc", 3),
    ("中文", 2),
])
def test_len_text(text: str, expected: int) -> None:
    assert len(text) == expected

mock 外部依赖

from unittest.mock import AsyncMock, patch
import asyncio

async def fetch_remote(slug: str) -> str:
    ...  # 实际会请求 HTTP

@pytest.mark.asyncio
async def test_fetch_remote() -> None:
    with patch("mymodule.fetch_remote", new_callable=AsyncMock) as mock:
        mock.return_value = "cached content"
        result = await fetch_remote("pya-testing")
        assert "content" in result

安装 pytest-asyncio 以测试 async 代码。

CI 清单

  • PR 必跑 pytest、ruff check、mypy
  • 覆盖率目标:核心模块 ≥ 80%(非硬性,视项目而定)
  • 测试应快、独立、可重复

练习

  • 为 packaging 课的 count_chars 补一个空目录边界测试
  • 用 @pytest.mark.parametrize 测试 slugify 的 3 组输入
  • 在 GitHub Actions 或本地脚本中串联 pytest + ruff

小结

测试锁定行为,typing 锁定形状。 下一课 tooling 统一格式化、lint 与 pre-commit 工作流。

加载本节练习…
← 上一课静态类型与校验下一课 →代码质量工具