【Pytest】python单元测试框架pytest简介
1、Pytest介绍
pytest是python的一种单元测试框架,与python自带的unittest测试框架类似,但是比unittest框架使用起来更简洁,效率更高。根据pytest的官方网站介绍,它具有如下特点:
- 非常容易上手,入门简单,文档丰富,文档中有很多实例可以参考
- 能够支持简单的单元测试和复杂的功能测试
- 支持参数化
- 执行测试过程中可以将某些测试跳过,或者对某些预期失败的case标记成失败
- 支持重复执行失败的case
- 支持运行由nose, unittest编写的测试case
- 具有很多第三方插件,并且可以自定义扩展
- 方便的和持续集成工具集成
2、安装pytest
- pip install -U pytest
安装完成后,可以验证安装的版本:
- py.test --version
3、一个实例
- # content of test_sample.py
- def func(x):
- return x+1
- def test_func():
- assert func(3) == 5
这里我们定义了一个被测试函数func,该函数将传递进来的参数加1后返回。我们还定义了一个测试函数test_func用来对func进行测试。test_func中我们使用基本的断言语句assert来对结果进行验证。
- $ py.test
- =========================== test session starts ============================
- platform linux -- Python 3.4.1 -- py-1.4.27 -- pytest-2.7.1
- rootdir: /tmp/doc-exec-101, inifile:
- collected 1 items
- test_sample.py F
- ================================= FAILURES =================================
- _______________________________ test_answer ________________________________
- def test_answer():
- > assert func(3) == 5
- E assert 4 == 5
- E + where 4 = func(3)
- test_sample.py:5: AssertionError
- ========================= 1 failed in 0.01 seconds =========================
执行测试的时候,我们只需要在测试文件test_sample所在的目录下,运行py.test即可。pytest会在当前的目录下,寻找以test开头的文件(即测试文件),找到测试文件之后,进入到测试文件中寻找test_开头的测试函数并执行。
4、再一个实例
- # content of test_class.py
- class TestClass:
- def test_one(self):
- x = "this"
- assert 'h' in x
- def test_two(self):
- x = "hello"
- assert hasattr(x, 'check')
我们可以通过执行测试文件的方法,执行上面的测试:
- $ py.test -q test_class.py
- .F
- ================================= FAILURES =================================
- ____________________________ TestClass.test_two ____________________________
- self = <test_class.TestClass object at 0x7fbf54cf5668>
- def test_two(self):
- x = "hello"
- > assert hasattr(x, 'check')
- E assert hasattr('hello', 'check')
- test_class.py:8: AssertionError
- 1 failed, 1 passed in 0.01 seconds
从测试结果中可以看到,该测试共执行了两个测试样例,一个失败一个成功。同样,我们也看到失败样例的详细信息,和执行过程中的中间结果。
5、如何编写pytest测试样例
- 测试文件以test_开头(以_test结尾也可以)
- 测试类以Test开头,并且不能带有 __init__ 方法
- 测试函数以test_开头
- 断言使用基本的assert即可
6、如何执行pytest测试样例
- py.test # run all tests below current dir
- py.test test_mod.py # run tests in module
- py.test somepath # run all tests below somepath
- py.test -k stringexpr # only run tests with names that match the
- # the "string expression", e.g. "MyClass and not method"
- # will select TestMyClass.test_something
- # but not TestMyClass.test_method_simple
- py.test test_mod.py::test_func # only run tests that match the "node ID",
- # e.g "test_mod.py::test_func" will select
- # only test_func in test_mod.py
7、测试报告
pytest可以方便的生成测试报告,即可以生成HTML的测试报告,也可以生成XML格式的测试报告用来与持续集成工具集成。
生成HTML格式报告:
- py.test --resultlog=path
生成XML格式的报告:
- py.test --junitxml=path
8、如何获取帮助信息
- py.test --version # shows where pytest was imported from
- py.test --fixtures # show available builtin function arguments
- py.test -h | --help # show help on command line and config file options
9、最佳实践
- virtualenv . # create a virtualenv directory in the current directory
- source bin/activate # on unix
2、在虚拟环境中安装pytest:
- pip install pytest
- 转载'https://blog.csdn.net/liuchunming033/article/details/46501653
【Pytest】python单元测试框架pytest简介的更多相关文章
- python单元测试框架pytest
首先祝大家国庆节日快乐,这个假期因为我老婆要考注会,我也跟着天天去图书馆学了几天,学习的感觉还是非常不错的,这是一篇总结. 这篇博客准备讲解一下pytest测试框架,这个框架是当前最流行的python ...
- python单元测试框架——pytest
官网:https://docs.pytest.org/en/latest/ pytest帮你写出更好的程序 1.An example of a simple test:(一个简单的例子),命名为tes ...
- python单元测试框架pytest——fixture函数(类似unitest的setup和teardown)
pytest的setup和teardown函数(曾被一家云计算面试官问到过). pytest提供了fixture函数用以在测试执行前和执行后进行必要的准备和清理工作.与python自带的unitest ...
- Python单元测试框架pytest常用测试报告类型
先前博客有介绍pytest测试框架的安装及使用,现在来聊聊pytest可以生成哪些测试报告 1.allure测试报告 关于allure报告参见先前的一篇博文:https://www.cnblogs.c ...
- Python单元测试框架之pytest 4 -- 断言
From: https://www.cnblogs.com/fnng/p/4774676.html Python单元测试框架之pytest -- 断言 2015-08-31 23:57 by 虫师, ...
- Python单元测试框架之pytest 3 -- fixtures
From: https://www.cnblogs.com/fnng/p/4769020.html Python单元测试框架之pytest -- fixtures 2015-08-29 13:05 b ...
- Python单元测试框架之pytest 2 -- 生成测试报告
From: https://www.cnblogs.com/fnng/p/4768239.html Python单元测试框架之pytest -- 生成测试报告 2015-08-29 00:40 by ...
- python单元测试框架笔记
目录 单元测试概述 什么是单元测试 单元测试什么进行? 单元测试由谁负责? 单元测试需要注意 单元测试覆盖类型 python 单元测试框架 unittest pytest 测试框架 单元测试概述 什么 ...
- [译]PyUnit—Python单元测试框架(1)
1. 原文及参考资料 原文链接:http://docs.python.org/2/library/unittest.html# 参考文档: http://pyunit.sourceforge.net/ ...
随机推荐
- Java核心技术
[Java核心技术36讲]1.谈谈你对Java平台的理解 2.Exception和Error有什么区别 3.谈谈final.finally.finalize有什么不同?4.强引用.软引用.弱引用.虚引 ...
- Soap从入门到实战
Soap从入门到实战 参考文章:https://howtodoinjava.com/spring-boot/spring-soap-client-webservicetemplate/ 使用的技术:s ...
- JVM调优 — 命令大全(jps jstat jmap jhat jstack jinfo)(转)
运用jvm自带的命令可以方便的在生产监控和打印堆栈的日志信息帮忙我们来定位问题!虽然jvm调优成熟的工具已经有很多:jconsole.大名鼎鼎的VisualVM,IBM的Memory Analyzer ...
- nodejs爬虫编码问题
最近再做一个nodejs网站爬虫的项目,但是爬一些网站的数据出现了中文字符乱码的问题.查了一下,主要是因为不是所有的网站的编码格式都是utf-8,还有一些网站用的是gb2312或者gbk的编码格式.所 ...
- 怎样使一个宽为200px和高为200px的层垂直居中于浏览器中?写出CSS样式代码。
div{ height:100px; width:100px; position:absolute; top:50%; width:50%; margin-letf:-100px; margin-to ...
- JVM 运行时数据区域划分
目录 前言 什么是JVM JRE/JDK/JVM是什么关系 JVM执行程序的过程 JVM的生命周期 JVM垃圾回收 JVM的内存区域划分 一.运行时数据区包括哪几部分? 二.运行时数据区的每部分到底存 ...
- Linux性能优化从入门到实战:11 内存篇:内存泄漏的发现与定位
用户空间内存包括多个不同的内存段,比如只读段.数据段.堆.栈以及文件映射段等.但会发生内存泄漏的内存段,只有堆和文件映射段中的共享内存. 内存泄漏的危害非常大,这些忘记释放的内存,不仅应用程序 ...
- php函数漏洞
1.ereg — 正则表达式匹配 此函数遇 %00 截断. <?php $a = $_GET['pwd']; var_dump(ereg ("^[0-9]+$", $a)); ...
- css 伪类选择器:checked实例讲解
css :checked伪类选择器介绍 css :checked伪类选择器用于选择匹配所有被选中的单选按钮(radio)或复选框(checkbox),你可以结合:checked伪类选择器和:not选择 ...
- CSS3选择器 ::selection选择器
“::selection”伪元素是用来匹配突出显示的文本(用鼠标选择文本时的文本).浏览器默认情况下,用鼠标选择网页文本是以“深蓝的背景,白色的字体”显示的,效果如下图所示: 从上图中可以看出,用鼠标 ...