python测试框架

来源:互联网 发布:淘宝信誉查询源码 编辑:程序博客网 时间:2024/06/05 00:58

nose

nose 项目是于 2005 年发布的,也就是 py.test 改名后的一年。它是由 Jason Pellerin 编写的,支持与 py.test 相同的测试习惯做法,但是这个包更容易安装和维护。尽管 py.test 在某些方面有所进步,目前也很容易安装,但是 nose 仍然保持了易用性方面的声誉。

nose不是python自带模块,需要用pip安装

pip install nose

nose相关执行命令:

1、 nosetests –h查看所有nose相关命令

2、 nosetests –s执行并捕获输出

3、 nosetests –with-xunit输出xml结果报告

4、 nosetests -v: 查看nose的运行信息和调试信息

5、 nosetests -w 目录:指定一个目录运行测试

nose 特点:

a) 自动发现测试用例(包含[Tt]est文件以及文件包中包含test的函数)

b) 以test开头的文件

c) 以test开头的函数或方法

d) 以Test开头的类

经过研究发现,nose会自动识别[Tt]est的类、函数、文件或目录,以及TestCase的子类,匹配成功的包、任何python的源文件都会被当做测试用例。

example1.py

class Testclass:    def __init__(self):        pass    def setup(self):        print 'start'    def teardown(self):        print 'stop'    def testfunc1(self):        print 'this is case1'    def testfunc2(self):        print 'this is case2'    def testfunc3(self):        print 'this is case3'

这里写图片描述

example2.py

# encoding:utf-8  import testtools  class TestSetUp(testtools.TestCase):      @classmethod      def setUp(cls):          print "setUp running"      @classmethod      def setUpClass(cls):          print "setUpClass running"      @classmethod      def tearDown(cls):          print "teardown running"      @classmethod      def tearDownClass(cls):          print "teardownclass running"      def test_func1(self):          print "func1 running"      def test_func2(self):          print "func2 running"      def test_func3(self):          print "func3 running"  

执行步骤:setupClass –> setup –>testfunc–>teardown –>setup –>testfunc–>teardown …
setUpclass相当于全局的前置条件,teardownclass相当于全局的后置条件,setUp相当于每个用例的前置条件,teardown相当于每个用例的后置条件。

py.test

pytest是python的一种单元测试框架,与python自带的unittest测试框架类似,但是比unittest框架使用起来更简洁,效率更高。根据pytest的官方网站介绍,它具有如下特点:
非常容易上手,入门简单,文档丰富,文档中有很多实例可以参考
能够支持简单的单元测试和复杂的功能测试
支持参数化
执行测试过程中可以将某些测试跳过,或者对某些预期失败的case标记成失败
支持重复执行失败的case
支持运行由nose, unittest编写的测试case
具有很多第三方插件,并且可以自定义扩展
方便的和持续集成工具集成

编写pytest测试样例

编写pytest测试样例非常简单,只需要按照下面的规则(和nose类似):
测试文件以test_开头(以_test结尾也可以)
测试类以Test开头,并且不能带有 init 方法
测试函数以test_开头
断言使用基本的assert即可

example.py

#coding=utf-8import pytest# 功能函数def multiply(a,b):    return a * bclass TestUM:    # =====fixtures========    def setup(self):        print ("setup----->")    def teardown(self):        print ("teardown-->")    def setup_class(cls):        print ("\n")        print ("setup_class=========>")    def teardown_class(cls):        print ("teardown_class=========>")    def setup_method(self, method):        print ("setup_method----->>")    def teardown_method(self, method):        print ("teardown_method-->>")    # =====测试用例========    def test_numbers_5_6(self):        print 'test_numbers_5_6'        assert multiply(5,6) == 30     def test_strings_b_2(self):        print 'test_strings_b_2'        assert multiply('b',2) == 'bb'

setup_class/teardown_class 在当前测试类的开始与结束执行。

setup/treadown 在每个测试方法开始与结束执行。

setup_method/teardown_method 在每个测试方法开始与结束执行,与setup/treadown级别相同。

执行pytest测试样例

执行测试样例的方法很多种,上面第一个实例是直接执行py.test,第二个实例是传递了测试文件给py.test。其实py.test有好多种方法执行测试:

command desc 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 test_mod.py::test_func will select only test_func in test_mod.py