当前位置: 首页 > news >正文

沈阳网站优化推广方案o2o模式成功案例

沈阳网站优化推广方案,o2o模式成功案例,怎么制作动态的网站,旅游网站开发文档前言 httprunner 用 yaml 文件实现接口自动化框架很好用,最近在看 pytest 框架,于是参考 httprunner的用例格式,写了一个差不多的 pytest 版的简易框架 项目结构设计 项目结构完全符合 pytest 的项目结构,pytest 是查找 test_.…

前言

httprunner 用 yaml 文件实现接口自动化框架很好用,最近在看 pytest 框架,于是参考 httprunner的用例格式,写了一个差不多的 pytest 版的简易框架

项目结构设计

项目结构完全符合 pytest 的项目结构,pytest 是查找 test_.py 文件,我这里是查找 test_.yml 文件,唯一不同的就是这个地方
以前写test_*.py 的测试用例,现在完全不用写了,全部写yaml 文件就行,项目结构参考

只需在 conftest.py 即可实现,代码量超级少
pytest 7.x最新版

def pytest_collect_file(parent, file_path):# 获取文件.yml 文件,匹配规则if file_path.suffix == ".yml" and file_path.name.startswith("test"):return YamlFile.from_parent(parent, path=file_path)

pytest 5.x以上版本

import pytest
import requestsdef pytest_collect_file(parent, path):# 获取文件.yml 文件,匹配规则if path.ext == ".yml" and path.basename.startswith("test"):# print(path)# print(parent)# return YamlFile(path, parent)return YamlFile.from_parent(parent, fspath=path)class YamlFile(pytest.File):# 读取文件内容def collect(self):import yamlraw = yaml.safe_load(self.fspath.open(encoding='utf-8'))for yaml_case in raw:name = yaml_case["test"]["name"]values = yaml_case["test"]yield YamlTest.from_parent(self, name=name, values=values)class YamlTest(pytest.Item):def __init__(self, name, parent, values):super(YamlTest, self).__init__(name, parent)self.name = nameself.values = valuesself.request = self.values.get("request")self.validate = self.values.get("validate")self.s = requests.session()def runtest(self):# 运行用例request_data = self.values["request"]# print(request_data)response = self.s.request(**request_data)print("\n", response.text)# 断言self.assert_response(response, self.validate)def assert_response(self, response, validate):'''设置断言'''import jsonpathfor i in validate:if "eq" in i.keys():yaml_result = i.get("eq")[0]actual_result = jsonpath.jsonpath(response.json(), yaml_result)expect_result = i.get("eq")[1]print("实际结果:%s" % actual_result)print("期望结果:%s" % expect_result)assert actual_result[0] == expect_result

pytest 4.x 以下版本

import pytest
import requests
# 作者-上海悠悠 QQ交流群:717225969
# blog地址 https://www.cnblogs.com/yoyoketang/def pytest_collect_file(parent, path):# 获取文件.yml 文件,匹配规则if path.ext == ".yml" and path.basename.startswith("test"):# print(path)# print(parent)return YamlFile(path, parent)class YamlFile(pytest.File):# 读取文件内容def collect(self):import yamlraw = yaml.safe_load(self.fspath.open(encoding='utf-8'))for yaml_case in raw:name = yaml_case["test"]["name"]values = yaml_case["test"]yield YamlTest(name, self, values)class YamlTest(pytest.Item):def __init__(self, name, parent, values):super(YamlTest, self).__init__(name, parent)self.name = nameself.values = valuesself.request = self.values.get("request")self.validate = self.values.get("validate")self.s = requests.session()def runtest(self):# 运行用例request_data = self.values["request"]# print(request_data)response = self.s.request(**request_data)print("\n", response.text)# 断言self.assert_response(response, self.validate)def assert_response(self, response, validate):'''设置断言'''import jsonpathfor i in validate:if "eq" in i.keys():yaml_result = i.get("eq")[0]actual_result = jsonpath.jsonpath(response.json(), yaml_result)expect_result = i.get("eq")[1]print("实际结果:%s" % actual_result)print("期望结果:%s" % expect_result)assert actual_result[0] == expect_result

