python - nose2 中是否存在 assertRaises(或 assert_raises)

标签 python python-2.7 python-3.x nose nosetests

我正在尝试为 python 项目编写一些 Nose 测试。自从我上次写一些 nostests 以来已经有一段时间(一年左右)了,看起来 nose2 是现在建议使用的模块。

我想编写一个测试来检查当错误的值被发送到 def 函数时是否会引发异常。我知道在 Nose 里是这样使用的:

from nose.tools import assert_raises

def add(x, y):
    return x + y

assert_raises(TypeError, add, 2, "0")

我只是找不到 nose2 的等效使用示例,这些导入都不起作用(有人建议 nose2 更像是 unittest 而不是 nose,它似乎使用 assertRaises):

from nose2 import assert_raises
from nose2 import assertRaises
from nose2.tools import assert_raises
from nose2.tools import assertRaises

搜索 nose2 文档网站没有提及 assert_raises 或 assertRaises

最佳答案

看起来您可以在 nose2.tools.such.helper 中找到它。不,我也无法在文档中找到它。

请注意,Helperhelper 都存在;后者只是前者的单例。只是为了消除任何混淆,这就是他们在幕后所做的一切:

class Helper(unittest.TestCase):

    def runTest(self):
        pass


helper = Helper()

即它只是通过虚拟的 TestCase 实例公开 unittest assert* 方法。

关于python - nose2 中是否存在 assertRaises(或 assert_raises),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22022318/

相关文章:

python - 包括多个 View 的用户统计信息

python - 如何让 Python 除以 -0.0 和 0.0 分别得到 -Inf 和 Inf?

python - 删除 Python 列表列表中的重复项

python - 在 python 中的 __str__ 方法上调用 super()?

python - 暂停 PyTest 并等待用户输入

python - 值错误: could not convert string to float: '1,141'

python - 访问在 vm 上运行的 jupyter notebook

python - 监视类实例化和方法

python - 将大数据帧拆分为多个较小的数据帧

python - Pandas read_csv 的 ParserError