Selenium自动化测试-assert断言

# assertEqual(a,b,msg=None)  检查a==b,不等则报错
# assertNotEqual(a,b,msg=None)  检查a!=b,相等则报错
# assertIn(a,b,msg=None)  检查a in b,不在报错
# assertNotIn(a,b,msg=None)  检查a not in b,在报错
# assertTrue(a,msg=None)  检查表达式a成立,不成立报错
# assertFalse(a,msg=None) 检查表达式a不成立,成立报错
# assertIsInstance(obj,cls,msg=None) 检查obj是cls一个实例,不是就报错
# assertNotIsInstance(obj,cls,msg=None)检查obj不是cls一个实例,是就报错
# assertIsNone(expr,msg)  #判断expr是否为空
# assertIsNotNone(expr,msg)  #判断expr是否不为空

新增:

1、assertAlmostEqual()

  用于单元测试中以检查两个给定的值是否几乎相等。此函数将使用五个参数作为输入,并根据断言条件返回布尔值。

此函数通过计算差值,四舍五入到给定的小数位数(默认为7),然后与零进行比较,以检查第一和第二个近似相等。

如果提供了增量而不是位置,则第一和第二之间的差异必须小于或等于增量。

用法: assertAlmostEqual(first, second, places=7, message=None, delta=None)

参数

  • first:第一个输入值(整数)
  • second:第二个输入值(整数)
  • places:近似的小数位
  • message:作为测试消息失败时显示的消息的字符串语句。
  • delta:近似的增量值
import unittest 
  
  
class TestStringMethods(unittest.TestCase):
    # negative test function to test if values are almost equal with place 
    def test_negativeWithPlaces(self):
        first = 4.4555
        second = 4.4566
        decimalPlace = 3
        # error message in case if test case got failed 
        message = "first and second are not almost equal."
        # assert function() to check if values are almost equal 
        self.assertAlmostEqual(first, second, decimalPlace, msg) 
  
    # positive test function to test if values are almost equal with place 
    def test_positiveWithPlaces(self):
        first = 4.4555
        second = 4.4566
        decimalPlace = 2
        # error message in case if test case got failed 
        message = "first and second are not almost equal."
        # assert function() to check if values are almost equal 
        self.assertAlmostEqual(first, second, decimalPlace, message) 
  
    # negative test function to test if values are almost equal with delta 
    def test_negativeWithDelta(self):
        first = 4.4555
        second = 4.4566
        delta = 0.0001
        # error message in case if test case got failed 
        message = "first and second are not almost equal."
        # assert function() to check if values are almost equal 
        self.assertAlmostEqual(first, second, None, message, delta) 
  
    # positive test function to test if values are almost equal with delta 
    def test_positiveWithDelta(self):
        first = 4.4555
        second = 4.4566
        delta = 0.002
        # error message in case if test case got failed 
        message = "first and second are not almost equal."
        # assert function() to check if values are almost equal 
        self.assertAlmostEqual(first, second, None, message, delta) 

  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值