pytest --快速入门

参考文档

https://learning-pytest.readthedocs.io/zh/latest/

环境安装

pip install pytest
检查安装
pytest --version

用例编写

用例默认识别规则

  1. 用例文件:所有文件名为test_开头 或者 _test 开头的文件会被识别为用例文件
  2. 用例类:测试文件中每个Test开头的类就是一个测试用例类
  3. 测试用例:测试类中每个test开头的方法就是一条测试用例,测试文件中每个test开头的函数也是一条测试用例。

上述默认的用例查找规则,可在pytest的配置文件进行修改。
另外pytest兼容unittest,已unittest的用例编写规范写的用例,pytest都能够识别出来

函数形式编写用例

规则:用例方法已test开头即可

# \testcases\test_demo1.py

def test_demo():
    assert 100 == 100

pytest.main执行的参数传递

pytest.main方法执行测试参数传递方式:
pytest.main(['-v','-s'])

所以的参数放在列表中,每个参数就是列表中的一个元素

指定执行的测试目录

pytest 测试目录路径

pytest testcase/

指定执行的测试文件

pytest 测试文件路径

pytest testcase/test_demo1.py

指定执行的测试类

pytest 测试文件::测试类

pytest testcase/test_demo.py::TestClass

指定执行的测试用例

pytest 测试文件::测试类::测试方法

pytest testcase/test_demo1.py::TestClass::test_method

查看

pytest -v 显示测试的详细参数信息
pytest -s 显示测试执行的输出信息

test_demo.py
def test_demo():
    assert 100 == 100


class TestDemo:
    def test_demo1(self):
        print('----测试用例执行-----------')
        assert 11 == 11

    def test_demo2(self):
        print('----测试用例执行-----------')
        assert 22 == 221
posted @   MAU  阅读(47)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示