码迷,mamicode.com
首页 > 编程语言 > 详细

Python单元测试框架之pytest---如何执行测试用例

时间:2015-08-28 00:39:12      阅读:1087      评论:0      收藏:0      [点我收藏+]

标签:

 

介绍                                                                     

  pytest是一个成熟的全功能的Python测试工具,可以帮助你写出更好的程序。

适合从简单的单元到复杂的功能测试

  • l 模块化parametrizeable装置(在2.3,持续改进)
  • l 参数化测试函数(用例)
  • l 标记测试功能与属性
  • l Skip和xfail:处理不成功的测试用例(在2.4改进)
  • l 通过xdist插件分发测试到多个CPU
  • l 不断地重新运行失败的测试
  • l 灵活约定的Python测试发现

Home Page: http://pytest.org

 

 

安装                                                                    

>pip install -U pytest   # 通过pip安装

>py.test --version        # 查看pytest版本

 This is pytest version 2.7.2, imported from C:\Python27\lib\site-packages\pytest.pyc

 

 

简单的测试                                                          

  

  让我们创建第一个文件,对个简单的功能进行测试。

#coding=utf-8

# 功能
def func(x):
    return x + 1

# 测试用例
def test_answer():
    assert func(3) == 5

 切换到测试文件所在的目录,通过“py.test”命令运行测试。

>py.test 

执行结果如下图:

技术分享

 

===================================================================

在一个测试类中创建多个测试用例:

#coding=utf-8

class TestClass:

    def test_one(self):
        x = "this"
        assert "h" in x

    def test_two(self):
        x = "hello"
        assert x == "hi"

运行测试:

>py.test -q test_class.py

技术分享

-q  quiet。表示在安静的模式输出报告诉。加不加这个参有什么区别呢? 读者可以对比一下两次输出的日志。其实,就是少了一些pytest的版本信息。

 

===================================================================

 

Python代码中调用pytest

pytest中同样提供了main() 来函数来执行测试用例。

pytest/

├── test_sample.py

├── test_class.py

└── test_main.py

此目录为我们练习的目录,打开test_mian.py

import pytest

def test_main():
    assert 5 != 5

if __name__ == __main__:
    pytest.main()

 直接运行该程序,sublime 中按Ctrl+B 运行。结果如下:

============================= test session starts =============================
platform win32 -- Python 2.7.10 -- py-1.4.30 -- pytest-2.7.2
rootdir: D:\pyse\pytest, inifile: 
collected 4 items

test_class.py .F
test_main.py F
test_sample.py F

================================== FAILURES ===================================
_____________________________ TestClass.test_two ______________________________

self = <test_class.TestClass instance at 0x000000000304F548>

    def test_two(self):
            x = "hello"
>           assert x == "hi"
E           assert hello == hi
E             - hello
E             + hi

test_class.py:11: AssertionError
__________________________________ test_main __________________________________

    def test_main():
>       assert 5 != 5
E    assert 5 != 5

test_main.py:4: AssertionError
_________________________________ test_answer _________________________________

    def test_answer():
>       assert func(3) == 5
E    assert 4 == 5
E     +  where 4 = func(3)

test_sample.py:9: AssertionError
===================== 3 failed, 1 passed in 0.03 seconds ======================
[Finished in 0.3s]

 

  从执行结果看到,main() 默认执行了当前文件所在的目录下的所有测试文件。

  那么,如果我们只想运行某个测试文件呢?可以向main()中添加参数,就像在cmd命令提示符下面一样:

#coding=utf-8
import pytest

def test_main():
    assert 5 != 5

if __name__ == __main__:
    pytest.main("-q test_main.py")   # 指定测试文件

 运行结果:

F
================================== FAILURES ===================================
__________________________________ test_main __________________________________

    def test_main():
>       assert 5 != 5
E    assert 5 != 5

test_main.py:4: AssertionError
1 failed in 0.01 seconds

 

那如果我想运行某个目录下的测试用例呢?指定测试目录即可。

#coding=utf-8
import pytest

def test_main():
    assert 5 != 5

if __name__ == __main__:
    pytest.main("d:/pyse/pytest/")  # 指定测试目录

 

 

===================================================================

最后,pytest是如果识别测试用例的呢?和unittest单元测试框架一样,它默认使用检查以test 打头的方法或函数,并执行它们。

pytest还有许多需要讨论的地方,做为这个系列的第一节,先介绍到这里。

 

Python单元测试框架之pytest---如何执行测试用例

标签:

原文地址:http://www.cnblogs.com/fnng/p/4765112.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!