python - 你如何在 Python 中断言某事不正确?

标签 python assert

在尝试理解 Python 中的 assert 时,特别是反转它,我想到了这个......

>>> assert != ( 5 > 2 )
>>> assert != ( 2 > 5 )

现在第一行失败,第二行通过。断言某事为假的惯用方式是什么?

最佳答案

你会使用 boolean not operator ,而不是 != 不等式比较运算符:

>>> assert not (5 > 2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AssertionError
>>> assert not (2 > 5)

如果测试在 bool 意义上为真,则 assert 通过,因此您需要使用 bool not 运算符来反转测试。

关于python - 你如何在 Python 中断言某事不正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24439370/

相关文章:

python - Enthought Python、Sage 或其他(在 Unix 集群中)

python - SymPy:lambdified dot() with (3,n)-array

Java断言 - 第二个参数评估?

Python 断言 isinstance() 向量

sql - SQL 选择列表中的 bool 表达式

java - 为什么 JUnit 不提供 assertNotEquals 方法?

python - TypeError : B2() takes exactly 1 argument (0 given). ..如果函数完全按照命名或使用变量调用,为什么很重要

python - 在 python 中将数字表示为两个幂之和的最快方法是什么

python - 如何对列表项使用 sum() ?

testing - 断言中未使用的警告变量