- 代码实现自动化相干理论
- 代码编写脚本和工具实现脚本区别是啥?
第一步:python+request+unittest;
具体描述?
第二步:封装、调用、数据驱动、日记、报告;
详细举例:
第三步:api\scripts\data\log\report\until…
1、抽取功能用例转为自动化用例
2、搭建环境(测试工具、)
3、搭建目录结构
4、编写脚本
5、实行脚本
6、设置持续集成
1、python、Pycharm、requests、pymysql、parametrize
2、jenkins、jdk
提示:由于编写的自动化脚本,而自动化脚本编写之前功能已测试完毕,所以不需要在单独搭建项目环境。
- import os
- # 服务器地址
- HPST = "http://user-p2p-test.itheima.net"
- # 项目目录路径
- dir_path = os.path.dirname(__file__)
复制代码
- api(api_register_login.py)
- from config import HOST
- class ApiRegisterLogin:
- # 初始化
- def __init__(self, session):
- # 获取session对象
- self.session = session # 实例化session,类下面的其他接口就能使用自己的session
- # 图片验证码url
- self.__url_img_code = HOST + "/common/public/verifycode1/{}" # {}是随机变量,用{}占用
- # 短信验证码url
- self.__url_phone_code = HOST + "/member/public/sendSms"
- # 注册url
- self.__url_register = HOST + "/member/public/reg"
- # 登录url
- self.__url_login = HOST + "/member/public/login"
- # 登录状态url
- self.__url_login_status = HOST + "/member/public/islogin"
- # 获取图片验证码接口 封装
- def api_img_code(self):
- pass
- # 获取短信验证码接口 封装
- def api_phone_code(self):
- pass
- # 注册接口 封装
- def api_register(self):
- pass
- # 登录接口 封装
- def api_login(self):
- pass
- # 查询登录状态接口 封装
- def api_login_status(self):
- pass
复制代码 加上__,只能在当千模块里调用,其他py文件调用不了

