python - 我们可以在 if 语句中使用 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)
推荐阅读
- python-3.x - 基于单个值生成一系列数字(坐标)
- python - 启动一个进程,如果父进程在 Windows 中被杀死则终止
- javascript -
session.will-download 事件触发一次 - android - 使用带有共享首选项的 Json 登录
- ios - 有没有办法从大量对象中仅加载几个项目并填充到 UICollectionView
- c# - 如何在 ASP.NET MVC 中使用 ViewBag 中的数据填充 DropDownList?
- python - 如果我有两个数据框,其中一个是其他数据框的子集,如何完全删除公共行?
- django-rest-framework - 如何使用 Mongoengine REST 将数据发布到嵌入式文档
- ibm-cloud - 如何在不通过 ibm-console 登录的情况下访问 IBM logDNA
- amazon-web-services - 仅将 api 流量路由到其他 AWS EC2 实例