#pytest常用的python断言:1)assertxx:判断xx为真2)assertnotxx:判断xx不为真3)assertainb:判断b包含a4)assertanotinb: 判断b不包含a5)asserta ==b:判断a等于b6)asserta !=b:判断a不等于b (2)使用pytest.raises触发期望异常的断言 #test_run.pydeftest_assert(self): r= requests.get("https://www.b...
我们这里也可以通过pytest.raises进行断言,我们可以根据返回的tpye进行对比,查看我们预期的与实际的是否一致 import pytest class Test_01: def cake(self): a = 'anjing' b = 'test_anjing' assert a == b def test_001(self): print('Test_01下的用例001') with pytest.raises(AssertionError) as excin...
assert a not in b:判断b不包含a assert a == b:判断a等于b assert a != b:判断a不等于b 二、异常断言 可以使用pytest.raises作为上下文管理器,当抛出异常时可以获取到对应的异常实例,然后断言它抛出的异常是不是预期的。 # 断言异常 def test_zero_division(): with pytest.raises(ZeroDivisionError...
assert not xx :判断 xx 不为真 assert a in b :判断 b 包含 a assert a == b :判断 a 等于 b assert a != b :判断 a 不等于 b 异常断言 可以使用 pytest.raises 作为上下文管理器,当抛出异常时可以获取到对应的异常实例 # 断言异常 def test_zero_division(): with pytest.raises(ZeroDivisionErro...
下面的用例执行后断言是成功的。 import pytest def test_zero_division(): with pytest.raises(ZeroDivisionError): 1 / 0 往期推荐 Pytest系列(1)-快速上手 Pytest系列(2)-Pytest指令详解 发布于 2021-06-12 23:18 软件测试管理 开发与测试 软件测试工程师 赞同4添加评论 分享喜欢...
Python's assert statement PREMIUM Trey Hunner 5 min. read • 4 min. video • Python 3.9—3.13 • Oct. 9, 2023 Let's talk about Python's assert statement.assert either does nothing or raises AssertionErrorWhen the assert statement is given a value that's True, it does nothing:...
对这类特定异常的断言,可以采用pytest中的pytest.raises()进行处理。以下示例对一个判断是否为闰年的方法进行测试:# is_leap_year.pydefis_leap_year(year):# 先判断year是不是整型if isinstance(year, int) isnotTrue: raise TypeError("传入的参数不是整数") elif year == 0: raise ValueError(...
assert not xx :判断 xx 不为真 assert a in b :判断 b 包含 a assert a == b :判断 a 等于 b assert a != b :判断 a 不等于 b 异常断言 可以使用 pytest.raises 作为上下文管理器,当抛出异常时可以获取到对应的异常实例 #断言异常 deftest_zero_division(): ...
当使用 python -O testAssert.py 运行时,内置属性 __debug__ 会输出 False,assert 1 > 2 语句由于没有执行不会报任何异常。 assert关键字语法 ①assert关键字语法格式如下: assert expression 1. 等价于: if not expression: raise AssertionError
Python if __debug__: if not expression: raise AssertionError(assertion_message) # Equivalent to assert expression, assertion_message If __debug__ is true, then the code under the outer if statement runs. The inner if statement checks expression for truthiness and raises an AssertionError ...