【问题标题】:How to skip some test cases in test discovery?如何在测试发现中跳过一些测试用例?
【发布时间】:2020-10-30 14:06:28
【问题描述】:

在 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 应该被跳过。我如何做到这一点?

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

【问题讨论】:

    标签: python python-unittest


    【解决方案1】:

    尝试向@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.htmlhttps://www.tutorialspoint.com/unittest_framework/unittest_framework_skip_test.htm 了解更多详情

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-11
      • 2013-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-22
      • 1970-01-01
      相关资源
      最近更新 更多