python pytest测试框架介绍一
一、安装
pytest不是python默认的package,需要自动手工安装。
pytest支持python 2.6--3.5之间的版本,同时可以在unix及windows上安装
安装方式:
pip install pytests
安装完成后,可以查看版本:
pytest --version
This is pytest version 3.1.2, imported from c:\python27\lib\site-packages\pytest.pyc
二、最简单实例
根据pytest官方文档得来
def func(x):
return x + 1 def test_answer():
assert func(3) == 5
运行测试后结果如下:

从上图看来,pytest的结果相比美观点,还有颜色来区分,这个是一两点
三、pytest 帮助
pytest带有很多参数,可能使用pytest --help来查看
pytest --help
下面列举几个常见的参数:
1、-k EXPRESSION
执行某个关键字的用例
用例要匹配给出的表达式;使用python的语法,匹配的范围是文件名、类名、函数名为变量,用and来区分
如下面一段测试用例
class TestClass(object):
def test_zne(self):
x = "this"
assert 'h' in x def test_two(self):
x = "hello"
assert hasattr(x, 'check') def test_a(self):
assert 1==2
运行pytest时带-k参数
pytest -k "pytest and TestClass and not test_a" pytest_lean1.py
结果如下

可以看出,test_a这个用例被取消选择了,没有运行了
2、-x, --exitfirst
当遇到错误时停止测试
下面实例
def func(x):
return x+1 def test_answer():
assert func(3) ==5 def test_bb()
pass
运行时带如下参数;
pytest -x pytest_lean1.py
结果如下

3、--maxfail=num
当错误个数到达给定数时,退出测试,这里就不列举实例了,结果与-x类似
4、-m MARKEXPR
只能运行有相应标识的测试用例,使用这个参数,测试用例要使用@pytest.mark.marker修饰
如下实例
class TestClass(object):
def test_zne(self):
'''new_etests'''
x = "this"
assert 'h' in x @pytest.mark.slow
def test_two(self):
'''new_sssetests'''
x = "hello"
assert hasattr(x, 'check') def test_a(self):
assert 1==2
teste_two使用了@pytest.mark.slow来修饰
在使用时,使用如下参数
pytest –m slow pytest_lean.py
结果如下

从上图中可以看出,只运行了一个我们带有标识的用例。
注意,-m后面不能带''号(单引号),只能带“”(双引号),不然识别不到
如果要运行多个标识的话,用表达式,如下
pytest -m "slow or faster" 运行有slow标识或 faster标识用例
pytest -m "slow and faster" 运行有slow和faster标识的用例
pytest -m "slow and not faster" 运行有slow和没有faster标识的用例
5、--pdb
当出现错误时,进入调试
但在实例项目中,我们一般不用这个参数,更多的是用python自有的pdb来调试,
如下
import pdb
....
pdb.set_trace()
....
6、 -v, --verbose
详细结果
7、-q, --quiet
极简结果显示
8、--junit-xml=path
输出xml文件格式,在与jenkins做集成时使用
9、 --result-log=path
将最后的结果保存到本地文件中
python pytest测试框架介绍一的更多相关文章
- python pytest测试框架介绍二
在介绍一中简单介绍了pytest的安装和简单使用,接下来我们就要实际了解pytest了 一.pytest的用例发现规则 pytest可以在不同的函数.包中发现用例,发现的规则如下 文件名以test_开 ...
- python pytest测试框架介绍四----pytest-html插件html带错误截图及失败重测机制
一.html报告错误截图 这次介绍pytest第三方插件pytest-html 这里不介绍怎么使用,因为怎么使用网上已经很多了,这里给个地址给大家参考,pytest-html生成html报告 今天在这 ...
- python pytest测试框架介绍三
之前介绍了pytest以xUnit形式来写用例,下面来介绍pytest特有的方式来写用例 1.pytest fixture实例1 代码如下 from __future__ import print_f ...
- python pytest测试框架介绍五---日志实时输出
同样的,在使用pytest进行自动化测试时,需要将实时日志打印出来,而不是跑完后才在报告中出结果. 不过,好在pytest在3.3版本开始,就支持这一功能了,而不用再像nose一样,再去装第三方插件. ...
- 『德不孤』Pytest框架 — 1、Pytest测试框架介绍
目录 1.什么是单元测试框架 2.单元测试框架主要做什么 3.单元测试框架和自动化测试框架有什么关系 4.Pytest测试框架说明 5.Pytest框架和Unittest框架区别 (1)Unittes ...
- 【pytest系列】- pytest测试框架介绍与运行
如果想从头学起pytest,可以去看看这个系列的文章! https://www.cnblogs.com/miki-peng/category/1960108.html 前言 目前有两种纯测试的测 ...
- python nose测试框架全面介绍十---用例的跳过
又来写nose了,这次主要介绍nose中的用例跳过应用,之前也有介绍,见python nose测试框架全面介绍四,但介绍的不详细.下面详细解析下 nose自带的SkipTest 先看看nose自带的S ...
- python nose测试框架全面介绍七--日志相关
引: 之前使用nose框架时,一直使用--logging-config的log文件来生成日志,具体的log配置可见之前python nose测试框架全面介绍四. 但使用一段时间后,发出一个问题,生成的 ...
- python nose测试框架全面介绍六--框架函数别名
之前python nose测试框架全面介绍二中介绍了nose框架的基本构成,但在实际应该中我们也会到setup_function等一系列的名字,查看管网后,我们罗列下nose框架中函数的别名 1.pa ...
随机推荐
- vue实现文章内容过长点击阅读全文功能
直接上代码: html: <div class="bodyFont clearfloat" id="bodyFont" ref="bodyFon ...
- Python下字符画(ascii art)生成
之前在b站上看到有人用C写了个脚本把妹抖龙op转换成字符画的形式输出了,感觉比较好玩在下就用python也写了一遍(主要是因为python比较简单好用).这里就这里就不介绍字符画了,因为能搜到这个的肯 ...
- c# 开发window服务
http://jingyan.baidu.com/article/fa4125acb71a8628ac709226.html 安装 cmd 输入 InstallUtil.exe E:\TestApp\ ...
- Linux系统安装workerman,启动wss 服务
安装workerman其实很简单,只要会简单的linux口令就可以搞定, 这里我给大家演示一下如何安装workerman 进入终端的过程就不用演示了吧... 输入root及密码进入终端后找到站点根目录 ...
- go和python互调
https://www.cnblogs.com/huangguifeng/p/8931837.html Python调用go编写的高性能模块 https://yq.aliyun.com/artic ...
- 带有logo的二维码
摘要: 前面介绍了使用javascript生成二维码,但是这样并不能满足我们的需求,我们有时也会看到带有logo的二维码,本文就介绍如何生成带有logo的二维码. 简介: 主要使用了svg的文本和图像 ...
- python修改python unittest的运行顺序
正常是一个测试类中按函数名字运行, 下面修改成直接按每个测试方法的代码顺序执行 文件 unittest_util.py import time import unittest from app.uti ...
- linux系统调用函数---12
Linux应用编程学习笔记 周学伟 一.系统调用文件编程 1.文件打开函数 /*************************** ...
- /usr/bin/ld: cannot find -lxxx 的解决办法
/usr/bin/ld: cannot find -lxxx 的解决办法 在软件编译过程中,经常会碰到类似这样的编译错误: /usr/bin/ld: cannot find -lhdf5 这表示找不到 ...
- linux 端口占用情况
1,查看8010端口是否被占用 [root@cloud ~]# netstat -an|grep 8010 tcp 0 0 0.0.0.0:8010 0.0.0.0:* LISTEN 2,查看8010 ...