Pytest-1

1.安装

pip install -U pytest
#查看是否安装成功
pytest --version

2.新建一个test_case.py文件

文件命名以test_开头或者_test结尾
测试函数以test_开头
断言使用assert

3.文件中写测试代码

# content of test_sample.py
def func(x):
    return x +1

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

4.执行测试用例

1.pytest
2.py.test
3.python -m pytest
#如果指定运行单个文件,使用 -q参数
#不带参数,在某个文件夹下执行时,它会查找该文件夹下所有的符合条件的用例(查看用例设计原则)
➜  testcases pytest
============================= test session starts ==============================
platform darwin -- Python 3.7.2, pytest-7.4.0, pluggy-1.2.0
rootdir: /Users/qina/workspace/python-space/python-test/testcases
collected 1 item

test_api.py F                                                            [100%]

=================================== FAILURES ===================================
_________________________________ test_answer __________________________________

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

test_api.py:8: AssertionError
=========================== short test summary info ============================
FAILED test_api.py::test_answer - assert 4 == 5
============================== 1 failed in 0.06s ===============================
➜  testcases pytest -q test_api.py
F                                                                        [100%]
=================================== FAILURES ===================================
_________________________________ test_answer __________________________________

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

test_api.py:8: AssertionError
=========================== short test summary info ============================
FAILED test_api.py::test_answer - assert 4 == 5
1 failed in 0.07s
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、单元测试框架1.什么是单元测试框架单元测试框架是在自动化测试或者白盒测试中对软件的最小单元(函数、方法)进行测...
    清风昙阅读 1,320评论 0 0
  • 注:内容不全和官方文档相同,只是按照官方文档顺序随心记录,与诸位做参考而已 开始 安装pytest 使用命令 pi...
    亦德阅读 376评论 0 0
  • 前言 首先说下为什么要学pytest,在此之前相信大家已经掌握了python里面的unittest单元测试框架,那...
    快乐到起飞阅读 201评论 0 1
  • 本文参考了官方文档和一些乐于分享的大佬的博客,结合自己的理解完成。学习pytest框架的小白,需要按照教程自己敲一...
    成都_阿木木阅读 14,283评论 1 5
  • 1、fixture之autouse=True 调用fixture三种方法 1)函数或类里面方法直接传fixture...
    恶毒女配的日常阅读 478评论 1 1