selenium

Pytest学习笔记2-断言

2018-11-17  本文已影响0人  少杰创业笔记

1. 断言

断言语句是什么?断言语句是将调试断言插入程序的便捷方式:

assert_stmt :: =“assert” expression[“,” expression]

简单的形式, 相当于assert expression

if __debug__:
    if not expression: raise AssertionError

上述代码的意思是,当assert expression为假的时候,就抛出一个异常:

# 栗子
girl = False

# 判断这个人是不是妹子
def assert_girl(expression):

    if not expression:
        raise AssertionError

assert_girl(girl)

这个栗子会抛出一个AssertionError的异常

D:\Python34\python.exe C:/Users/lenovo/Desktop/pytest学习/pytest1/test_add.py
Traceback (most recent call last):
  File "C:/Users/lenovo/Desktop/pytest学习/pytest1/test_add.py", line 26, in <module>
    assert_girl(girl)
  File "C:/Users/lenovo/Desktop/pytest学习/pytest1/test_add.py", line 24, in assert_girl
    raise AssertionError
AssertionError

拓展形式,相当于assert expression1,expression2

if __debug__:
    if not expression1: raise AssertionError(expression2)

上诉代码的意思是当assert expression1,expression2为假的时候,就会抛出一个异常。我们最常使用就是这种拓展形式

# 判断2个数是否相等
def assert_num(expression1, expression2):

    expression = expression1 == expression2
    if not expression:
        raise AssertionError

assert_num(1, 2)

同样这个栗子也会抛出一个异常:

Traceback (most recent call last):
  File "C:/Users/lenovo/Desktop/pytest学习/pytest1/test_add.py", line 24, in <module>
    assert_num(1, 2)
  File "C:/Users/lenovo/Desktop/pytest学习/pytest1/test_add.py", line 22, in assert_num
    raise AssertionError
AssertionError

2. 断言的使用

现在我们对一个登录接口进行断言,先创建一个test_login的py文件。定义一个login的接口,和test_login的用例

# 登录接口
def login(username, password):

    # 在这里,我就不做数据库查询了,只是举一个简单的例子,只要用户名或密码传值,就算登录成功
    if username or password:
        return {"message":"登录成功", "code": 0}
    else:
        return {"message":"账号或密码错误", "code": 400}

# 接口测试用例
def test_login():

    res = login("admin", "123456")
    assert res['code'] == 1
    

Pycharm中的terminal运行测试用例

pytest

运行结果如下:

C:\Users\lenovo\Desktop\pytest学习>pytest                                                                                                                                                                                            
============================= test session starts =============================
platform win32 -- Python 3.4.4, pytest-3.8.0, py-1.6.0, pluggy-0.7.1
rootdir: C:\Users\lenovo\Desktop\pytest学习, inifile:                                                                                                                                                                                
plugins: celery-4.2.1, allure-adaptor-1.7.10, html-1.19.0, metadata-1.7.0
collected 1 item                                                               

pytest1\test_add.py F                                                    [100%]

================================== FAILURES ===================================
_________________________________ test_login __________________________________

    def test_login():
    
        res = login("admin", "123456")
>       assert res['code'] == 1
E       assert 0 == 1

pytest1\test_add.py:30: AssertionError
========================== 1 failed in 0.20 seconds ===========================

3. 总结

上一篇 下一篇

猜你喜欢

热点阅读