python接口自动化——unittest断言

前言:

  在测试用例中,执行完测试用例后,最后一步是判断测试结果是 pass 还是 fail,自动化测试脚本里面一般把这种生成测试结果的方法称为断言(assert)。用 unittest 组件测试用例的时候,断言的方法还是很多的,下面介绍几种常用的断言方法:assertEqual、assertIn、assertTrue。想了解更多可以点击 传送门 看一下最后的小结有大致介绍。unittest断言计划是分上、中、下三篇有简单到复杂的给小伙伴们介绍一下,填补一下这部分的空白。

基本断言方法

基本的断言方法提供了测试结果是True还是False。所有的断言方法都有一个msg参数,如果指定msg参数的值,则将该信息作为失败的错误信息返回。

import unittest
class TestStringMethods(unittest.TestCase):
    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')
    def test_isupper(self):
        self.assertTrue('FOO'.isupper())#验证字符串是否为大写,如果是返回True
        self.assertFalse('Foo'.isupper())#验证字符串是否为大写,如果不是返回False
    def test_split(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)
#4.copy 和运行测试
class IntegerArithmeticTestCase(unittest.TestCase):
     def testSubtract(self):  # test method names begin with 'test'
         result = 6-5   #实际结果
         hope = 1       #期望结果
         self.assertEqual(result, hope)

     def testDivide(self):
         result = 7 / 2  # 实际结果
         hope = 3.5  # 期望结果
         self.assertEqual(result, hope)

if __name__ == '__main__':
    unittest.main(["-v"])

案例2

import unittest
#4.copy 和运行测试
class IntegerArithmeticTestCase(unittest.TestCase):
    def setUp(self):  #每个测试方法执行之前都会执行setUp方法
        print("我已经进入到IntegerArithmeticTestCase类的setUp方法中")
        print("setUp:在写测试用例的时候,每次操作其实都是基于打开浏览器输入对应网址这些操作,这个就是执行用例的前置条件。")

    def tearDown(self):
        print("我已经进入到IntegerArithmeticTestCase类的tearDown方法中")
        print("tearDown:在写测试用例的时候,每次操作其实都是基于打开浏览器输入对应网址这些操作,这个就是执行用例的后置条件。")

    def testSubtract(self):  # test method names begin with 'test'
         print("我已经进入到IntegerArithmeticTestCase类的testSubtract方法中")
         result = 6-5   #实际结果
         hope = 1       #期望结果
         print("result=%s,hope=%s"%(result,hope))
         self.assertEqual(result, hope)
    def testDivide(self):
         print("我已经进入到IntegerArithmeticTestCase类的testDivide方法中")
         result = 7 / 2  # 实际结果
         hope = 3.6  # 期望结果
         print("result=%s,hope=%s" % (result, hope))
         self.assertEqual(result, hope)
if __name__ == '__main__':
    unittest.main(["-v"])

 案例3

import unittest
#4.copy 和运行测试
class IntegerArithmeticTestCase(unittest.TestCase):
    def setUp(self):  #每个测试方法执行之前都会执行setUp方法
        print("我已经进入到IntegerArithmeticTestCase类的setUp方法中")
        print("setUp:在写测试用例的时候,每次操作其实都是基于打开浏览器输入对应网址这些操作,这个就是执行用例的前置条件。")

    def tearDown(self):
        print("我已经进入到IntegerArithmeticTestCase类的tearDown方法中")
        print("tearDown:在写测试用例的时候,每次操作其实都是基于打开浏览器输入对应网址这些操作,这个就是执行用例的后置条件。")

    def testSubtract(self):  # test method names begin with 'test'
         print("我已经进入到IntegerArithmeticTestCase类的testSubtract方法中")
         result = 6-5   #实际结果
         hope = 1       #期望结果
         print("result=%s,hope=%s"%(result,hope))
         self.assertEqual(result, hope)
    def testDivide(self):
         print("我已经进入到IntegerArithmeticTestCase类的testDivide方法中")
         result = 7 / 2  # 实际结果
         hope = 3.6  # 期望结果
         print("result=%s,hope=%s" % (result, hope))
         self.assertEqual(result, hope)
if __name__ == '__main__':
    suite1 = unittest.TestSuite()
    suite2 = unittest.TestSuite()
    # 添加测试用例
    suite1.addTest(IntegerArithmeticTestCase("testSubtract"))
    # 实例化TextTestRunner类
    runner = unittest.TextTestRunner()
    # 运行测试用例
    runner.run(suite1)

参考文献

python接口自动化(二十三)--unittest断言——上(详解) - 北京-宏哥 - 博客园

python接口自动化(二十四)--unittest断言——中(详解) - 北京-宏哥 - 博客园

python接口自动化(二十五)--unittest断言——下(详解) - 北京-宏哥 - 博客园

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

欲游山河十万里

你的鼓励是我们创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值