pytest framework _assert assertion

In automated test cases, the most commonly used assertion is the equality assertion, which is to assert that the expected result is consistent with the actual result. Usually the data types of the expected result and actual result we assert are strings, tuples, dictionaries, lists, and objects. Pytest can perfectly support the equality assertion of these data types through assert and ==. Let's introduce the assertion operations of several common data types.

import pytest

def test_01():
	"""
	常用断言
	"""
	assert aa == driver.find_element_by_link_text("新闻").text
    assert "aa" in driver.find_element_by_link_text("新闻").text
    assert "bb" not in driver.find_element_by_id("su").get_attribute("value")
    assert find_element_by_id("cc").is_selected()
    assert find_element_by_id("dd").is_displayed()

"""
【pytest_常用断言】
pytest 里面断言实际上就是 python 里面的 assert 断言方法,常用的有以下几种
assert xx :判断 xx 为真
assert not xx :判断 xx 不为真
assert a in b :判断 b 包含 a
assert a == b :判断 a 等于 b
assert a != b :判断 a 不等于 b


【selenium对元素进行判断:常用这两个】
driver.find_element_by_link_text("新闻").text	获取元素的文本值
driver.find_element_by_id("su").get_attribute("value")		获取元素value属性的值


driver.title	获取页面“title”,百度首页的title是“百度一下,你就知道”
driver.current_url	获取窗口的url
find_element_by_id("xx").is_selected()	判断元素是否被选择
find_element_by_id("kw").is_displayed()		判断元素在页面是否显示

"""

Exception assertion:
If we want to assert that the exception thrown is not expected, such as execution: 1/0, the expected result is throwing an exception: ZeroDivisionError: division by zero, then we have to assert the exception, usually asserting the type and value of the exception Worth it. Here the exception type of 1/0 is ZeroDivisionError, and the value of the exception is division by zero,
so the use case can be designed like this:

import pytest
def test_zero_division():
	"""
	断言异常
	"""
    with pytest.raises(ZeroDivisionError) as excinfo:
        1 / 0
    assert excinfo.type == ZeroDivisionError
    assert "division by zero" in str(excinfo.value)

	"""
	断言2
	"""
	try:
      login = driver.find_element_by_name("tj_login")
    except ElementNotVisibleException:
      raise 

Examples of assertions:

def test_assertion():
    assert [1, 2, 3] == [1, 2, 4], "left is [1,2,3], right is [1,2,4]"


test_2.py::test_assertion ---进入要执行模块的的界面---
FAILED

================================== FAILURES ===================================
_______________________________ test_assertion ________________________________

    def test_assertion():
>       assert [1, 2, 3] == [1, 2, 4], "left is [1,2,3], right is [1,2,4]"
E       AssertionError: left is [1,2,3], right is [1,2,4]
E       assert [1, 2, 3] == [1, 2, 4]
E         At index 2 diff: 3 != 4
E         Full diff:
E         - [1, 2, 4]
E         ?        ^
E         + [1, 2, 3]
E         ?        ^

test_2.py:15: AssertionError
--------- generated html file: file://D:\se_frame\Reports\report.html ---------
=========================== short test summary info ===========================
FAILED test_2.py::test_assertion - AssertionError: left is [1,2,3], right is ...
============================== 1 failed in 9.49s ==============================

Process finished with exit code 0

Guess you like

Origin blog.csdn.net/weixin_45451320/article/details/113926498