- # 获取图片验证码接口 封装
- def api_img_code(self, random):
- # 调用get方法,返回响应对象
- res = self.session.get(url=self.__url_img_code.format(random))
- return res
- # 获取短信验证码接口 封装
- def api_phone_code(self, phone, imgVerifyCode):
- # 1、定义请求参数
- data = {
- "phone": phone,
- "imgVerifyCode": imgVerifyCode,
- "type": "reg"
- }
- # 2、调用请求方法
- res = self.session.post(url=self.__url_phone_code, data=data)
- return res
- # 注册接口 封装
- def api_register(self, phone, password, verifycode, phone_code):
- # 1、定义请求参数
- data = {
- "phone": phone,
- "password": password,
- "verifycode": verifycode,
- "phone_code": phone_code,
- "dy_server": "on",
- "invite_phone": ""
- }
- # 2、调用请求方法
- res = self.session.post(url=self.__url_register, data=data)
- return res
- # 登录接口 封装
- def api_login(self, keywords, password):
- # 1、定义请求参数
- data = {
- "keywords": keywords,
- "password": password
- }
- # 2、调用请求方法
- res = self.session.post(url=self.__url_login, data=data)
- return res
- # 查询登录状态接口 封装
- def api_login_status(self):
- res = self.session.post(self.__url_login_status)
复制代码
- script(test01_register_login.py)
- import unittest
- import requests
- from api.api_register_login import ApiRegisterLogin
- class TestRegisterLogin(unittest.TestCase):
- # 初始化
- def setUp(self):
- # 获取session对象
- self.session = requests.session()
- # 获取ApiRegisterLogin实例
- self.reg = ApiRegisterLogin(self.session)
- # 结束
- def tearDown(self):
- # 关闭session对象
- self.session.close()
- # 获取图片验证码接口 测试
- def test01_img_code(self):
- pass
- # 获取短信验证码接口 测试
- def test02_phone_code(self):
- pass
- # 注册接口测试
- def test03_register(self):
- pass
- # 登录接口 测试
- def test04_login(self):
- pass
- # 查询登录状态 测试
- def test05_login_status(self):
- pass
复制代码
- import unittest
- import requests
- from api.api_register_login import ApiRegisterLogin
- class TestRegisterLogin(unittest.TestCase):
- # 初始化
- def setUp(self):
- # 获取session对象
- self.session = requests.session()
- # 获取ApiRegisterLogin实例
- self.reg = ApiRegisterLogin(self.session)
- # 结束
- def tearDown(self):
- # 关闭session对象
- self.session.close()
- # 获取图片验证码接口 测试
- def test01_img_code(self):
- # 1、调用图片验证码接口
- r = self.reg.api_img_code(123)
- # 2、查看响应状态码
- print(r.status_code)
- # 获取短信验证码接口 测试
- def test02_phone_code(self, phone=13600001111, imgVerifyCode=8888):
- # 1、调用获取图片验证码接口----目的:让session记住cookie
- self.reg.api_img_code(123)
- # 2、调用短信验证码接口
- r = self.reg.api_phone_code(phone=phone, imgVerifyCode=imgVerifyCode)
- # 3、查看响应结果
- print(r.json())
- # 注册接口测试
- def test03_register(self, phone=13600001111, imgVerifyCode=8888, password="test123", phone_code=666666):
- # 1、图片验证码接口
- self.reg.api_img_code(123)
- # 2、短信验证码接口
- self.reg.api_phone_code(phone=phone, imgVerifyCode=imgVerifyCode)
- # 3、注册接口
- r = self.reg.api_register(phone=phone, password=password, verifycode=imgVerifyCode, phone_code=phone_code)
- # 4、查看结果
- print(r.json())
- # 登录接口 测试
- def test04_login(self, keywords=13600001111, password="test123"):
- # 1、登录接口
- r = self.reg.api_login(keywords=keywords, password=password)
- # 2、查询结果
- print(r.json())
- # 查询登录状态 测试
- def test05_login_status(self, keywords=13600001111, password="test123"):
- # 1、调用登录接口
- self.reg.api_login(keywords=keywords, password=password)
- # 2、调用查询登录结果接口
- r = self.reg.api_login_status()
- print(r.json())
复制代码
阐明:判定程序实行实际结果是否符合预期结果
- 提示:
- 捕捉异常的目的是为了将错误信息记录下来,
- 捕捉信息完成后,必须抛出异常
- import unittest
- import requests
- from api.api_register_login import ApiRegisterLogin
- class TestRegisterLogin(unittest.TestCase):
- # 初始化
- def setUp(self):
- # 获取session对象
- self.session = requests.session()
- # 获取ApiRegisterLogin实例
- self.reg = ApiRegisterLogin(self.session)
- # 结束
- def tearDown(self):
- # 关闭session对象
- self.session.close()
- # 获取图片验证码接口 测试
- def test01_img_code(self):
- try:
- # 1、调用图片验证码接口
- r = self.reg.api_img_code(123)
- # 2、查看响应状态码
- self.assertEqual(200, r.status_code)
- except Exception as e:
- # 日志
- print(e)
- # 抛出异常
- raise
- # 获取短信验证码接口 测试
- def test02_phone_code(self, phone=13600001111, imgVerifyCode=8888, expect_text="发送成功"):
- try:
- # 1、调用获取图片验证码接口----目的:让session记住cookie
- self.reg.api_img_code(123)
- # 2、调用短信验证码接口
- r = self.reg.api_phone_code(phone=phone, imgVerifyCode=imgVerifyCode)
- # 3、查看响应结果
- self.assertIn(expect_text, r.text)
- except Exception as e:
- # 日志
- print(e)
- # 抛出异常
- raise
- # 注册接口测试
- def test03_register(self, phone=13600001111, imgVerifyCode=8888, password="test123", phone_code=666666, expect_text="注册成功"):
- try:
- # 1、图片验证码接口
- self.reg.api_img_code(123)
- # 2、短信验证码接口
- self.reg.api_phone_code(phone=phone, imgVerifyCode=imgVerifyCode)
- # 3、注册接口
- r = self.reg.api_register(phone=phone, password=password, verifycode=imgVerifyCode, phone_code=phone_code)
- # 4、查看结果
- self.assertIn(expect_text, r.text)
- except Exception as e:
- # 日志
- print(e)
- # 抛出异常
- raise
- # 登录接口 测试
- def test04_login(self, keywords=13600001111, password="test123", expect_text="登录成功"):
- try:
- # 1、登录接口
- r = self.reg.api_login(keywords=keywords, password=password)
- # 2、查询结果
- self.assertIn(expect_text, r.text)
- except Exception as e:
- # 日志
- print(e)
- # 抛异常
- raise
- # 查询登录状态 测试
- def test05_login_status(self, keywords=13600001111, password="test123", expect_text="OK"):
- try:
- # 1、调用登录接口
- self.reg.api_login(keywords=keywords, password=password)
- # 2、调用查询登录结果接口
- r = self.reg.api_login_status()
- self.assertIn(expect_text, r.text)
- except Exception as e:
- # 日志
- print(e)
- # 抛出异常
- raise
复制代码
1.编写数据存储文件json
2.编写读取工具方法 read_json()
3.利用参数化组件进行引用 parametrize
- 1 .编写参数化文件(register_login.json)
心得:
1、根据模块来新建json文件(1个模块1个json文件)
2、最外侧利用{},模块下几个接口,编写几个key,值为列表
3、列表值中,有几组数据,就写几个{}
4、每组数据{}中,组成格式:阐明+参数+预期结果
- 几个接口几个key,这里有五个接口(图片验证码、短信验证码、注册、登录、登录状态接口)
- 每组数据组成格式:阐明+参数+预期结果
- {
- "img_code": [
- {
- "desc": "获取图片验证码成功(随机小数)",
- "random": "0.123",
- "expect_code": 200
- },
- {
- "desc": "获取图片验证码成功(随机整数)",
- "random": "123",
- "expect_code": 200
- },
- {
- "desc": "获取图片验证码成功(随机数为空)",
- "random": "",
- "expect_code": 404
- },
- {
- "desc": "获取图片验证码成功(随机数为字符串)",
- "random": "heloo123",
- "expect_code": 400
- }
- ],
- "phone_code": [
- {
- "desc": "获取短信验证码成功",
- "phone": "13600001111",
- "imgVerifyCode": "8888",
- "expect_text": "发送成功"
- },{
- "desc": "获取短信验证码失败",
- "phone": "13600001111",
- "imgVerifyCode": "8889",
- "expect_text": "验证码错误"
- }
- ],
- "register": [
- {
- "desc": "注册成功(必填参数)",
- "phone": "13600001111",
- "password": "test123",
- "verifycode": 8888,
- "phone_code": 666666,
- "dy_server": "on",
- "expect_text": "注册成功"
- },{
- "desc": "注册失败(图片验证码错误)",
- "phone": "13600001112",
- "password": "test123",
- "verifycode": 8889,
- "phone_code": 666666,
- "dy_server": "on",
- "expect_text": "验证码错误"
- },{
- "desc": "注册失败(短信验证码错误)",
- "phone": "13600001112",
- "password": "test123",
- "verifycode": 8888,
- "phone_code": 666667,
- "dy_server": "on",
- "expect_text": "验证码错误"
- },{
- "desc": "注册失败(手机号已存在)",
- "phone": "13600001111",
- "password": "test123",
- "verifycode": 8888,
- "phone_code": 666666,
- "dy_server": "on",
- "expect_text": "已存在"
- }
- ],
- "login": [
- {
- "desc": "登录成功",
- "keywords": "13600001111",
- "password": "test123",
- "expect_text": "登录成功"
- }, {
- "desc": "登录失败(密码为空)",
- "keywords": "13600001111",
- "password": "",
- "expect_text": "不能为空"
- }, {
- "desc": "登录失败(解锁)",
- "keywords": "13600001111",
- "password": "errror123",
- "expect_text": "登录成功"
- }
- ],
- "login_status": [
- {
- "desc": "查询登录状态(已登录)",
- "status": "已登录",
- "expect_text": "OK"
- }, {
- "desc": "查询登录状态(未登录)",
- "status": "未登录",
- "expect_text": "未登"
- }
- ]
- }
复制代码
- from config import DIR_PATH
- def read_json(filename, key):
- # 拼接读取文件的完整路径 os.sep动态获取/ 还是 \ ,根据电脑的操作系统
- file_path = DIR_PATH + os.sep + "data" + os.sep + filename
- arr = []
- with open(file_path, 'r', encoding="utf-8") as f:
- # print(json.load(f))
- # print(json.load(f).get(key)) [{},{},{}]----->[(),(),()]
- for data in json.load(f).get(key):
- arr.append(tuple(data.values())[1:])
- print(arr)
- if __name__ == '__main__':
- read_json("register_login.json", "img_code")
- # [('0.123', 200), ('123', 200), ('', 404), ('heloo123', 400)]
复制代码
- # 登录接口 测试
- @parameterized.expand(read_json("register_login.json", "login"))
- def test04_login(self, keywords, password, expect_text):
- try:
- i = 1
- if "error" in password:
- while i <= 3:
- r = self.reg.api_login(keywords=keywords, password=password)
- i += 1
- # 断言锁定
- print("测试锁定:", r.text)
- self.assertIn("锁定", r.text)
- # 暂停60s
- sleep(60)
- # 测试登录成功
- r = self.reg.api_login(keywords="13600001111", password="test123")
- self.assertIn(expect_text, r.text)
- # 1、登录接口
- r = self.reg.api_login(keywords=keywords, password=password)
- # 2、查询结果
- self.assertIn(expect_text, r.text)
- except Exception as e:
- # 日志
- print(e)
- # 抛异常
- raise
复制代码
- # 查询登录状态 测试
- @parameterized.expand(read_json("register_login.json", "login_status"))
- def test05_login_status(self, status, expect_text):
- try:
- if status == "已登录":
- # 1、调用登录接口
- self.reg.api_login(keywords="13600001111", password="test123")
- # 2、调用查询登录结果接口
- r = self.reg.api_login_status()
- self.assertIn(expect_text, r.text)
- except Exception as e:
- # 日志
- print(e)
- # 抛出异常
- raise
复制代码
- 断言代码示例:(test01_register_login.py)
- import unittestimport requestsfrom api.api_register_login import ApiRegisterLoginfrom parameterized import parameterizedfrom util import read_jsonfrom time import sleepclass TestRegisterLogin(unittest.TestCase): # 初始化 def setUp(self): # 获取session对象 self.session = requests.session() # 获取ApiRegisterLogin实例 self.reg = ApiRegisterLogin(self.session) # 竣事 def tearDown(self): # 关闭session对象 self.session.close() # 获取图片验证码接口 测试 @parameterized.expand(read_json("register_login.json", "img_code")) def test01_img_code(self, random, expect_code): try: # 1、调用图片验证码接口 r = self.reg.api_img_code(random) # 2、检察响应状态码 self.assertEqual(expect_code, r.status_code) except Exception as e: # 日记 print(e) # 抛出异常 raise # 获取短信验证码接口 测试 @parameterized.expand(read_json("register_login.json", "phone_code")) def test02_phone_code(self, phone, imgVerifyCode, expect_text): try: # 1、调用获取图片验证码接口----目的:让session记住cookie self.reg.api_img_code(123) # 2、调用短信验证码接口 r = self.reg.api_phone_code(phone=phone, imgVerifyCode=imgVerifyCode) # 3、检察响应结果 self.assertIn(expect_text, r.text) except Exception as e: # 日记 print(e) # 抛出异常 raise # 注册接口测试 @parameterized.expand(read_json("register_login.json", "register")) def test03_register(self, phone, password, imgVerifyCode, phone_code, expect_text): try: # 1、图片验证码接口 self.reg.api_img_code(123) # 2、短信验证码接口 self.reg.api_phone_code(phone=phone, imgVerifyCode=imgVerifyCode) # 3、注册接口 r = self.reg.api_register(phone=phone, password=password, verifycode=imgVerifyCode, phone_code=phone_code) # 4、检察结果 self.assertIn(expect_text, r.text) except Exception as e: # 日记 print(e) # 抛出异常 raise # 登录接口 测试
- @parameterized.expand(read_json("register_login.json", "login"))
- def test04_login(self, keywords, password, expect_text):
- try:
- i = 1
- if "error" in password:
- while i <= 3:
- r = self.reg.api_login(keywords=keywords, password=password)
- i += 1
- # 断言锁定
- print("测试锁定:", r.text)
- self.assertIn("锁定", r.text)
- # 暂停60s
- sleep(60)
- # 测试登录成功
- r = self.reg.api_login(keywords="13600001111", password="test123")
- self.assertIn(expect_text, r.text)
- # 1、登录接口
- r = self.reg.api_login(keywords=keywords, password=password)
- # 2、查询结果
- self.assertIn(expect_text, r.text)
- except Exception as e:
- # 日志
- print(e)
- # 抛异常
- raise
- # 查询登录状态 测试
- @parameterized.expand(read_json("register_login.json", "login_status"))
- def test05_login_status(self, status, expect_text):
- try:
- if status == "已登录":
- # 1、调用登录接口
- self.reg.api_login(keywords="13600001111", password="test123")
- # 2、调用查询登录结果接口
- r = self.reg.api_login_status()
- self.assertIn(expect_text, r.text)
- except Exception as e:
- # 日志
- print(e)
- # 抛出异常
- raise
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |