Python自动化测试:pytest的使用与依赖模拟
立即解锁
发布时间: 2025-09-03 01:44:03 阅读量: 8 订阅数: 20 AIGC 

### Python自动化测试:pytest的使用与依赖模拟
#### 1. 引言
在软件开发中,测试是确保代码质量和稳定性的关键环节。Python的`pytest`库为我们提供了强大而灵活的测试功能,它拥有丰富的插件生态系统,能与数据库、Web服务等其他系统集成,还能通过额外功能扩展测试能力,如代码覆盖率检测、并行测试和性能基准测试等。接下来,我们将逐步学习如何使用`pytest`编写和运行测试用例。
#### 2. 编写和执行测试用例
##### 2.1 准备工作
首先,我们需要安装`pytest`模块。可以将其添加到`requirements.txt`文件中,然后使用`pip`进行安装:
```bash
$ echo "pytest==5.4.1" >> requirements.txt
$ pip install -r requirements.txt
```
同时,我们将使用`test_case.py`文件,你可以从[GitHub仓库](https://github.com/PacktPublishing/Python-Automation-Cookbook-Second-Edition/tree/master/Chapter12/tests)下载该文件。
##### 2.2 具体操作步骤
1. **查看测试文件**:`tests/test_case.py`文件中定义了四个测试函数:
```python
LIST = [1, 2, 3]
def test_one():
# Nothing happens
pass
def test_two():
assert 2 == 1 + 1
def test_three():
assert 3 in LIST
def test_fail():
assert 4 in LIST
```
2. **运行测试文件**:使用`pytest`命令运行测试文件:
```bash
$ pytest tests/test_case.py
```
运行结果如下:
```plaintext
=====================. test session starts ======================
platform darwin -- Python 3.8.2, pytest-5.4.1, py-1.8.1,
pluggy-0.13.1
rootdir: /Python-Automation-Cookbook/Chapter12
collected 4 items
tests/test_case.py ...F
[100%]
============================= FAILURES ===========================
_____________________________ test_fail __________________________
def test_fail():
> assert 4 in LIST
E assert 4 in [1, 2, 3]
tests/test_case.py:16: AssertionError
==================== short test summary info =====================
FAILED tests/test_case.py::test_fail - assert 4 in [1, 2, 3]
=================== 1 failed, 3 passed in 0.03s ==================
```
从结果可以看出,有一个测试用例失败了。
##### 2.3 工作原理
`pytest`允许我们将测试简单地定义为函数。在`tests/test_case.py`文件中,每个以`test`开头的函数都被视为一个测试用例。这些测试用例的基本原理是执行代码并包含一个或多个断言,用于验证代码的正确性。具体分析如下:
- `test_one`:没有任何断言,因此总是会通过。
- `test_two`:使用`==`运算符检查加法运算是否正确。
- `test_three`:使用`in`运算符检查某个值是否包含在列表中。
- `test_fail`:同样使用`in`运算符,但由于`4`不在列表`[1, 2, 3]`中,所以该测试用例失败。
在运行测试时,`pytest`会先收集所有测试用例,然后依次执行。每个通过的测试用例用一个点表示,失败的用`F`表示。默认情况下,只有以`test`开头的函数才会被视为测试用例,不过这个配置可以根据需要进行更改,相关文档可参考[这里](https://docs.pytest.org/en/latest/goodpractices.html#test-discovery)。如果终端支持颜色显示,失败的测试用例会标记为红色,通过的标记为绿色。对于失败的测试用例,`pytest`会显示断言失败的确切行号和详细信息,方便我们定位和修复问题。
##### 2.4 更多信息
默认情况下,`pytest`显示的信息几乎不包含通过的测试用例,这样可以让我们专注于失败的测试。如果想显示每个测试用例的详细信息,可以使用`-v`或`--verbose`标志:
```bash
$ pytest -v tests/test_case.py
```
运行结果如下:
```plaintext
======================= test session starts ========================
platform darwin -- Python 3.8.2, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 --
/usr/local/opt/[email protected]/bin/python3.8
cachedir: .pytest_cache
rootdir: Python-Automation-Cookbook/Chapter12
collected 4 items
tests/test_case.py::test_one PASSED [ 25%]
tests/test_case.py::test_two PASSED [ 50%]
tests/test_case.py::test_three PASSED [ 75%]
tests/test_case.py::test_fail FAILED [100%]
============================= FAILURES =============================
____________________________ test_fail _____________________________
def test_fail():
> assert 4 in LIST
E assert 4 in [1, 2, 3]
tests/test_case.py:18: AssertionError
===================== short test summary info ======================
FAILED tests/test_case.py::test_fail - assert 4 in [1, 2, 3]
=================== 1 failed, 3 passed in 0.03s ===================
```
#### 3. 测试外部代码
##### 3.1 测试目标
测试的主要目的是检查测试文件范围之外的代码。我们可以在测试中轻松导入外部代码,然后验证其是否按预期工作。
##### 3.2 准备工作
同样,我们需要安装`pytest`模块:
```bash
$ echo "pytest==5.4.1" >> requirements.txt
$
```
0
0
复制全文
相关推荐









