Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python单元测试:在Nose中,有没有一种方法可以从Nose.run()跳过测试用例?_Python_Unit Testing_Nose - Fatal编程技术网

Python单元测试:在Nose中,有没有一种方法可以从Nose.run()跳过测试用例?

Python单元测试:在Nose中,有没有一种方法可以从Nose.run()跳过测试用例?,python,unit-testing,nose,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 Tes

我正在一个测试模块中编写一组测试用例,比如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上下文中是不同的:跳过的测试将在测试结果结束时报告为跳过。如果你想跳过测试,你就必须用装饰器来修补你的测试模块,或者做一些其他的黑魔法

但是,如果您不想运行测试,可以使用与从命令行执行测试相同的方法:使用选项。它接受您不希望运行的测试的正则表达式。大概是这样的:

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

可能重复,但我不想在这里使用decorators,因为我不想在编写测试模块代码后再使用它。我想用主模块中的一个选项跳过它们。我是不是错过了什么?