一.目录思路:
1.在任何一家公司 先创建一个目录 例如:rc_test yh_test
2.在rc_test目录下创建多个目录
3.创建common目录 用于放一些方法 (待确定)
4.创建config配置目录 用于存放配置
5.创建data目录 用于放一些数据 例如excel表 csv文件之类
6.创建interface目录 在这个目录下去写接口代码 在这个区域内保证接口可以跑通 所有的接口写在这个目录下
7.创建cases目录 在这个目录下去写功能方面代码 与接口代码区分开来 框架与接口框架相同 都为单元测试框架 所有的功能测试case都写在这个目录下
8.创建py文件 run.py 用来跑cases目录下的所有cases
9.创建report目录 这个目录下用来放一些用run.py跑出来的HTML报告
二.接口代码 和功能代码框架怎么去写?
接口与功能都可用单元测试框架
单元测试框架 功能:
1.先创建一个类 继承unittest.TestCase 例如登陆:class TestLogin(unittest.TestCase)
2.环境搭建:
@classmethod
def setUpClass(cls):
print('环境搭建')
在需要用到的时候可以写上 不需要的时候可以不用写(具体什么时候需要用 什么时候不需要用 是用来干什么的?)
3.环境还原:
@classmethod
def tearDownClass(cls):
print('环境还原')
同上环境搭建 什么时候该用 什么时候不该用 能起到什么作用
4.写测试用例
登陆为例:
def test_login_normal(self):
username = 'gaoguiquan' 用户名
password = '123456' 密码
ret = user.login(username,password) 用一个对象ret去接收user文件下的login接口的返回 需要传参数username,password
i_password = ret.get('from').get('password') ret.get('from').get('password')用来拿到实际结果,用一个对象i_password去接收实际结果
self.assertEqual(password,i_password) 做一个断言 password为预期结果 i_password为实际结果
可在下面写sql 去查有没有落到表里 sql = ‘***’
测试用例def后面必须以test开头 不然不能识别为一个用例
5.用main函数去执行
if __name__ == '__name__':
unittest.main()
单元测试框架 接口:
除过写用例部分 所有同上
接口方面需要导入requsets模块
登陆接口为例:
from config import settings 导入配置 settings下面的host等等东西 之后可直接用
import requests 导入requests模块
def test_login(username,password)
url = settings.host + 'path' settings.host是之前在config目录下settings文件下写的 现在直接调用 +上path拼接成url
data = {'username':username,'password':password} username password先放进去 等需要用的时候传参
rep = requests.post(url,data) 用一个对象去接收接口的返回
return req.json() 返回接口响应 以json的格式
run.py:
import unittest 导入unittest模块
from HTMLTestRunnerX import HTMLTestRunner 导入HTML....用于生成HTML报告
if __name__ == '__main__': 用于执行下面代码 主要下面的代码都得缩进进去
suite = unittest.defaultTestLoader.discover('./cases','*.py') 创建一个测试套件对象suite 执行cases目录下所有的以py结尾的文件 ./cases 执行这个目录下所有的用例 *.py 所有以.py结尾的文件
b = open(file='测试结果.html',mode='wb') 创建一个名字为测试结果的html文件 权限是可写 w写入 有图片的话带b wb 创建个对象b 用b去代表它
runner = HTMLTestRunnerX.HTMLTestRunner(stream=b,verbosity=3,title='测试结果') 创建一个运行器对象 stream=b 写入到哪里 verbosity 详细程度 title HTML内的标题
runner.run(suite) 运行器对象去运行套件 生成HTML报告
b.close() 关闭文件
执行完之后 报告放入创建好的report目录下
接口自动化:
get接口有参数的话用params去传参数
post接口分几种方式 键值对x-www的话用data去传参
json的话 传参的时候用json
需要上传文件的话用files去上传文件 例:
# cc={ # 'username':'lisi' # } # f = { # 'pic':open(file='/Users/edy/Desktop/WechatIMG325.jpeg',mode='rd',encoding='utf-8') # } # result2 = requests.post(url='http://127.0.0.1:8080/fore/user/upload.action',data=cc,files=f) # print(result2.json())
如接口依赖的话需要去提取 例如请求头:
可在外写方法提取cookie
def Setcookie(headers): cookie = headers['Set-cookie'] if cookie != None: file = open(file='cookie.txt',mode='w',encoding='utf-8') file.write(cookie) file.close() print('保存成功')
提取cookie
def Getcookie(): file = open(file='cookie.txt', mode='r', encoding='utf-8') cookie = file.read() file.close() return cookie
获得cookie
之后在需要的时候可以去调用函数去提取和获得 例:
import unittest import requests from 接口自动化 import utils class TestJk(unittest.TestCase): # list = [] # headers = {} @classmethod def setUpClass(cls): print('环境搭建') def test_Code(self,): '''获取验证码接口''' c = { } result = requests.post(url='**') if result.status_code == 200: self.assertEqual(result.json()['status'], 200, '验证不通过') #提取code code = result.json()['data']['code'] # 提取cookie 调用函数 utils.Setcookie(result.headers) #提取cookie2 存到列表里 # self.list.clear() # self.list.append(result.headers['Set-cookie']) #提取cookie3 存到字典里 # self.headers['cookie'] = result.headers['Set-cookie'] return code def test_zhuce(self): '''注册接口''' code= self.test_Code() a= { 'username':'zhangsan', 'password':'123123', 'email':'123@163.com', 'VerifyCode':'code' } #第一种方式 调用函数 h = { 'cookie':utils.Getcookie() } #第二种方式 列表 # h = { # 'cookie':self.list[0] # } #第三种方式 字典 result2 = requests.post(url='**',data=a,headers=h) if result2.status_code == 200: print(result2.json()) self.assertEqual(result2.json()['status'],200,'验证不通过') @classmethod def tearDownClass(cls): print('环境还原') if __name__ == '__main__': unittest.main()
细节处再去实践 补充 修改
仅自己记录所学