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都运行了,解决办法可参考这里

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值