断言与参数化

断言(自动判断实际结果与预期结果)
参数化(将测试数据装入json)
跳过
生成测试报告

assertEqual与assertIn

assertEqual(预期结果,实际结果)------如果相等用例通过,不相等抛出异常用例不通过。

assertIn(预期结果,实际结果)-------预期结果是否包含在实际结果中,存在则用例通过、不存在抛出异常用例不通过。

import unittest

class TestAssert(unittest.TestCase):

    def test_equal(self):
        self.assertEqual(10,10)

    def test_asser_2(self):
        self.assertEqual(10,11)

    def test_in(self):
        self.assertIn('admin','欢迎 aaaaaaaaaaaaaaaaaaaaaaaadminnnnnnnnnnnnnnnnnnnn 登录')
import unittest
from assert01 import TestAssert

suite = unittest.TestSuite()

suite.addTest(unittest.makeSuite(TestAssert))

unittest.TextTestRunner().run(suite)

运行结果
在这里插入图片描述

参数化

环境(安装)
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple parameterized

import unittest

from tolols import add
from parameterized import parameterized
data = [(1,1,2),(1,2,3),(2,3,5),(4,5,9)]

class TestAdd(unittest.TestCase):
    @parameterized.expand(data)
    def test_1(self,a,b,expect):
        print(f'a:{b},b:{b},expect:{expect}')
        self.assertEqual(expect,add(a,b))

使用parameterized可以加载外部数据

import unittest

from tolols import add
from parameterized import parameterized
data = [(1,1,2),(1,2,3),(2,3,5),(4,5,9)]

class TestAdd(unittest.TestCase):
    @parameterized.expand(data)
    def test_1(self,a,b,expect):
        print(f'a:{b},b:{b},expect:{expect}')
        self.assertEqual(expect,add(a,b))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值