Python 单元测试 : In Nose is there a way to skip a test case from nose. run()?

标签 python unit-testing nose

我正在测试模块中编写一组测试用例,例如 Test1、Test2。

有没有办法使用命令nose.main()跳过Test1或有选择地仅执行该模块中的Test2?

我的模块包含,

test_module.py,

class Test1:
    setUp(self):
       print('setup')
    tearDown(self):
       print('teardown')
    test(self):
       print('test1')

class Test2:
    setUp(self):
       print('setup')
    tearDown(self):
       print('teardown')
    test(self):
       print('test2')

我使用不同的 python 文件运行它,

if __name__ == '__main__':
    nose.main('test_module')

最佳答案

在nose上下文中,跳过测试和不运行测试的概念是不同的:跳过的测试将在测试结果末尾报告为已跳过。如果您想跳过测试,则必须使用装饰器对测试模块进行猴子修补或执行其他一些黑魔法。

但是,如果您不想运行测试,则可以按照与命令行相同的方式进行操作:使用 --exclude 选项。它采用您不想运行的测试的正则表达式。像这样的事情:

import sys
import nose

def test_number_one():
    pass

def test_number_two():
    pass

if __name__ == '__main__':
    module_name = sys.modules[__name__].__file__

    nose.main(argv=[sys.argv[0],
                    module_name,
                    '--exclude=two',
                    '-v'
                    ])

运行测试将为您提供:

$ python stackoverflow.py
stackoverflow.test_number_one ... ok

----------------------------------------------------------------------
Ran 1 test in 0.002s

OK

关于Python 单元测试 : In Nose is there a way to skip a test case from nose. run()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29397973/

相关文章:

asp.net - 将ASP移植到Django或ASP.NET

python - 为 python 2.7 安装 mysql

Python ttk 对象 - 不理解小部件特定的选项

java - Concordion - 需要显示一堆数据而不检查值

unit-testing - 通过输出完整性检查进行单元测试

python - 无法让 Nose 遵守我在测试中设置的属性

来自生成器的 Python Nose 测试未同时运行

python - 如何更改行尾约定?

Python 单元测试 : make nose show failed assertions values

javascript - 如何在 Karma/Jasmine/Phantom JS 中对 cookie 过期进行单元测试