📝 面试求职: 「面试试题小程序」 ,内容涵盖 测试基础、Linux操作系统、MySQL数据库、Web功能测试、接口测试、APPium移动端测试、Python知识、Selenium自动化测试相关、性能测试、性能测试、计算机网络知识、Jmeter、HR面试,命中率杠杠的。(大家刷起来…)
📝 职场经验干货:
PyTest,自动化测试的Python守护者!
今天我们要学习的是一个非常强大的测试框架——PyTest。想象一下,你正在开发一个复杂的软件项目,并希望确保每个功能都能正常工作,这时PyTest就能派上大用场了。它的主要特点是能够编写简洁易读的测试用例,并且支持多种高级特性,如参数化测试、 fixtures 和插件系统。接下来,我们将一起探索如何使用PyTest来进行自动化测试。
什么是PyTest?
PyTest是一个开源的Python测试框架,专门用于编写和运行测试用例。它的主要特点包括:
-
简洁易读:提供简洁的语法来编写测试用例。
-
强大功能:支持参数化测试、fixtures 和插件系统。
-
社区活跃:拥有庞大的用户社区和支持丰富的插件生态系统。
接下来,我们将一起探索如何使用PyTest来进行自动化测试。
安装PyTest
首先,我们需要安装PyTest。你可以使用pip命令来安装它:
pip install pytest
安装完成后,我们就可以开始编写测试用例了。
编写第一个测试用例
为了演示PyTest的功能,我们将编写一个简单的测试用例来验证一个数学函数。
创建Python脚本
# math_operations.py
def add(x, y):
return x + y
def subtract(x, y):
return x - y
def multiply(x, y):
return x * y
def divide(x, y):
if y == 0:
raise ValueError("Cannot divide by zero.")
return x / y
在这段代码中,我们创建了一个包含基本数学运算的模块math_operations.py。
创建测试文件
# test_math_operations.py
from math_operations import add, subtract, multiply, divide
def test_add():
assert add(2, 3) == 5
assert add(-1, 1) == 0
assert add(-1, -1) == -2
def test_subtract():
assert subtract(5, 3) == 2
assert subtract(-1, 1) == -2
assert subtract(-1, -1) == 0
def test_multiply():
assert multiply(2, 3) == 6
assert multiply(-1, 1) == -1
assert multiply(-1, -1) == 1
def test_divide():
assert divide(10, 2) == 5
assert divide(-6, 2) == -3
assert divide(-6, -2) == 3
try:
divide(10, 0)
except ValueError as e:
assert str(e) == "Cannot divide by zero."
在这段代码中,我们创建了一个测试文件test_math_operations.py,其中包含了对math_operations.py中各个函数的测试用例。
运行测试用例
pytest
运行上述命令后,PyTest会自动发现并运行所有以test_开头的函数。你会看到如下输出:
============================= test session starts ==============================
platform linux -- Python 3.x.y, pytest-x.y.z, py-x.y.z, pluggy-x.y.z
rootdir: /path/to/your/project
collected 4 items
test_math_operations.py .... [100%]
============================== 4 passed in 0.01s ===============================
所有测试用例都通过了。
参数化测试
参数化测试允许我们在一个测试函数中运行多个不同的输入组合。
修改测试文件
# test_math_operations.py
import pytest
from math_operations import add, subtract, multiply, divide
@pytest.mark.parametrize("x, y, expected", [(1, 2, 3), (-1, 1, 0), (-1, -1, -2)])
def test_add(x, y, expected):
assert add(x, y) == expected
@pytest.mark.parametrize("x, y, expected", [(5, 3, 2), (-1, 1, -2), (-1, -1, 0)])
def test_subtract(x, y, expected):
assert subtract(x, y) == expected
@pytest.mark.parametrize("x, y, expected", [(2, 3, 6), (-1, 1, -1), (-1, -1, 1)])
def test_multiply(x, y, expected):
assert multiply(x, y) == expected
@pytest.mark.parametrize("x, y, expected", [(10, 2, 5), (-6, 2, -3), (-6, -2, 3)])
def test_divide(x, y, expected):
assert divide(x, y) == expected
def test_divide_by_zero():
with pytest.raises(ValueError) as excinfo:
divide(10, 0)
assert str(excinfo.value) == "Cannot divide by zero."
在这段代码中,我们使用@pytest.mark.parametrize装饰器为每个测试函数添加了多个输入组合。同时,我们还添加了一个新的测试函数test_divide_by_zero来测试除零异常。
运行测试用例
pytest
运行上述命令后,PyTest会自动发现并运行所有以test_开头的函数。你会看到如下输出:
============================= test session starts ==============================
platform linux -- Python 3.x.y, pytest-x.y.z, py-x.y.z, pluggy-x.y.z
rootdir: /path/to/your/project
collected 8 items
test_math_operations.py ........ [100%]
============================== 8 passed in 0.02s ===============================
所有测试用例都通过了。
使用Fixtures
Fixtures是PyTest中的一种机制,用于在测试前后执行一些准备工作或清理工作。
创建Fixture
# conftest.py
import pytest
@pytest.fixture
def sample_data():
return {
'a': 1,
'b': 2,
'c': 3
}
在这段代码中,我们创建了一个名为sample_data的fixture,返回一个字典。
使用Fixture
# test_fixtures.py
def test_sample_data(sample_data):
assert sample_data['a'] == 1
assert sample_data['b'] == 2
assert sample_data['c'] == 3
在这段代码中,我们创建了一个测试文件test_fixtures.py,并在测试函数中使用了sample_data fixture。
运行测试用例
pytest
运行上述命令后,PyTest会自动发现并运行所有以test_开头的函数。你会看到如下输出:
============================= test session starts ==============================
platform linux -- Python 3.x.y, pytest-x.y.z, py-x.y.z, pluggy-x.y.z
rootdir: /path/to/your/project
collected 9 items
test_math_operations.py ........ [ 88%]
test_fixtures.py . [100%]
============================== 9 passed in 0.02s ===============================
所有测试用例都通过了。
插件系统
PyTest拥有丰富的插件生态系统,可以扩展其功能。
安装插件
例如,我们可以安装pytest-html插件来生成HTML格式的测试报告:
pip install pytest-html
运行测试并生成报告
pytest --html=report.html
运行上述命令后,PyTest会生成一个名为report.html的测试报告文件。
查看报告
打开report.html文件,你会看到详细的测试报告。
练习题
-
尝试编写更多的测试用例,覆盖更多的情况。
-
使用参数化测试来测试其他函数。
-
创建一个新的fixture,并在多个测试函数中使用它。
-
安装并使用其他插件,比如
pytest-cov来生成覆盖率报告。
总结
今天我们一起学习了PyTest的基本用法,包括安装、编写测试用例、参数化测试、使用fixtures和插件系统。希望你能感受到PyTest的强大之处,也希望能激发你对自动化测试的兴趣。
记得动手实践哦!试着编写更多的测试用例,添加更多的功能和资源,甚至尝试其他测试工具。祝你在学习的路上越走越远!
小贴士:想要了解更多高级特性,比如自定义标记、配置文件等等,建议阅读官方文档:PyTest Documentation
最后: 下方这份完整的软件测试视频教程已经整理上传完成,需要的朋友们可以自行领取【保证100%免费】

699

被折叠的 条评论
为什么被折叠?



