一、新建框架
base:基础类
config:配置文件
data:存储测试数据的
image:存储图片文件的
log:存储日志文件
page:对象层 report:
存储测试报告
test:测试模块
utils:工具类的封装
二、在besc层写入底层封装的方法,代码如下:
import requests import json import unittest class Requests: def request(self,url,method='get',**kwargs): if method=='get': return requests.request(url=url,method=method,**kwargs) elif method=='post': return requests.request(url=url,method='post',**kwargs) elif method=='put': return requests.request(url=url,method='put',**kwargs) elif method=='delete': return requests.request(url=url,method='delete',**kwargs) def get(self,url,**kwargs): return self.request(url=url,**kwargs) def post(self,url,**kwargs): return self.request(url=url,method='post',**kwargs) def put(self,url,**kwargs): return self.request(url=url,method='put',**kwargs) def delete(self,url,**kwargs): return self.request(url=url,method='delete',**kwargs)
二、在data下新建一个json文件,把参数写进去
三、在utils文件下创建一个py文件,获取当前文件的路径
import os def base_dir(): return os.path.dirname(os.path.dirname(__file__)) def filePath(directorty='data',fileName=None): return os.path.join(base_dir(),directorty,fileName)
执行代码,可以获取当前工程的路径:
四、在utils下创建一个py文件,读取文件内容
from utils.operationPath import filePath import json def readJson(fileName): return json.load(open(filePath(fileName=fileName),encoding='utf-8'))
五、在page层写入具体的操作方法
from base.method import Requests from utils.operationJson import * def objRequest(): return Requests() def getLogin(): r=objRequest().get( url='http://localhost:5000/login') return r def postLogin(key): """ 针对login服务的post请求方法的封装 :param key:指的是login.json文件里面的key值 :return: """ r=objRequest().post( url='http://localhost:5000/login', json=readJson(fileName='login.json')['login'][key]) return r
六、新建一个test的包,写测试用例
import pytest from page.login import * def test_login_get(): """登陆验证:验证login服务获取的信息""" r=getLogin() assert r.status_code==200 assert r.json()['status']==0 assert r.json()['data']=='this is a login page' def test_login_post_001(): """登陆验证:验证登陆成功的信息""" r=postLogin(key='success') assert r.status_code==200 assert r.json()['username']=='wuya' def test_login_post_002(): """登陆验证:验证用户名为空的信息""" r=postLogin(key='username') assert r.status_code==400 assert r.json()['message']['username']=='用户名不能为空' def test_login_post_003(): """登陆验证:验证密码为空的信息""" r=postLogin(key='password') assert r.status_code==400 assert r.json()['message']['password']=='账户密码不能为空' def test_login_post_004(): """登陆验证:验证年龄为空的信息""" r=postLogin(key='age') assert r.status_code==400 assert r.json()['message']['age']=='年龄必须为正正数' def test_login_post_005(): """登陆验证:验证性别为空的信息""" r = postLogin(key='sex') assert r.status_code == 400 assert r.json()['message']['sex']=='性别只能是男或者女' if __name__ == '__main__': pytest.main(["-s","-v","test_login_api.py"])
八、书籍实战
1、在工程的根目录下创建一个conftest.py文件,使用fixture共享代码
import pytest from base.method import Requests from utils.operationJson import readJson obj=Requests() @pytest.fixture() def token(): r=obj.post( url='http://localhost:5000/auth', json=readJson(fileName='book.json')['book']['auth']) return r.json()['access_token'] @pytest.fixture() def headers(token): return {'Authorization':'jwt {0}'.format(token)}
2、besc层(和login的一样)
3、data(和login一样)
4、路径不变
5、page对象层
from utils.operationJson import readJson from base.method import Requests import json obj=Requests() def addBook(headers): r=obj.post(url='http://localhost:5000/v1/api/books', json=readJson(fileName='book.json')['book']['add'], headers=headers) json.dump(r.json()['datas']['id'],open('bookID','w')) return r def bookID(): """获取书籍的ID信息""" return int(json.load(open('bookID','r'))) def delBook(headers): r=obj.delete( url='http://localhost:5000/v1/api/book/{0}'.format(bookID()), headers=headers) return r def setBook(headers): r=obj.put( url='http://localhost:5000/v1/api/book/{0}'.format(bookID()), json=readJson(fileName='book.json')['book']['put'], headers=headers) return r def getBook(headers): r=obj.get( url='http://localhost:5000/v1/api/book/{0}'.format(bookID()), headers=headers) return r def allBooks(headers): r=obj.get(url='http://localhost:5000/v1/api/books', headers=headers) return r
6、test写和执行测试用例
import pytest from page.book import * def test_all_books(headers): """书籍验证:获取所有书籍""" r=allBooks(headers) assert r.status_code==200 assert r.json()['status']==0 assert len(r.json()['datas'])==2 def test_get_books(headers): """书籍验证:添加书籍""" addBook(headers) r=getBook(headers) delBook(headers) assert r.status_code==200 assert r.json()['status']==0 assert r.json()['datas'][0]['id']==bookID() def test_del_books(headers): """书籍验证:删除书籍""" addBook(headers) getBook(headers) r=delBook(headers) assert r.status_code==200 assert r.json()['status']==1009 assert len(allBooks().json()['datas'])==2 def test_add_books(headers): """书籍验证:获取所有书籍""" r=addBook(headers) delBook(headers) # print(r.text) assert r.status_code == 200 assert r.json()['status']==1002 assert len(r.json()['datas'])==4 def test_put_books(headers): """书籍验证:获修改书籍""" addBook(headers) r=setBook(headers) getBook(headers) delBook(headers) # print(r.text) assert r.status_code==200 assert r.json()['status']==1008 assert len(r.json()['datas'])==1 if __name__ == '__main__': pytest.main(["-s","-v","test_book_api.py::test_put_books"])