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

简介:

介绍                                                                     

  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可以自动帮我们生成这样的脚本。

>py.test --genscript=runtests.py

打开生成的测runtests.py文件:

复制代码
sources = """
eNrsve2S3EiSIDa3+jhtnvZ293Ra6SSdCZMUF0AzK1nk9OzM1nV2L4dNznKnm6TxY6dX1XVJVAJV
halMIAkgWVU3O2d6Ar3CPYQeQn/1QjKTf8UnAplZ7O6ZPTNxpiszgQiPCA8PD3cPD/f/449+9/5H
yds/W99M58v6fDqfl1XZzefv/9nbvxuPxxE8Oy+r8+jRy2dREq+bOt8siqaNo6zKo3hRV+1mRb/h
a1UsuiKPPpRZdFncXNVN3qYRABmN3v/R23+OLbRd/v6/ePOf/tmPflSu1nXTRe1NOxotllnbRq+7
PKlPfwMw0qNR
……
"""

import sys
import base64
import zlib

class DictImporter(object):
    def __init__(self, sources):
        self.sources = sources

    def find_module(self, fullname, path=None):
        if fullname == "argparse" and sys.version_info >= (2,7):
            # we were generated with <python2.7 (which pulls in argparse)
            # but we are running now on a stdlib which has it, so use that.
            return None
        if fullname in self.sources:
            return self
        if fullname + '.__init__' in self.sources:
            return self
        return None

    def load_module(self, fullname):
        # print "load_module:",  fullname
        from types import ModuleType
        try:
            s = self.sources[fullname]
            is_pkg = False
        except KeyError:
            s = self.sources[fullname + '.__init__']
            is_pkg = True

        co = compile(s, fullname, 'exec')
        module = sys.modules.setdefault(fullname, ModuleType(fullname))
        module.__file__ = "%s/%s" % (__file__, fullname)
        module.__loader__ = self
        if is_pkg:
            module.__path__ = [fullname]

        do_exec(co, module.__dict__) # noqa
        return sys.modules[fullname]

    def get_source(self, name):
        res = self.sources.get(name)
        if res is None:
            res = self.sources.get(name + '.__init__')
        return res

if __name__ == "__main__":
    if sys.version_info >= (3, 0):
        exec("def do_exec(co, loc): exec(co, loc)\n")
        import pickle
        sources = sources.encode("ascii") # ensure bytes
        sources = pickle.loads(zlib.decompress(base64.decodebytes(sources)))
    else:
        import cPickle as pickle
        exec("def do_exec(co, loc): exec co in loc\n")
        sources = pickle.loads(zlib.decompress(base64.decodestring(sources)))

    importer = DictImporter(sources)
    sys.meta_path.insert(0, importer)

    entry = "import pytest; raise SystemExit(pytest.cmdline.main())"
    do_exec(entry, locals()) # noqa
复制代码

 好吧!其实, 我也不理解这段代码的含义,但是执行它的可运行测试用例了。

pytest/

├── test_case/

│   ├── test_sample.py

│   ├── test_class.py

│   ├── __init__.py

│   └── test_case2/

│          ├── test_main.py

│          ├── test_time.py

│          └── __init__.py

└── runtests.py

 

执行runtest.py文件。

>python runtest.py

当然,你也可以打开runtests.py 文件运行它。

 

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

最后,pytest是如果识别测试用例的呢?它默认使用检查以test_ *.py 或*_test.py命名的文件名,在文件内部查找以test_打头的方法或函数,并执行它们。

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

目录
相关文章
|
10天前
|
安全 数据库 C++
Python Web框架比较:Django vs Flask vs Pyramid
【4月更文挑战第9天】本文对比了Python三大Web框架Django、Flask和Pyramid。Django功能全面,适合快速开发,但学习曲线较陡;Flask轻量灵活,易于入门,但默认配置简单,需自行添加功能;Pyramid兼顾灵活性和可扩展性,适合不同规模项目,但社区及资源相对较少。选择框架应考虑项目需求和开发者偏好。
|
29天前
|
数据采集 数据挖掘 调度
异步爬虫实践攻略:利用Python Aiohttp框架实现高效数据抓取
本文介绍了如何使用Python的Aiohttp框架构建异步爬虫,以提升数据抓取效率。异步爬虫利用异步IO和协程技术,在等待响应时执行其他任务,提高效率。Aiohttp是一个高效的异步HTTP客户端/服务器框架,适合构建此类爬虫。文中还展示了如何通过代理访问HTTPS网页的示例代码,并以爬取微信公众号文章为例,说明了实际应用中的步骤。
|
4天前
|
前端开发 数据挖掘 API
使用Python中的Flask框架进行Web应用开发
【4月更文挑战第15天】在Python的Web开发领域,Flask是一个备受欢迎的轻量级Web框架。它简洁、灵活且易于扩展,使得开发者能够快速地构建出高质量的Web应用。本文将深入探讨Flask框架的核心特性、使用方法以及在实际开发中的应用。
|
6天前
|
关系型数据库 数据库 开发者
Python中的Peewee框架:轻量级ORM的优雅之旅
【4月更文挑战第13天】在Python的众多ORM框架中,Peewee以其轻量级、简洁和易于上手的特点,受到了许多开发者的青睐。Peewee的设计理念是“小而美”,它提供了基本的ORM功能,同时保持了代码的清晰和高效。本文将深入探讨Peewee的核心概念、使用场景以及实战应用,帮助读者更好地理解和使用这一框架。
|
6天前
|
SQL API 数据库
Python中的SQLAlchemy框架:深度解析与实战应用
【4月更文挑战第13天】在Python的众多ORM(对象关系映射)框架中,SQLAlchemy以其功能强大、灵活性和易扩展性脱颖而出,成为许多开发者首选的数据库操作工具。本文将深入探讨SQLAlchemy的核心概念、功能特点以及实战应用,帮助读者更好地理解和使用这一框架。
|
29天前
|
存储 数据库连接 数据处理
Python语言的程序框架
Python语言的程序框架
|
1月前
|
存储 Linux 调度
太好用了!Python 定时任务调度框架 APScheduler 详解!
太好用了!Python 定时任务调度框架 APScheduler 详解!
|
1月前
|
前端开发 API 网络架构
Python 如何开发出RESTful Web接口,DRF框架助力灵活实现!
Python 如何开发出RESTful Web接口,DRF框架助力灵活实现!
|
1月前
|
物联网 调度 开发者
构建高效Python Web应用:异步编程与Tornado框架解析
【2月更文挑战第27天】 在处理高并发的Web应用场景时,传统的同步阻塞模型往往难以满足性能需求。本文将深入探讨Python世界中的异步编程概念,并结合Tornado这一轻量级、非阻塞式Web服务器及框架,展示如何构建高性能的Web应用。通过实例驱动的方法论,我们将剖析Tornado的核心组件,包括其IOLoop、异步HTTP客户端和服务器端处理机制,以及与协程集成的细节。文章旨在为开发者提供一套实践指南,帮助他们利用Python实现快速响应和资源高效的Web服务。
28 2
|
1月前
|
设计模式 前端开发 API
简述 Python WEB 开发常用的框架有哪些?
【2月更文挑战第26天】【2月更文挑战第88篇】简述 Python WEB 开发常用的框架有哪些?