Pytest 作业
pytest的作业:
创建conftest.py文件,利用pytest.fixture(),实现以下接口验证
1、通过请求书架推荐位接口(https://api-bc.wtzw.com/api/v4/book-shelf/recommend),获取返回的id和title
2、通过id请求详情页接口(https://api-bc.wtzw.com/api/v4/book/detail)
3、验证详情页接口返回的title是否和书架接口返回的一致
代码如下:
#conftest.py import pytest import requests @pytest.fixture() def getIdTitle(): r = requests.post(url="https://api-bc.wtzw.com/api/v4/book-shelf/recommend") book_id = r.json()['data']['book']['id'] book_title = r.json()['data']['book']['title'] return book_id, book_title
# book.py import requests import pytest def test_getTitle(getIdTitle): r = requests.get(url='https://api-bc.wtzw.com/api/v4/book/detail', params={'id': getIdTitle[0]}) book_title = r.json()['data']['book']['title'] assert book_title == getIdTitle[1] if __name__ == "__main__": pytest.main(["-s", "book.py"])