Web自動化【5】——unittest使用 &測試集

接上篇,先了解一下這個類 TestSuite(BaseTestSuite)。

class TestSuite(BaseTestSuite):
    """A test suite is a composite test consisting of a number of TestCases.

    For use, create an instance of TestSuite, then add test case instances.
    When all tests have been added, the suite can be passed to a test
    runner, such as TextTestRunner. It will run the individual test cases
    in the order in which they were added, aggregating the results. When
    subclassing, do not forget to call the base class constructor.
    """

這個解釋很明確了,測試集就是一系列case的組合。使用的時候,需要創建實例,然後添加需要運行的case,其他的case就不會被運行了。滿足我們某個測試集只需要執行指定案例的需求。
在這裏插入圖片描述
文件名:calculator.py
類名:Jessi

class Jessi:
    def __init__(self, a, b):
        self.a = a
        self.b = b

    def count(self):
        return self.a + self.b

    def minus(self):
        return self.a - self.b

文件名:003_unittest_測試集.py

import unittest
from again.base_agin.fun.calculator import Jessi


class TestCal(unittest.TestCase):
    def setUp(self) -> None:
        print('start')

    def test01(self):
        j = Jessi(1, 2)
        try:
            self.assertEqual(j.count(), 5)
        except AssertionError as msg:
            print(msg)

    def test02(self):
        j = Jessi(6, 2)
        self.assertEqual(j.minus(), 4)

    def tearDown(self) -> None:
        print('end')


# if __name__ == '__main__':
#     unittest.main()

suit = unittest.TestSuite()  # 申請測試集套件對象
suit.addTest(TestCal('test01'))  # 添加測試案例     類名(函數名)
# suit.addTest(TestCal('test02'))

runner = unittest.TextTestRunner()  # 申請執行測試的對象
runner.run(suit)

如上,添加‘’test02‘’被註釋掉了,所以不會執行。

如果遇到,僅添加部分案例,但實際卻全部case都運行了,解決辦法可參考這裏

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章