import pytest
import requests
#定义预期结果:
expect = {"title":"V2EX"}
def test_case_01():
response = requests.get(url='https://www.v2ex.com/api/site/info.json')
#拿到json数据:
print(response.json())
if response.json()["title"] == expect["title"]:
print("断言成功")
assert 1
else:
print("断言失败")
assert 0
def test_case_02():
response = requests.get(url='https://www.v2ex.com/api/site/info.json')
#拿到json数据:
print(response.json())
if response.json()["title"] != expect["title"]:
print("断言成功")
assert 1
else:
print("断言失败")
assert 0
if __name__ == '__main__':
#pytest框架自动收集这个脚本里面所有以test开头当成测试用例执行里面的代码
pytest.main(["-s","run测试用例函数写法.py"])
"""
结果:
============================= test session starts =============================
platform win32(当前平台信息) -- Python 3.6.6(解释器版本), pytest-5.3.2(pytest版本), py-1.8.1, pluggy-0.13.1(以及其他版本)
rootdir(根目录): D:s27day68
collected 1 item(收集了一个用例)
run.py {'title': 'V2EX', 'slogan': 'way to explore', 'description': '创意工作者们的社区', 'domain': 'www.v2ex.com'}
断言成功
.
================================== FAILURES(错误信息) ===================================
________________________________ test_case_02 _________________________________
def test_case_02():
response = requests.get(url='https://www.v2ex.com/api/site/info.json')
#拿到json数据:
print(response.json())
if response.json()["title"] != expect["title"]:
print("断言成功")
assert 1
else:
print("断言失败")
> assert 0
E assert 0
============================== 1 passed in 1.01s(用例通过并耗时1秒) ==============================
"""