pytest登陆接口的测试用例

经过昨天学习,终于找到一点热情,我们今天继续学习下pytest,顺便来写一个简单的登录接口测试用例

通过编写登录函数测试发现报以下错误

微信截图_20200825122905.png

经过百度查资料发现,使用pytest测试的单元,如果函数有参数需要使用专门的装饰器@pytest.mark.parametrize来传参

好吧,我们接着来看书写的测试代码,基本是这样的

#!/usr/bin/python3
#coding: utf-8
#test_login.py
import requests
import hashlib
import json
import pytest

test_info = [("test", "test")]

@pytest.mark.parametrize("fp_user, fp_pass", test_info)
def test_login(fp_user, fp_pass):
    url = "http://127.0.0.1/test/api/login"
    headers = {
        "Content-Type": "application/json"
    }
    data = {
        "name": fp_user,
        "password": hashlib.md5(fp_pass.encode(encoding="utf-8")).hexdigest()
    }
    r = requests.post(url, data=json.dumps(data), headers=headers)
    print(r.json())
    assert r.json()["rtn"] == 0

test_list = ['test_login.py']
pytest.main(test_list)

执行pytest -s test_login.py,打印

微信截图_20200825123612.png

这里我们再介绍下pytest.main()

1.直接执行pytest.main() 【自动查找当前目录下,以test_开头的文件或者以_test结尾的py文件】
2.设置pytest的执行参数 pytest.main(['--html=./report.html','test_login.py'])【执行test_login.py文件,并生成html格式的报告】
main()括号内可传入执行参数和插件参数,通过[]进行分割,[]内的多个参数通过‘逗号,’进行分割
运行目录及子包下的所有用例  pytest.main(['目录名'])
运行指定模块所有用例  pytest.main(['test_reg.py'])
运行指定模块指定类指定用例  pytest.main(['test_reg.py::TestClass::test_method'])  冒号分割


内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://sulao.cn/post/770.html

我要评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。