Python提供了多种测试工具,其中pytest是最流行的单元测试框架之一。本文将介绍Python中的assert语句以及pytest框架的基本用法。
一、assert语句
1. 基本用法
1 2 3 4 5 6 7
| x = 10 assert x > 0
x = -5 assert x > 0, "x必须大于0"
|
2. 常见用途
1 2 3 4 5 6 7 8 9 10
| def divide(a, b): assert b != 0, "除数不能为零" return a / b
def validate_age(age): assert 0 <= age <= 150, "年龄必须在0到150之间" return age
print(validate_age(25))
|
二、pytest框架
1. 基本安装和使用
2. 编写测试函数
1 2 3 4 5 6 7 8
| def add(a, b): return a + b
def test_add(): assert add(1, 2) == 3 assert add(-1, 1) == 0 assert add(0, 0) == 0
|
3. 运行测试
1 2 3
| pytest test_math.py pytest test_math.py::test_add pytest -v
|
三、pytest常用功能
1. 断言
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| def test_assertions(): assert 1 == 1
assert 1 != 2
assert True assert not False
assert "a" in "abc" assert 1 in [1, 2, 3]
assert isinstance(1, int) assert isinstance("hello", str)
|
2. 异常测试
1 2 3 4 5 6 7 8 9 10
| import pytest
def divide(a, b): if b == 0: raise ValueError("除数不能为零") return a / b
def test_divide_by_zero(): with pytest.raises(ValueError): divide(1, 0)
|
3. 参数化测试
1 2 3 4 5 6 7 8 9
| import pytest
@pytest.mark.parametrize("a,b,expected", [ (1, 2, 3), (0, 0, 0), (-1, 1, 0), ]) def test_add(a, b, expected): assert a + b == expected
|
四、综合示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
""" 测试示例 """
def add(a, b): """加法函数""" return a + b
def subtract(a, b): """减法函数""" return a - b
def multiply(a, b): """乘法函数""" return a * b
def divide(a, b): """除法函数""" if b == 0: raise ValueError("除数不能为零") return a / b
def test_add(): assert add(1, 2) == 3 assert add(-1, 1) == 0
def test_subtract(): assert subtract(5, 3) == 2 assert subtract(1, 1) == 0
def test_multiply(): assert multiply(3, 4) == 12 assert multiply(0, 100) == 0
def test_divide(): assert divide(10, 2) == 5 assert divide(9, 3) == 3
def test_divide_by_zero(): import pytest with pytest.raises(ValueError): divide(1, 0)
|
五、注意事项
1. 不要过度使用assert
1 2 3 4 5 6
|
if b == 0: raise ValueError("除数不能为零")
|
2. 测试覆盖
3. 测试文件命名
1 2
| test_example.py # 测试文件以test_开头 _example_test.py # 或以_test结尾
|