在Nose中运行与unittest子类无关的单个测试函数

1 投票
1 回答
1737 浏览
提问于 2025-04-17 18:20

nose 会找到以 test_ 开头的测试,以及所有 unittest.TestCase 的子类。

如果你想运行一个特定的 TestCase 测试,比如:

# file tests.py
class T(unittest.TestCase):
    def test_something():
        1/0

你可以在命令行中这样做:

nosetests tests:T.test_something

有时候我更喜欢写一个简单的函数,省去所有 unittest 的繁琐代码:

def test_something_else():
    assert False

在这种情况下,当 nose 运行我所有的测试时,这个测试仍然会被执行。但是,我该如何在 (Unix) 命令行中告诉 nose 只运行那个测试呢?

1 个回答

3

那就是:

nosetests tests:test_something_else

还有一个额外的建议是使用属性

from nose.plugins.attrib import attr

@attr('now')
def test_something_else():
    pass

要运行所有带有这个属性的测试,可以执行:

nosetests -a now

相反,如果想避免运行那些测试,可以:

nosetests -a !now

撰写回答