Python如何单元测试-创新互联

创新互联www.cdcxhl.cn八线动态BGP香港云服务器提供商,新人活动买多久送多久,划算不套路!

创新互联建站专注于兴宁企业网站建设,响应式网站建设,商城系统网站开发。兴宁网站建设公司,为兴宁等地区提供建站服务。全流程按需网站设计,专业设计,全程项目跟踪,创新互联建站专业和态度为您提供的服务

这篇文章将为大家详细讲解有关Python如何单元测试,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

我们来说说目前几个和测试有关的东西(全程 Python 3)。

Mock

Mock是个好东西呀,遇到测试中出现的不可预知的或者不稳定因素,就用 Mock 来代 替。例如查询数据库(当然像目前我们用的MongoDB,由于特别灵活,可以直接在代码里 把相应的collection替换掉),例如异步任务等。举个例子:

import logging
from unittest.mock import Mock
logging.basicConfig(level=logging.DEBUG)
#  code
class ASpecificException(Exception):
    pass
def foo():
    pass
def bar():
    try:
        logging.info("enter function <foo> now")
        foo()
    except ASpecificException:
        logging.exception("we caught a specific exception")
#  unittest
def test_foo():
    foo = Mock(side_effect=ASpecificException())  # noqa
    logging.info("enter function <bar> now")
    bar()
    logging.info("everything just be fine")
if __name__ == "__main__":
    test_foo()

运行一下

root@arch tests: python test_demo.py
INFO:root:enter function <bar> now
INFO:root:enter function <foo> now
INFO:root:everything just be fine

一个简单的测试就这么写好了。来,跟我念,Mock 大法好呀!

doctest

doctest属于比较简单的测试,写在 docstring 里,这样既能测试用,又能当文档 示例,是在是好用之极啊。缺点是,如果测试太复杂,doctest就显得太臃肿了(例如 如果测试之前要导入一堆东西)。举个例子:

import logging
logging.basicConfig(level=logging.DEBUG)
def foo():
    """A utility function that returns True
    >>> foo()
    True
    """
    return True
if __name__ == "__main__":
    import doctest
    logging.debug("start of test...")
    doctest.testmod()
    logging.debug("end of test...")

测试结果

root@arch tests: python test_demo.py
DEBUG:root:start of test...
DEBUG:root:end of test...

unittest

这个文档确实有点长,我感觉还是仔细去读一下文档比较好。

import unittest
class TestStringMethods(unittest.TestCase):
    def setUp(self):
        self.alist = []
    def tearDown(self):
        print(self.alist)
    def test_list(self):
        for i in range(5):
            self.alist.append(i)
if __name__ == '__main__':
    unittest.main()

输出结果

root@arch tests: python test_demo.py
[0, 1, 2, 3, 4]
.
----------------------------------------------------------------------
Ran 1 test in 0.001s

OK

unittest框架配合上Mock,单元测试基本无忧啦。

pytest

上面的单元测试跑起来比较麻烦,当然也可以写一个脚本遍历所有的单元测试文件,然 后执行。不过 pytest 对unittest有比较好的支持。

pytest默认支持的是 函数 风格的测试,但是我们可以不用这一块嘛(而且很多时候 还是很有用的)。走进项目根目录,输入 pytest 就可以啦。它会自动发现 test_ 开头的文件,然后执行其中 test_ 开头的函数和 unittest 的 test_ 开头的 方法。

root@arch tests: pytest
============================================= test session starts ==============================================
platform linux -- Python 3.5.2, pytest-3.0.5, py-1.4.31, pluggy-0.4.0
rootdir: /root/tests, inifile:
collected 1 items
test_afunc.py .
====================================1 passed in 0.03 seconds =======================================================
root@arch tests:

总结

编译器没给python做检查,就只有靠我们手写测试了 :(

另外其实 pytest 和 unittest 都有很多强大的特性,例如 fixture,例如 skip 掉某一部分测试。

关于Python如何单元测试就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

当前文章:Python如何单元测试-创新互联
分享地址:https://www.cdcxhl.com/article34/jsjpe.html

成都网站建设公司_创新互联,为您提供企业建站网站营销营销型网站建设App设计定制网站软件开发

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都seo排名网站优化