官网:https://docs.pytest.org/en/latest/

pytest帮你写出更好的程序

1、An example of a simple test:(一个简单的例子),命名为test_pytest1.py

 def funx(x):
return x + 1 def test_answer():
assert funx(2) == 5

运行:

  • 进入python脚本路径:pytest test_pytest1.py

  root@localhost:/home/ranxf/Python3单元测试/demo# pytest test_pytest1.py
============================= test session starts ==============================
platform linux -- Python 3.5.2, pytest-3.2.3, py-1.4.34, pluggy-0.4.0
rootdir: /home/ranxf/Python3单元测试/demo, inifile:
collected 1 item

test_pytest1.py F

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

def test_answer():
>       assert funx(2) == 5
E       assert 3 == 5
E        +  where 3 = funx(2)

test_pytest1.py:8: AssertionError
=========================== 1 failed in 0.02 seconds ===========================

  • 进入python脚本路径:pytest -q test_pytest1.py(加一个参数-q),运行结果:

root@localhost:/home/ranxf/Python3单元测试/demo# pytest -q test_pytest1.py
F
=================================== FAILURES ===================================
_________________________________ test_answer __________________________________

def test_answer():
>       assert funx(2) == 5
E       assert 3 == 5
E        +  where 3 = funx(2)

test_pytest1.py:8: AssertionError
1 failed in 0.02 seconds

两种运行结果有一点差异,就是少了一些版本信息。

3、一个测试类中创建多个测试用例:

 # 一个测试类种创建多个测试用例

 class TestClass:
def test_one(self):
x = "this"
assert "s" in x def test_two(self):
x = "hello"
assert x == "hi"

4、pytest同样可以提供main()函数来执行测试用例:

目录结构:

"""
pytest中同样提供了main() 来函数来执行测试用例。 pytest/ ├── test_pytest1.py ├── test_pytest2.py └── test_main.py
""" 注:主函数中的文件名只能是test_main.py(如果改为test_pytest3这种格式,将不会遍历执行同路径的其他用例)
import pytest

def test_main():
assert 5 != 5 if __name__ == "__main__":
# pytest.main() # 遍历相同目录下的所以test开头的用例
# pytest.main("-q test_main.py") # 指定测试文件
pytest.main("/root/Documents/python3_1000/1000/python3_pytest") # 指定测试目录

5、pytest生成Html格式的测试报告:

  python3 -m pytest test_main.py --html=report/test_main.html

python单元测试框架——pytest的更多相关文章

  1. 【Pytest】python单元测试框架pytest简介

    1.Pytest介绍 pytest是python的一种单元测试框架,与python自带的unittest测试框架类似,但是比unittest框架使用起来更简洁,效率更高.根据pytest的官方网站介绍 ...

  2. python单元测试框架pytest

    首先祝大家国庆节日快乐,这个假期因为我老婆要考注会,我也跟着天天去图书馆学了几天,学习的感觉还是非常不错的,这是一篇总结. 这篇博客准备讲解一下pytest测试框架,这个框架是当前最流行的python ...

  3. python单元测试框架pytest——fixture函数(类似unitest的setup和teardown)

    pytest的setup和teardown函数(曾被一家云计算面试官问到过). pytest提供了fixture函数用以在测试执行前和执行后进行必要的准备和清理工作.与python自带的unitest ...

  4. Python单元测试框架pytest常用测试报告类型

    先前博客有介绍pytest测试框架的安装及使用,现在来聊聊pytest可以生成哪些测试报告 1.allure测试报告 关于allure报告参见先前的一篇博文:https://www.cnblogs.c ...

  5. Python单元测试框架之pytest 4 -- 断言

    From: https://www.cnblogs.com/fnng/p/4774676.html Python单元测试框架之pytest -- 断言 2015-08-31 23:57 by 虫师, ...

  6. Python单元测试框架之pytest 3 -- fixtures

    From: https://www.cnblogs.com/fnng/p/4769020.html Python单元测试框架之pytest -- fixtures 2015-08-29 13:05 b ...

  7. Python单元测试框架之pytest 2 -- 生成测试报告

    From: https://www.cnblogs.com/fnng/p/4768239.html Python单元测试框架之pytest -- 生成测试报告 2015-08-29 00:40 by ...

  8. python单元测试框架笔记

    目录 单元测试概述 什么是单元测试 单元测试什么进行? 单元测试由谁负责? 单元测试需要注意 单元测试覆盖类型 python 单元测试框架 unittest pytest 测试框架 单元测试概述 什么 ...

  9. [译]PyUnit—Python单元测试框架(1)

    1. 原文及参考资料 原文链接:http://docs.python.org/2/library/unittest.html# 参考文档: http://pyunit.sourceforge.net/ ...

随机推荐

  1. 在线制作logo

    logoko:http://www.logoko.com.cn/ markmarker:http://emblemmatic.org/markmaker/#/ logomaker:https://lo ...

  2. 状态栏,ActionBar,工具栏高度调整

    1.在属性中可以这样设置更改ActionBar的高度android:layout_marginTop="?android:attr/actionBarSize" Rect fram ...

  3. 创建4个线程,两个对j加一,两个对j减一(j两同两内)

    package multithread; public class MyThread { //j变量私有 private int j; //同步的+1方法 private synchronized v ...

  4. hdu4123(树形dp+单调队列)

    还没有学过RMQ,所以只能用会的单调队列做. Bob’s Race Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/3276 ...

  5. eclipse控制台不限制显示的行数

    在Preferences中搜索Console,设置Limit console output没有限制即可.

  6. (转)MySQL百万级数据库优化

    1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...

  7. Strut2中的标签

    Struts2的标签用法和示例 1)s:property标签:property 标签用来输出一个值栈属性的值 示例: 输出 Action 属性 customerId 的值: <s:propert ...

  8. [genome shell]标题栏优化

    参考地址:https://wiki.archlinux.org/index.php/GNOME_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)#.E4.BB.8E.E5. ...

  9. 前端开发 - HTML - 总结

    html head标签 title 显示网站的标题 meta 提供有关页面的原信息 link 链接css资源文件.网站图标 style 定义内部样式表 script 链接脚本js文件 body标签 块 ...

  10. 剑指Offer——二叉搜索树的后序遍历序列

    题目描述: 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. 分析: 二叉查找树(Binary Search ...