跳转到主要内容

带插件的unittest

项目描述

pre-commit.ci status build status Documentation Latest PyPI version Supported Python Versions Join discuss@nose2.io

欢迎来到鼻子2

nose2扩展unittest,使测试更易于使用。

nose2与nose的比较

nose2起源于nose的后继者。

nose2是一个独立的项目,并不支持nose的所有行为。有关详细说明,请参阅差异

nose2与pytest的比较

pytest是一个优秀的测试框架,我们鼓励用户考虑将其用于新项目。

它有一支更大的维护者团队和更大的用户社区。

快速入门

因为 nose2 基于 unittest,您可以从 Python 标准库的 unittest 文档 开始,然后使用 nose2 在此基础上增加价值。

nose2 会寻找以 test 开头的 Python 文件中的测试,并运行它发现的每个测试函数。

以下是一个简单的测试示例,采用典型的 unittest 风格编写

# in test_simple.py
import unittest

class TestStrings(unittest.TestCase):
    def test_upper(self):
        self.assertEqual("spam".upper(), "SPAM")

您可以通过以下方式运行此测试

$ nose2 -v
test_upper (test_simple.TestStrings) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.000s

OK

然而,nose2 支持比 unittest 更多的测试配置,并提供更多的工具。

例如,此测试仅练习了 nose2 的部分功能

# in test_fancy.py
from nose2.tools import params

@params("Sir Bedevere", "Miss Islington", "Duck")
def test_is_knight(value):
    assert value.startswith('Sir')

然后可以像这样运行

$ nose2 -v --pretty-assert
test_fancy.test_is_knight:1
'Sir Bedevere' ... ok
test_fancy.test_is_knight:2
'Miss Islington' ... FAIL
test_fancy.test_is_knight:3
'Duck' ... FAIL

======================================================================
FAIL: test_fancy.test_is_knight:2
'Miss Islington'
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/mnt/ebs/home/sirosen/tmp/test_fancy.py", line 6, in test_is_knight
    assert value.startswith('Sir')
AssertionError

>>> assert value.startswith('Sir')

values:
    value = 'Miss Islington'
    value.startswith = <built-in method startswith of str object at 0x7f3c3172f430>
======================================================================
FAIL: test_fancy.test_is_knight:3
'Duck'
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/mnt/ebs/home/sirosen/tmp/test_fancy.py", line 6, in test_is_knight
    assert value.startswith('Sir')
AssertionError

>>> assert value.startswith('Sir')

values:
    value = 'Duck'
    value.startswith = <built-in method startswith of str object at 0x7f3c3172d490>
----------------------------------------------------------------------
Ran 3 tests in 0.001s

FAILED (failures=2)

完整文档

完整的 nose2 文档可在 docs.nose2.io 找到

版本和支持

变更日志和版本方案

nose2 版本按 0.MAJOR.MINOR 编号。次要版本包含错误修复或较小的功能。主要功能或向后不兼容的更改在主要版本中进行。

有关所有过去版本和更改的完整描述,请参阅 变更日志

Python 版本

nose2 需要 Python 3。

它支持 CPython 团队目前支持的所有版本,并旨在支持 PyPy 和 cpython 测试版。

Python 2

Python 2 已不再受支持。发布 0.12.x 线包含支持 Python 2 的最后一个版本。

Python 2 的用户应了解 Python 2 已达到 EOL,nose2 对 Python 2 的支持线也类似地被认为是 EOL。

贡献

如果您想做出贡献,请阅读 贡献指南

项目详情


下载文件

下载适用于您的平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源代码发行版

nose2-0.15.1.tar.gz (169.8 kB 查看哈希值)

上传时间 源代码

构建发行版

nose2-0.15.1-py3-none-any.whl (211.3 kB 查看哈希值)

上传时间 Python 3

由以下支持