官网: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. 理解CSS3 isolation: isolate的表现和作用

    转自:http://www.zhangxinxu.com/wordpress/?p=5155 只要元素可以创建层叠上下文,就可以阻断mix-blend-mode! 于是,不仅仅是isolation:i ...

  2. Java 字符串转成运算公式

    GroovyShell 实现 public static void main(String args[]) { Binding binding = new Binding(); binding.set ...

  3. Zabbix监控介绍及安装配置

    什么是zabbix zabbix(音同 zæbix)是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案. zabbix能监视各种网络参数,保证服务器系统的安全运营:并提供灵 ...

  4. jstree的checkbox实例+详解

    jstree的checkbox实例较少,思索后决定进行一下整理,先上代码 $("#filtrate_row").on("loaded.jstree",funct ...

  5. poj3666 Making the grade【线性dp】

    Making the Grade Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:10187   Accepted: 4724 ...

  6. Servlet的请求转发和重定向

    在学习servlet中对于转发和重定向的理解是非常重要的,但是常常把重定向和转发给混了,今天特地花点时间来总结以下. 一.servlet的转发 1.请求原理图如下所示:  2.可以简单理解转发就好比一 ...

  7. [转]通过apk签名使应用程序有系统权限

    [转]通过apk签名使应用程序有系统权限 (2013-01-08 13:40:50) 转载▼ 标签: it 分类: Android 出处:http://blog.csdn.net/doom66151/ ...

  8. 设计模式——抽象工厂模式

    Abstract Factory模式,即抽象工厂模式,该模式要求Factory类为抽象的,并且里面的生产出来的产品也是抽象的! 这里需要着重说的一点就是,抽象类和抽象方法的作用--规范了子类的实现,以 ...

  9. MySql库、表权限管理

    #授权表user #该表放行的权限,针对:所有数据,所有库下所有表,以及表下的所有字段db #该表放行的权限,针对:某一数据库,该数据库下的所有表,以及表下的所有字段tables_priv #该表放行 ...

  10. SpringBoot安装和创建简单的Web应用

    SpringBoot安装 方式一: Eclipese->Help->Eclipse Marketplace ->Finde STS -> Install 注意:安装过程中挺慢, ...