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



在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或https://www.tutorialspoint.com/unittest_framework/unittest_framework_skip_test.htm有关更多详细信息,

最新更新