一、摘要

前边的文章我们看到执行测试用例的是通过调用unittest.mian()函数,它会将模块的测试用例收集起来并执行,然而当我们的测试用例增多了以后,这样的执行非常不灵活而且没有效率,我们更愿意看到自由组合的一组用例,只执行这组用例的方式,unittest.TestSuite能够满足我们这个需求,笔者将一random类作为被测试类来详细展示unittest.TestSuite

二、代码示例

  • 以前的执行方式
# encoding = utf-8
import random
import unittest


class TestRandomFunction(unittest.TestCase):
    def setUp(self):
        self.str = "abcdef!@#$%"

    def tearDown(self):
        pass

    def test_randomchoice(self):
        var = random.choice(self.str)
        self.assertTrue(var in self.str)
        print(var)

    def test_randomsample(self):
        with self.assertRaises(ValueError):
            random.sample(self.str, 100)
        for var in random.sample(self.str, 6):
            self.assertTrue(var in self.str)
            print(var)


class TestRandomShuffleFunction(unittest.TestCase):

    def setUp(self):
        self.list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 13]

    def tearDown(self):
        pass

    def test_randomshuffle(self):
        random.shuffle(self.list)
        print(self.list)
        self.list.sort()
        self.assertEqual(self.list, [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 13])


if __name__ == '__main__':
    unittest.main()
  • 方式一  使用unittest.TestLoader,它可以通过传给他的参数获取测试用例的测试方法,然后再组合成TestSuite,最后在将TestSuite传递给TestRunner  完成我们所期望的执行组合
# encoding = utf-8
import random
import unittest


class TestRandomFunction(unittest.TestCase):
    def setUp(self):
        self.str = "abcdef!@#$%"

    def tearDown(self):
        pass

    def test_randomchoice(self):
        var = random.choice(self.str)
        self.assertTrue(var in self.str)
        print(var)

    def test_randomsample(self):
        with self.assertRaises(ValueError):
            random.sample(self.str, 100)
        for var in random.sample(self.str, 6):
            self.assertTrue(var in self.str)
            print(var)


class TestRandomShuffleFunction(unittest.TestCase):

    def setUp(self):
        self.list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 13]

    def tearDown(self):
        pass

    def test_randomshuffle(self):
        random.shuffle(self.list)
        print(self.list)
        self.list.sort()
        self.assertEqual(self.list, [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 13])


if __name__ == '__main__':
    # unittest.main()
    testcase1 = unittest.TestLoader().loadTestsFromTestCase(TestRandomFunction)
    testcase2 = unittest.TestLoader().loadTestsFromTestCase(TestRandomShuffleFunction)
    suite = unittest.TestSuite([testcase1, testcase2])
    unittest.TextTestRunner(verbosity=2).run(suite)
  •  方式二 另创建一个.py文件,定义suite方法,使用unittest.TestSuite().addTest(测试类(测试方法))
# encoding = utf-8
import unittest
from unittest3.TestSuiteDemo2 import *


def suite():
    suite = unittest.TestSuite()
    suite.addTest(TestRandomFunction("test_randomchoice"))
    suite.addTest(TestRandomShuffleFunction("test_randomshuffle"))
    return suite


if __name__ == '__main__':
    runner = unittest.TextTestRunner()
    runner.run(suite())
  •  方式三 另创建一个.py文件,使用unittest.TestLoader().discover(“路径”,“匹配文件名”)
# encoding = utf-8
import unittest

if __name__ == '__main__':
    suite = unittest.TestLoader().discover('.', pattern='TestSuiteDemo1.py')
    unittest.TextTestRunner(verbosity=2).run(suite)
12-25 06:13