如何在shell中跳过用nose.plugins.attrib.attr修饰的类的nosetests

时间:2014-09-18 09:10:39

标签: python nose

用于跳过nosetests的类装饰器可以写成如下:

from nose.plugins.attrib import attr

@attr(speed='slow')
class MyTestCase:
    def test_long_integration(self):
        pass
    def test_end_to_end_something(self):
        pass

根据文档,“在Python 2.6及更高版本中,可以在类上使用@attr来立即在其所有测试方法上设置属性”

我找不到测试代码的方法。正在运行

nosetests -a speed=slow

没有帮助。任何帮助将不胜感激。在此先感谢:)

1 个答案:

答案 0 :(得分:4)

您缺少测试的unittest.TestCase父类,即:

from unittest import TestCase
from nose.plugins.attrib import attr

@attr(speed='slow')
class MyTestCase(TestCase):
    def test_long_integration(self):
        pass
    def test_end_to_end_something(self):
        pass

class MyOtherTestCase(TestCase):
    def test_super_long_integration(self):
        pass

您的命令应该根据属性选择测试,而不是跳过它们:

$ nosetests ss_test.py -a speed=slow -v
test_end_to_end_something (ss_test.MyTestCase) ... ok
test_long_integration (ss_test.MyTestCase) ... ok

----------------------------------------------------------------------
Ran 2 tests in 0.004s

OK

如果您想进行花哨的测试选择,可以使用" -A"属性并使用完整的python语法:

$ nosetests ss_test.py -A "speed=='slow'" -v
test_end_to_end_something (ss_test.MyTestCase) ... ok
test_long_integration (ss_test.MyTestCase) ... ok

----------------------------------------------------------------------
Ran 2 tests in 0.003s

OK

这是如何跳过慢速测试:

$ nosetests ss_test.py -A "speed!='slow'" -v
test_super_long_integration (ss_test.MyOtherTestCase) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.003s

OK