接口自动化项目落地之HTTPBin网站
本书作者:测试开发刚哥,文章版权归个人所有,未经授权请勿转载。 接口自动化项目落地系列 找个开源网站或开源项目,用tep实现整套pytest接口自动化项目落地,归档到电子书,作为tep完整教程的项目篇一部分。自从tep完整教程发布以后,tep被越来越多小伙伴了解。教程只是纯理论,是骡子是马,拉出来遛遛才知道。做接口自动化项目落地,一方面是为了让自己脑海中的构想实实在在的呈现出来,现实和理想存在多少差距,不断尝试去弥补和修缮;另一方面也是方便读者朋友们学习使用,借助实际项目来练习,才能在赛道中弯道超车。 HTTPBin网站 httpbin.org是一个简单的在线提供HTTP服务的网站: 它能够用来对HTTP进行在线测试。 测试报告 HTTPBin网站的接口自动化项目包含11个用例集: 67条测试用例: 自动化执行正确率98.5%,其中有1条错误结果,是我故意为之的,因为想展示下断言失败的效果。 环境配置 包含http和https两套环境,因为HTTPBin支持HTTP&HTTPS: fixtures/fixture_env_vars.py #!/usr/bin/python ## encoding=utf-8 from tep.fixture import * @pytest.fixture(scope="session") def env_vars(config): class Clazz(TepVars): env = config["env"] """变量定义开始""" # 环境变量 mapping = { "http": { # http环境 "domain": "http://httpbin.org", }, "https": { # https环境 "domain": "https://httpbin.org", } # 继续添加 } # 定义类属性,敲代码时会自动补全 domain = mapping[env]["domain"] """变量定义结束""" return Clazz() 配置默认为http环境: conf.yaml env: http 用例集 http-methods import allure from tep.client import request @allure.title("get请求") def test(env_vars): # 描述 # 数据 # 请求 response = request( "get", url=env_vars.domain + "/get", headers={'Host': 'httpbin.org', 'Proxy-Connection': 'keep-alive', 'accept': 'application/json', 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.109 Safari/537.36', 'Referer': 'http://httpbin.org/', 'Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7'}, params={} ) # 提取 # 断言 assert response.status_code < 400 auth ...