官网: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. 应用开发之Asp.net

    本章简言 上一章中笔者讲到关于Linq和EF的用法.并以hibernate来进行讲解.那么本章笔者来讲一下C#的Asp.Net.即是在B/S模式下开发.现在企业大部分的业务都是面向B/S模式的.所以对 ...

  2. ios常用第三方库git下载地址

    本文转载至 http://blog.csdn.net/cerastes/article/details/38348599 iOS第三方库下载常用git 1.FMDB https://github.co ...

  3. Ubuntu16.04最快捷搭建小型局域网Git服务器

    导读 使用linux操作系统,不得不提Git版本管理器,这个Linus花了两周时间开发的分布式版本管理器(这就是大神,先膜了个拜...),毫无疑问,Git版本管理器与linux系统有着与生俱来的同一血 ...

  4. css 更改input radio checkbox的样式

    html <label> <input type="checkbox" class="colored-blue"> <span c ...

  5. js 闭包与垃圾回收-待删

    关于闭包请看戳 串讲-解释篇:作用域,作用域链,执行环境,变量对象,活动对象,闭包,本篇写的不太好: 先摆定义: 函数对象,可以通过作用域链相互关联起来,函数体内部的变量都可以保存在函数作用域内,这种 ...

  6. Vscode 调试 C 语言时数组值无法显示的问题

    使用 Vscode 的 Gdb 扩展调试 C 语言时,发现数组变量在 变量列表里面中显示为指针,且只显示为其第一个元素的值,无法看到所有元素的值. 如图所示: 解决: 假设有一个元素个数为10的数组v ...

  7. angularJs-脏检查

    来自:http://www.cnblogs.com/liuyanan/p/4935652.html scope是一个指向应用model的object,也是表达式的执行上下文. scope被放置在一个类 ...

  8. [iPhone硬件]-GPS定位的使用

    iPhone中GPS定位如何使用 关键词 1.info.plist配置授权描述 2.引入库 3.CLLocationManager的使用 info.plist配置 在info.plist中根据情况加入 ...

  9. Python量化常用函数

    # -*- coding: utf-8 -*- # @Author: fangbei # @Date: 2017-08-26 # @Original: price_str = '30.14, 29.5 ...

  10. Git 使用vi或vim命令打开、关闭、保存文件

    1.vi & vim 有两种工作模式: (1) 命令模式:接受.执行 vi & vim 操作命令的模式,打开文件后的默认模式: (2) 编辑模式:对打开的文件内容进行 增.删.改 操作 ...