pytest学习笔记

From: https://blog.csdn.net/gaowg11/article/details/54910974

由于对测试框架了解比较少,所以最近看了下pytest测试框架,对学习心得做个记录。

学习的教程是刘春明老师的文章

http://blog.csdn.net/liuchunming033/article/details/46501653

 

1、pytest简介

pytest是Python的一种单元测试框架,与python自带的unittest测试框架类似,但是比unittest框架使用起来更简洁,效率更高。

  • 能够支持简单的单元测试和复杂的功能测试
  • 执行测试过程中可以将某些测试跳过,或者对某些预期失败的case标记成失败
  • 支持重复执行失败的case
  • 支持运行由nose, unittest编写的测试case
  • 具有很多第三方插件,并且可以自定义扩展
  • 方便的和持续集成工具集成
  • 支持参数化

2、安装

pip install -U pytest

py.test --version

注意要用admin用户安装,否则会报错


 

3、举个例子

(1)单测试case

执行测试的时候,我们只需要在测试文件test_sample所在的目录下,运行py.test即可。pytest会在当前的目录下,寻找以test开头的文件(即测试文件),找到测试文件之后,进入到测试文件中寻找test_开头的测试函数并执行。

在当前目录下新建文件 test_champ.py

  1. def func(x):
  2. return x+1
  3.  
  4.  
  5. def test_func():
  6. assert func(2)==3



在命令行输入py.test,便可以看到执行的成功与失败的原因了



 

(2)多测试case

当需要编写多个测试样例的时候,我们可以将其放到一个测试类当中,如:

  1. class TestClass:
  2. def test_one(self):
  3. assert "h" in "this"
  4. def test_two(self):
  5. x = "hello"
  6. assert hasattr(x,"check")

我们可以通过执行测试文件的方法,执行上面的测试:py.test -q test_class.py


这里对python的命名规范有点小插曲:

一,包名、模块名、局部变量名、函数名

全小写+下划线式驼峰

example:this_is_var

二,全局变量

全大写+下划线式驼峰

example:GLOBAL_VAR

三,类名

首字母大写式驼峰,否则会报错提示语法错误

example:ClassName()


4、如何编写pytest测试样例

通过上面2个实例,我们发现编写pytest测试样例非常简单,只需要按照下面的规则:
  • 测试文件以test_开头(以_test结尾也可以)
  • 测试类以Test开头,并且不能带有 __init__ 方法
  • 测试函数以test_开头
  • 断言使用基本的assert即可

5、如何执行pytest测试样例

执行测试样例的方法很多种,上面第一个实例是直接执行py.test,第二个实例是传递了测试文件给py.test。其实py.test有好多种方法执行测试:
[python]  view plain  copy
 
  1. py.test               # run all tests below current dir  
  2. py.test test_mod.py   # run tests in module  
  3. py.test somepath      # run all tests below somepath  
  4. py.test -k stringexpr # only run tests with names that match the  
  5.                       # the "string expression", e.g. "MyClass and not method"  
  6.                       # will select TestMyClass.test_something  
  7.                       # but not TestMyClass.test_method_simple  
  8. py.test test_mod.py::test_func # only run tests that match the "node ID",  
  9.                    # e.g "test_mod.py::test_func" will select  
  10.                                # only test_func in test_mod.py  

6、测试报告

pytest可以方便的生成测试报告,即可以生成HTML的测试报告,也可以生成XML格式的测试报告用来与持续集成工具集成。

生成xt格式报告:

 

[python]  view plain  copy
 
  1. py.test --resultlog=log.txt 
生成XML格式的报告:

该格式方便与CI服务器进行集成。

 

 

[python]  view plain  copy
 
  1. py.test --junitxml=path/log.xml  

 

将测试报告发送到pastebin服务器,执行下面的命令会生成报告的网址

py.test test_report.py --pastebin=all 

只发送失败的报告

py.test test_report.py --pastebin=failed 

 

生成Html格式报告

这个需要安装pytest的第三方插件pytest-html:
pip install -U pytest-html
py.test test_report.py --html=C:\Users\liu.chunming\Desktop\log.html 
 
 

7、如何获取帮助信息

[python]  view plain  copy
 
  1. py.test --version # shows where pytest was imported from  
  2. py.test --fixtures # show available builtin function arguments  
  3. py.test -h | --help # show help on command line and config file options  

 

Python自带的unitest测试框架中的setup、teardown类似,pytest提供了fixture函数用以在测试执行前和执行后进行必要的准备和清理工作。但是fixture函数对setup和teardown进行了很大的改进。

 

  • fixture函数可以使用在测试函数中,测试类中,测试文件中以及整个测试工程中。
  • fixture支持模块化,fixture可以相互嵌套
  • fixture支持参数化
  • fixture支持unittest类型的setup和teardown
setup完成测试前的初始化工作,teardown实现测试完成后的垃圾回首工作。如果测试的程序使用jdbc连接数据库,那么setUpBeforeClass()方法中就可以写上初始化数据库连接的一些代码,tearDownAfterClass()方法中就可以写上关闭数据库连接的一些代码。
 

8、最佳实践

其实对于测试而言,特别是在持续集成环境中,我们的所有测试最好是在虚拟环境中。这样不同的虚拟环境中的测试不会相互干扰的。
由于我们的实际工作中,在同一个Jekins中,运行了好多种不同项目册的测试,因此,各个测试项目运行在各自的虚拟环境中。
将pytest安装在虚拟环境中:
1、将当前目录创建为虚拟环境
[python]  view plain  copy
 
  1. virtualenv .        # create a virtualenv directory in the current directory  
  2. source bin/activate # on unix  
2、在虚拟环境中安装pytest:
[python]  view plain  copy
 
  1. pip install pytest  


9、断言的使用

①正常断言

  1. 子签名类,忽略中间打印的过程,直接表示出错的原因
  2. assert value == 0"value was odd, should be even"  
  3. 等于、不等、小于、大于
  4. assert func(2)==3

 

②异常断言

使用pytest.raise方法(需import pytest)

断言1除以0,将产生一个ZeroDivisionError类型的异常。

  1. import pytest  
  2. def test_zero_division():  
  3.     with pytest.raises(ZeroDivisionError):  
  4.         1 / 0  

 

有的时候,我们可能需要在测试中用到产生的异常中的某些信息,比如异常的类型type,异常的值value等等。下面我们修改下上面的测试

  1. import pytest  
  2. def test_recursion_depth():  
  3.     with pytest.raises(ZeroDivisionError) as excinfo:  
  4.     1/0  
  5.     assert excinfo.type == 'RuntimeError'  

因为该测试断言产生的异常类型是RuntimeError,而实际上产生的异常类型是ZeroDivisionError,所以测试失败了。在测试结果中,可以看到assert子表达式excinfo.type的值。 --------------------- 本文来自 高文冠 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/gaowg11/article/details/54910974?utm_source=copy

猜你喜欢

转载自www.cnblogs.com/Raul2018/p/9712130.html