第六十四回 托塔天王梦中显圣 浪里白条水上报冤-测试框架pytest学习与实践

宋江亲自给索超解绑,劝其归顺了梁山。

宋江一天晚上做梦,梦见晁盖让说他背上之事发了,只有江南地灵星可免无事。宋江与吴用讲了这件事,第二天就发烧不起,背上长了毒疮。张顺说安道全可以治这个病,于是派张顺到江南找安道全。

张顺到了杨子江边,上了一艘渡船,因为连日赶路辛苦,吃了一碗饭倒头就睡了。结果被船家张旺绑了,到了江心给扔江里去了。张顺咬断绳子,游到南岸。见到了活闪婆王定六,给了张顺一身衣裳,并杀鸡置酒招待。

张顺杨子江上被打劫,真是阴沟里帆船。俗话说小心驶得万年船,项目测试非常重要。

测试框架pytest学习与实践

在维护和构建大型项目时,单独一个一个手工测试代码已经不适用了,这时候就要用专门的测试框架进行测试。

pytest是一个专业的测试框架,可以帮助我们对python项目进行测试,提高测试的效率。

pytest官网手册:pytest: helps you write better programs --- pytest documentation

中文手册:Pytest 教程

入门学习

安装pytest

bash 复制代码
pip install pytest

写例子test_sample.py

python 复制代码
def inc(x):
    return x + 1


def test_answer():
    assert inc(3) == 5


def test_answer2():
    assert inc(3) == 4

注意里面的测试函数都是test_开头,按照pytest的规范,需要以"test"4个字母开头的函数才会被测试。

执行测试

python 复制代码
pytest test_sample.py

显示:

python 复制代码
==================================== FAILURES =====================================
___________________________________ test_answer ___________________________________

    def test_answer():
>       assert inc(3) == 5
E       assert 4 == 5
E        +  where 4 = inc(3)

test_sample.py:9: AssertionError
============================= short test summary info =============================
FAILED test_sample.py::test_answer - assert 4 == 5
=========================== 1 failed, 1 passed in 0.20s ===========================

可以看到,两个测试函数,assert inc(3) == 5的这个没有测试通过。

pytest提高篇

当前目录和子目录的所有类似test_*.py*_test.py 的文件,都会自动参与测试。

比如我们在当前目录下,再写一个文件test_sysexit.py:

python 复制代码
# content of test_sysexit.py
import pytest


def f():
    raise SystemExit(1)


def test_mytest():
    with pytest.raises(SystemExit):
        f()

然后在当前目录直接执行pytest命令,则会自动测试test_sample.py test_sysexit.py这两个文件,并给出测试结果:

bash 复制代码
collected 3 items                                                                 

test_sample.py F.                                                           [ 66%]
test_sysexit.py .                                                           [100%]

==================================== FAILURES =====================================
___________________________________ test_answer ___________________________________

    def test_answer():
>       assert inc(3) == 5
E       assert 4 == 5
E        +  where 4 = inc(3)

test_sample.py:9: AssertionError
============================= short test summary info =============================
FAILED test_sample.py::test_answer - assert 4 == 5
=========================== 1 failed, 2 passed in 0.46s ===========================

在kotti项目里集成测试

在Kotti项目中进行pytest测试可以参考这个文档:以Kotti项目为例使用pytest测试项目-CSDN博客

下载安装Kotti库

pip install https://atomgit.com/skywalk/Kotti
cd Kotti
pip install -r requirements.txt
python3 setup.py develop --user

安装好pytest和其需要的库

bash 复制代码
pip install pytest
pip install mock
pip install pytest-flake8 pytest-cov
pip install webtest zope.testbrowser

执行测试

bash 复制代码
~/github/Kotti]$ pytest

输出类似:

pytest

=============================== test session starts ===============================

platform linux -- Python 3.10.12, pytest-8.1.1, pluggy-1.4.0

rootdir: /home/skywalk/github/kotti

configfile: pytest.ini

plugins: cov-5.0.0, flake8-1.1.1, Kotti-2.0.10.dev0

collected 454 items

kotti/init.py F

kotti/alembic/env.py F

kotti/alembic/versions/1063d7178fa_add_node_path_column.py F

kotti/tests/init.py F

kotti/tests/conftest.py F

kotti/tests/test_app.py F................

kotti/tests/test_cache.py F......

F表示测试失败,后面的绿点表示测试成功,每个文档第一个测试是flake8,这个可以先不用去管,具体见文档:pytest的时候输出一个F后面跟很多绿色的点解读-CSDN博客

最后,应该将pytest测试集成到你的持续集成/持续部署(CI/CD)流程中,这样每次代码变更时都会自动运行测试。这有助于及早发现和修复问题,保持代码的健康和稳定。

张顺去健康府找安道全,安道全放不下相好的李巧奴,不想去梁山。可巧晚上张旺来找安道全李巧奴,张顺杀了虔婆、两个下人和李巧奴,却让张旺跑了。于是满屋写下杀人者安道全,这样安道全只好跟张顺去梁山。

过河又遇到张旺,张顺说我生在浔阳江边,长在小孤山下,做卖鱼牙子,天下闻名!跟了宋江之后纵横天下,谁不怕我!结果你竟然敢打劫我,要不是我会水,就没命了。最后说饶你不得,然后把张旺手脚捆在一起,丢到了江中。

安道全是个文人,走路慢,在一家店里碰见神行太保戴宗。戴宗说宋江都快死了,于是做法带着安道全先走了。张顺在店里歇了两三天,碰到了赶来的王定六,于是跟王定六一起回了梁山。

安道全到了梁山,很快就把宋江病治好了。宋江想打大名救卢俊义,安道全说疮口刚好,不能轻易运动。吴用也劝宋江好好休息调理,说初春的时候就能打下大名城。

吴用怎么打大名,且听下回分解。

相关推荐
yngsqq9 分钟前
031集——文本文件按空格分行——C#学习笔记
笔记·学习·c#
_.Switch16 分钟前
Python机器学习模型的部署与维护:版本管理、监控与更新策略
开发语言·人工智能·python·算法·机器学习
Hoper.J35 分钟前
PyTorch 模型保存与加载的三种常用方式
人工智能·pytorch·python
zengy536 分钟前
Effective C++中文版学习记录(三)
数据结构·c++·学习·stl
cyt涛1 小时前
MyBatis 学习总结
数据库·sql·学习·mysql·mybatis·jdbc·lombok
Willliam_william1 小时前
SystemC学习(1)— SystemC安装与HelloWorld
学习
sealaugh321 小时前
aws(学习笔记第一课) AWS CLI,创建ec2 server以及drawio进行aws画图
笔记·学习·aws
布丁不叮早起枣祈1 小时前
10.5学习
学习
弱冠少年1 小时前
websockets库使用(基于Python)
开发语言·python·numpy
向上的车轮1 小时前
Django学习笔记五:templates使用详解
笔记·学习·django