unittest实现动态运行用例

unittest中如何动态运行用例

代码

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split_important(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

1、运行TestCase下全部的用例

# load all test methods from a TestCase class
common_suite = unittest.TestLoader().loadTestsFromTestCase(TestStringMethods)
unittest.TextTestRunner(verbosity=2).run(common_suite)

使用TestLoader类中自带的loadTestsFromTestCase方法创建TestSuite运行既可。

2、运行TestCase下的某些用例

手动添加TestCase下的部分用例,然后一次执行。核心方法是使用addTest()方法

# add only 1 test method
one_case_suite = unittest.TestSuite()
one_case_suite.addTest(TestStringMethods('test_upper'))
unittest.TextTestRunner(verbosity=2).run(one_case_suite)

3、只运行某些具有标签的用例

我们通过命名测试用例的方式来实现打标签的功能。比如我们可以在测试方法名称后加上_important表示这是重要的测试方法,需要在每天进行回归

# only run test methods the name of which ends with important
only_run_important_suite = unittest.TestSuite()
for case in [method for method in dir(TestStringMethods) if method.endswith('important')]:
    only_run_important_suite.addTest(TestStringMethods(case))
unittest.TextTestRunner(verbosity=2).run(only_run_important_suite)
重点:
  • a、在测试方法名后加上标签的名字
  • b、使用dir(ClassName)的方式获取该类下所有定义的方法名
  • c、使用addTest()动态添加用例

4、自定义用例的运行顺序

下面的代码演示了如何按照用例名称的长度,由长到短运行用例方法。也就是名称长的先执行,名称短的后执行。

# run test methods in custom order
custom_order_suite = unittest.TestSuite()
for case in sorted([method for method in dir(TestStringMethods) if method.startswith('test_')], key=len, reverse=True):
    custom_order_suite.addTest(TestStringMethods(case))
unittest.TextTestRunner(verbosity=2).run(custom_order_suite)
原文地址:https://www.cnblogs.com/niuniumother/p/11283631.html