断言语句有什么作用?
What does the assert statement do?
我是 Selenium 的新手。我想了解 assert
语句的作用。
在下面提到的代码中,我无法指出它存在的原因(第三行)。
driver = webdriver.Chrome('./data/chromedriver.exe')
driver.get("http://www.python.org")
assert "Python" in driver.title
您显然将 Selenium 与 Python 一起使用。总之,assert
关键字在很多编程语言中都有。
有关 assert
的语言独立解释,请查看 Wikipedia:
In computer programming, specifically when using the imperative programming paradigm, an assertion is a predicate (a Boolean-valued function over the state space, usually expressed as a logical proposition using the variables of a program) connected to a point in the program, that always should evaluate to true at that point in code execution. Assertions can help a programmer read the code, help a compiler compile it, or help the program detect its own defects.
For the latter, some programs check assertions by actually evaluating the predicate as they run. Then, if it is not in fact true – an assertion failure –, the program considers itself to be broken and typically deliberately crashes or throws an assertion failure exception.
可在此处找到 assert
的官方 Python 文档:
https://docs.python.org/3/reference/simple_stmts.html#the-assert-statement
assert
其实不是一个函数,而是一个语句。这是对特定条件成立的检查。否则,程序将无法 运行 以某种方式。在 Python 的情况下,将引发 AssertionError
:
if __debug__:
if not expression: raise AssertionError
更具体地说,如果在 http://www.python.org 页面的标题中找不到 Python,您问题中的断言将失败。
assert关键字用于比较实际结果和预期结果。主要用于验证。
例如:如果您正在测试一些 URL,您可以验证标题
assert actual_title == expected_title
如果值匹配则测试用例通过,否则测试用例失败给你 AssertionError
我是 Selenium 的新手。我想了解 assert
语句的作用。
在下面提到的代码中,我无法指出它存在的原因(第三行)。
driver = webdriver.Chrome('./data/chromedriver.exe')
driver.get("http://www.python.org")
assert "Python" in driver.title
您显然将 Selenium 与 Python 一起使用。总之,assert
关键字在很多编程语言中都有。
有关 assert
的语言独立解释,请查看 Wikipedia:
In computer programming, specifically when using the imperative programming paradigm, an assertion is a predicate (a Boolean-valued function over the state space, usually expressed as a logical proposition using the variables of a program) connected to a point in the program, that always should evaluate to true at that point in code execution. Assertions can help a programmer read the code, help a compiler compile it, or help the program detect its own defects.
For the latter, some programs check assertions by actually evaluating the predicate as they run. Then, if it is not in fact true – an assertion failure –, the program considers itself to be broken and typically deliberately crashes or throws an assertion failure exception.
可在此处找到 assert
的官方 Python 文档:
https://docs.python.org/3/reference/simple_stmts.html#the-assert-statement
assert
其实不是一个函数,而是一个语句。这是对特定条件成立的检查。否则,程序将无法 运行 以某种方式。在 Python 的情况下,将引发 AssertionError
:
if __debug__:
if not expression: raise AssertionError
更具体地说,如果在 http://www.python.org 页面的标题中找不到 Python,您问题中的断言将失败。
assert关键字用于比较实际结果和预期结果。主要用于验证。
例如:如果您正在测试一些 URL,您可以验证标题
assert actual_title == expected_title
如果值匹配则测试用例通过,否则测试用例失败给你 AssertionError