断言这部分,目前只写了判断相等,仅供参考,支持jsonpath来提取json数据

yaml格式的用例

在项目的任意目录,只要是符合test_开头的yml文件,我们就认为是测试用例
test_login.yml的内容如下

- test:name: login case1request:url: http://49.235.x.x:7000/api/v1/login/method: POSTheaders:Content-Type: application/jsonUser-Agent: python-requests/2.18.4json:username: testpassword: 123456validate:- eq: [$.msg, login success!]- eq: [$.code, 0]- test:name: login case2request:url: 49.235.x.x:7000/api/v1/login/method: POSTheaders:Content-Type: application/jsonUser-Agent: python-requests/2.18.4json:username: testpassword: 123456validate:- eq: [$.msg, login success!]- eq: [$.code, 0]

运行用例

运行用例,完全符合pytest的只需用例风格,支持allure报告

pytest -v

D:\soft\api_pytest_1208>pytest -v
====================== test session starts ======================
platform win32 -- Python 3.6.6, pytest-4.5.0, py-1.9.0,
cachedir: .pytest_cache
rootdir: D:\soft\api_pytest_1208
plugins: allure-pytest-2.8.6
collected 4 itemsdata/test_login.yml::login case1 PASSED                    [ 25%]
data/test_login.yml::login case2 PASSED                    [ 50%]
data/test_login1.yml::login case1 PASSED                   [ 75%]
data/test_login1.yml::login case2 PASSED                   [100%]=================== 4 passed in 1.34 seconds ====================

allure报告

pytest --alluredir ./report

目前是把 yaml 文件下每个 test 当一个用例执行,后续还可以加上提取参数,参数关联更高级的功能!

这可能是B站最详细的pytest自动化测试框架教程,整整100小时,全程实战!!!

http://www.yayakq.cn/news/463033/

相关文章:

  • 怎么创建网站快捷方式自己做的网站设定背景图像
  • wordpress主题图片怎么换济南网站优化公司排名
  • 做门窗的建网站怎么赚钱WordPress实现登陆提醒
  • 免费门户网站开发电力行业企业网站建设
  • 网站的企业特色展示常用网站有哪些
  • 网站开发恶意索赔制作板块的网站
  • 怎样做网站的优化、排名做机票在线预订网站
  • 网站运营做的是什么工作如何禁用wordpress播放器
  • 网站建设实训记录天津网站建站公司
  • 自适应网站什么做wordpress 首页模板
  • 国际购物网站平台有哪些禁止wordpress获取隐私
  • 怀柔石家庄网站建设网站建设优化东莞
  • 那个网站可以帮助做数学题网站建设论文框架
  • 百度站长平台诊断wordpress is_user_logged_in
  • 苏州建站模板展示邢台经济开发区
  • 国外好的电商网站有哪些深圳兼职做网站
  • 深圳做网站的网站建设安全协议
  • 同城换物网站为什么做不起来网站建设的重要性意义与价值
  • 做网站下一页郴州网站建设软件定制开发制作
  • 做一些好玩的个人网站福建省文明建设办公室网站
  • 常州网站制作公司多吗关于协会网站建设的建议
  • 成都公司做网站多少钱威海建设集团信息网站
  • 珠海制作公司网站网站建设工程师面试
  • 北京门户企业网站建设长春新建火车站
  • 网站做全景北京专业做网站电话
  • 如何做网站二级域名wordpress更改主站点
  • 怎么建投票网站网页设计与制作广东开放大学
  • 帝国cms影视网站模板官方网站建设 找磐石网络一流
  • 网站建设合同印花税税率html手机版网站
  • 网站建设哪家好首选万维科技快看点自媒体平台注册入口和下载