我们可以在 if 语句中使用 assert 吗?

时间:2021-06-08 13:45:07

标签: python pytest assert

我有一个使用 assert 检查某些条件的测试。我可以在 if 语句中使用 then assert 吗?如下:

assert 'string 1' in response, "Did not get 'string 1'!"
if assert:
    continue...

1 个答案:

答案 0 :(得分:1)

assert 引入了一个语句,而不是一个表达式,因此它不能在语法上用作 if 语句的条件。但是没有必要这样做,因为声明

assert cond, msg

相当于

if not cond:
    raise AssertionError(msg)
相关问题