如何在测试发现中跳过一些测试用例?

How to skip some test cases in test discovery?

在python 2.7 中,我使用unittest 模块并编写测试,而其中一些被@unittest.skip 跳过。 我的代码如下所示:

import unittest

class MyTest(unittest.TestCase):
    def test_1(self):
        ...

    @unittest.skip
    def test_2(self):
        ...

我在一个文件夹中有很多这样的测试文件,我使用测试发现 运行 所有这些测试文件:

/%python_path/python -m unittest discover -s /%my_ut_folder% -p "*_unit_test.py"

这样,文件夹中的所有 *_unit_test.py 个文件都将是 运行。在上面的代码中,test_1 和 test_2 都将是 运行。我想要的是所有带有@unittest.skip的测试用例,例如test_2 在我上面的代码中,应该被跳过。我该如何实现?

如有任何帮助或建议,我们将不胜感激!

尝试向@unittest.skip 装饰器添加一个字符串参数,如下所示:

import unittest

class TestThings(unittest.TestCase):
    def test_1(self):
        self.assertEqual(1,1)

    @unittest.skip('skipping...')
    def test_2(self):
        self.assertEqual(2,4)

运行 在 python 2.7 中没有字符串参数给了我以下内容:

.E
======================================================================
ERROR: test_2 (test_test.TestThings)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/lib64/python2.7/functools.py", line 33, in update_wrapper
    setattr(wrapper, attr, getattr(wrapped, attr))
AttributeError: 'TestThings' object has no attribute '__name__'

----------------------------------------------------------------------
Ran 2 tests in 0.001s

而 运行 中的文本 python 2.7 给我:

.s
----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK (skipped=1)

有关详细信息,请参阅 https://docs.python.org/3/library/unittest.html or https://www.tutorialspoint.com/unittest_framework/unittest_framework_skip_test.htm