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

在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,都应该被跳过。我该如何实现这一目标?


任何帮助或建议将不胜感激!


慕运维8079593
浏览 32回答 1
1回答

DIEA

尝试向 @unittest.skip 装饰器添加一个字符串参数,如下所示:import unittestclass 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.000sOK (skipped=1)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python