免费python编程教程:
https://pan.quark.cn/s/2c17aed36b72
凌晨两点,小王盯着屏幕上的报错信息,额头冒汗。明天就要上线的新功能,刚才合并代码后,突然不知道哪里出了问题。更可怕的是,他不确定这个bug影响了多少功能。这已经是本月第三次因为回归测试不充分导致的线上故障了。
小王的困境,是无数开发团队的缩影。手动测试耗时耗力,覆盖不全,还容易遗漏。而自动化测试,特别是Python生态中最优雅的pytest框架,正是解决这一困境的良药。
为什么是pytest
Python的测试框架不止一个,unittest是Python标准库自带的,nose也曾风靡一时。但pytest凭借其简洁的语法、强大的插件体系和丰富的断言方式,成为了最受欢迎的选择。
安装pytest非常简单:
pip install pytest
验证安装成功:
pytest --version
从一个简单函数开始
假设我们正在开发一个电商系统,需要一个计算订单折扣的函数。业务规则是:满1000元打9折,满500元打95折,会员额外享受折上9.5折。
discount.py
def calculate_discount(amount, is_member=False):
if amount < 0:
raise ValueError("金额不能为负数")
discount_rate = 1.0
if amount >= 1000:
discount_rate = 0.9
elif amount >= 500:
discount_rate = 0.95
if is_member:
discount_rate *= 0.95
return round(amount * discount_rate, 2)
第一个测试用例
在同一个目录下创建测试文件,pytest会自动发现以test_开头或结尾的文件。
test_discount.py
import pytest
from discount import calculate_discount
def test_normal_customer_no_discount():
"""普通用户未达到折扣门槛"""
assert calculate_discount(300) == 300
def test_normal_customer_500_discount():
"""普通用户满500打95折"""
assert calculate_discount(500) == 475.0
assert calculate_discount(800) == 760.0
def test_normal_customer_1000_discount():
"""普通用户满1000打9折"""
assert calculate_discount(1000) == 900.0
assert calculate_discount(1500) == 1350.0
运行测试:
pytest test_discount.py -v
-v参数让输出更详细。看到绿色的点,表示测试通过。
异常测试
测试不仅要验证正常情况,还要验证异常情况。pytest提供了简洁的异常断言:
def test_negative_amount():
"""测试金额为负数时抛出异常"""
with pytest.raises(ValueError, match="金额不能为负数"):
calculate_discount(-100)
参数化测试
写测试时,我们经常需要测试多组数据。手动写多个测试函数既冗余又难维护。pytest的参数化功能完美解决这个问题:
@pytest.mark.parametrize("amount, is_member, expected", [
(300, False, 300), # 普通用户,无折扣
(500, False, 475.0), # 普通用户,500档
(1000, False, 900.0), # 普通用户,1000档
(300, True, 300 0.95), # 会员,无门槛折扣
(500, True, 500 0.95 0.95), # 会员,500档叠加会员折扣
(1000, True, 1000 0.9 * 0.95), # 会员,1000档叠加会员折扣
])
def test_discount_cases(amount, is_member, expected):
"""参数化测试多种场景"""
assert calculate_discount(amount, is_member) == expected
这样,一组数据就是一个测试用例。增加测试数据只需要在列表中追加,不需要新增函数。
固件(Fixture)的妙用
实际项目中,测试往往需要准备复杂的测试数据。比如测试用户订单,需要创建用户、商品、库存、优惠券等。如果每个测试函数都重复这些准备工作,代码会变得臃肿。
pytest的fixture解决了这个问题:
import pytest
from datetime import datetime, timedelta
@pytest.fixture
def sample_user():
"""创建一个测试用户"""
return {
"id": 1,
"name": "测试用户",
"is_member": True,
"join_date": datetime.now() - timedelta(days=30)
}
@pytest.fixture
def sample_order():
"""创建一个测试订单"""
return {
"id": 1001,
"items": [
{"product_id": 1, "name": "商品A", "price": 299, "quantity": 2},
{"product_id": 2, "name": "商品B", "price": 199, "quantity": 1}
],
"total_amount": 797
}
def test_order_with_member_discount(sample_user, sample_order):
"""测试会员订单折扣"""
user = sample_user
order = sample_order
# 计算折扣后的金额
discounted = calculate_discount(order["total_amount"], user["is_member"])
expected = 797 * 0.95 # 会员95折
assert discounted == expected
fixture的强大之处在于它可以自动管理资源的创建和清理,可以设置作用域(function、class、module、session),还可以相互依赖。
模拟(Mock)外部依赖
实际开发中,函数往往会调用外部服务——数据库、API、文件系统等。测试时需要隔离这些依赖,让测试既快速又可靠。
假设我们的折扣函数需要调用会员积分服务:
def calculate_discount_with_points(amount, user_id):
"""根据用户积分计算折扣"""
points = get_user_points(user_id) # 调用外部API
if points > 1000:
discount = 0.8
elif points > 500:
discount = 0.9
else:
discount = 1.0
return amount * discount
测试时,我们不希望真的调用积分服务。使用pytest-mock插件(基于unittest.mock):
def test_discount_with_points(mocker):
"""模拟积分服务返回值"""
# 模拟get_user_points函数返回固定值
mocker.patch('discount.get_user_points', return_value=600)
from discount import calculate_discount_with_points
result = calculate_discount_with_points(1000, 123)
assert result == 900 # 600积分,打9折
测试覆盖率
写了测试,如何知道测了多少代码?pytest-cov插件可以统计测试覆盖率:
pip install pytest-cov
pytest --cov=discount test_discount.py --cov-report=html
这条命令会生成HTML格式的覆盖率报告,直观展示哪些代码被测试覆盖,哪些没有。
持续集成中的pytest
测试只有持续运行才有意义。配置CI/CD流水线,每次代码提交自动运行测试:
.github/workflows/test.yml
name: Run tests
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.9'
- name: Install dependencies
run: |
pip install pytest pytest-cov
pip install -r requirements.txt
- name: Run tests
run: pytest --cov=./ --cov-report=xml
测试分层策略
回到小王的故事。他的团队代码混乱,测试无从下手。我建议他采用测试金字塔策略:
单元测试:测试单个函数、类。用mock隔离依赖,追求快速执行。覆盖率目标80%以上。
集成测试:测试模块间的交互,如数据库操作、API调用。可以用测试数据库,每次测试后回滚。
端到端测试:模拟用户操作,测试完整业务流程。运行最慢,数量最少。
集成测试示例
@pytest.fixture
def test_db():
"""创建测试数据库连接"""
db = create_test_database()
yield db
db.cleanup() # 测试后清理
def test_save_order_to_database(test_db):
"""测试订单保存到数据库"""
order = create_sample_order()
order_id = test_db.save_order(order)
saved_order = test_db.get_order(order_id)
assert saved_order.total_amount == order.total_amount
实际项目中的pytest配置
大型项目中,pytest配置文件很有用。创建pytest.ini:
[pytest]
testpaths = tests
pythonfiles = test.py
python_classes = Test
pythonfunctions = test*
markers =
slow: 运行较慢的测试
integration: 集成测试
smoke: 冒烟测试
addopts = -v --strict-markers --tb=short
使用标记运行特定测试:
pytest -m "not slow" # 跳过慢测试
pytest -m "smoke" # 只跑冒烟测试
处理测试数据
测试数据管理是个常见痛点。pytest-datadir插件帮助管理测试文件:
def test_import_data(datadir):
"""使用datadir中的测试文件"""
csv_file = datadir / 'test_data.csv'
data = read_csv(csv_file)
assert len(data) == 10
并发测试
测试多了,运行时间变长。pytest-xdist实现并发测试:
pip install pytest-xdist
pytest -n auto # 自动使用所有CPU核心
失败重试
网络不稳定的测试环境,可以用pytest-rerunfailures自动重试失败用例:
pip install pytest-rerunfailures
pytest --reruns 3 --reruns-delay 1
从0到1建立测试体系
三个月后,小王团队的测试覆盖率从5%提升到了78%。他们是怎么做到的?
从核心逻辑开始:先为业务核心的函数编写单元测试,这类代码改动频繁,测试收益最高。
修复bug先加测试:遇到bug,先写一个会失败的测试用例,再修复代码。这样既验证了修复,又防止回归。
测试代码也是代码:保持测试代码整洁,像生产代码一样review。复杂的测试逻辑同样需要注释。
不追求100%覆盖率:覆盖率是参考,不是目标。UI层、第三方集成等代码测试成本高,可以适当放低要求。
结语
测试不是额外的工作,而是开发的一部分。pytest让测试变得如此简单,以至于你会有写测试的冲动。
现在的小王,下班前运行一下测试,看到一片绿色,安心地关掉电脑。即使凌晨被叫起来,他也能自信地说:“测试都通过了,问题不在我们这边。”
自动化测试不能消灭所有bug,但能让你睡个安稳觉。从今天开始,用pytest给你的代码上份保险吧。