Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我们可以在if语句中使用assert吗?_Python_Pytest_Assert - Fatal编程技术网

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

Python 我们可以在if语句中使用assert吗?,python,pytest,assert,Python,Pytest,Assert,我有一个使用assert检查某些条件的测试。我可以在if语句中使用then断言吗?例如: assert 'string 1' in response, "Did not get 'string 1'!" if assert: continue... assert引入的是一条语句,而不是一个表达式,因此它不能在语法上用作if语句的条件。不过,没有必要这样做,因为声明 assert cond, msg 相当于 if not cond: raise Assert

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

assert 'string 1' in response, "Did not get 'string 1'!"
if assert:
    continue...
assert引入的是一条语句,而不是一个表达式,因此它不能在语法上用作if语句的条件。不过,没有必要这样做,因为声明

assert cond, msg
相当于

if not cond:
    raise AssertionError(msg)

你试过了吗?你期望它做什么?是的,得到了错误'SyntaxError:invalid syntax'认为我用错了way@Roman如果断言为true,它将继续,这就是断言的目的。不,因为断言引入的是语句,而不是表达式。