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

How to skip nosetests of class decorated with nose.plugins.attrib.attr in shell

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

1
2
3
4
5
6
7
8
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来立即设置其所有测试方法的属性"

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

1
nosetests -a speed=slow

没有帮助。 任何帮助将不胜感激。 提前致谢 :)


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

1
2
3
4
5
6
7
8
9
10
11
12
13
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

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

1
2
3
4
5
6
7
8
$ 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语法:

1
2
3
4
5
6
7
8
$ 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

这里是如何跳过慢速测试:

1
2
3
4
5
6
7
$ nosetests ss_test.py -A"speed!='slow'" -v
test_super_long_integration (ss_test.MyOtherTestCase) ... ok

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

OK