一、目的
测试人员在进行自动化脚本编写的时候可能不知道从哪里入手,通过GPT可以帮助测试人员上手自动化脚本的编写
二、地址
GPT的地址:http://chat.yuting360.cn/
三、使用案例
1.前提:我们使用chrome把我们需要的功能转化成了shell脚本,现在要转换成python语言实现,同时和现在的自动化框架结合
2.把我们的shell脚本扔给gpt,并让他帮我们以python格式输出,如图:
这个是gpt给出的代码回复
import requests
headers = {
‘Accept’: ‘application/json, text/plain, /’,
‘Accept-Language’: ‘zh-CN,zh;q=0.9,en;q=0.8’,
‘Cache-Control’: ‘no-cache’,
‘Connection’: ‘keep-alive’,
‘Content-Type’: ‘application/x-www-form-urlencoded’,
‘Pragma’: ‘no-cache’,
‘Referer’: ‘https://192.168.190.222/infrastructure/compute/hostAndCluster’,
‘Sec-Fetch-Dest’: ‘empty’,
‘Sec-Fetch-Mode’: ‘cors’,
‘Sec-Fetch-Site’: ‘same-origin’,
‘User-Agent’: ‘Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36’,
‘sec-ch-ua’: ‘“Not.A/Brand”;v=“8”, “Chromium”;v=“114”, “Google Chrome”;v=“114”’,
‘sec-ch-ua-mobile’: ‘?0’,
‘sec-ch-ua-platform’: ‘“macOS”’,
‘token’: ‘750ecff3-6e96-4c50-a594-474a9aaed2d3’,
}
params = (
(‘clusterId’, ‘9’),
(‘usageType’, ‘3’),
)
response = requests.get(‘https://192.168.190.222/hypercenter/statistics/getVmTopNUsageDataByClusterId’, headers=headers, params=params, verify=False)
print(response.json())
3.然后我们让跟我们的框架结合,由于我们的自动化框架使用的是pytest+alllure的形式,所以,下图是和gpt的交互:
如下是gpt给出的代码:
好的,下面是使用pytest和allure的示例代码:
- 安装pytest和allure-pytest
pip install pytest
pip install allure-pytest
- 创建测试文件test_api.py
import pytest
import requests
import allure
@pytest.fixture()
def headers():
headers = {
'Accept': 'application/json, text/plain, */*',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
'Content-Type': 'application/x-www-form-urlencoded',
'Pragma': 'no-cache',
'Referer': 'https://192.168.190.222/infrastructure/compute/hostAndCluster',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-origin',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36',
'sec-ch-ua': '"Not.A/Brand";v="8", "Chromium";v="114", "Google Chrome";v="114"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"macOS"',
'token': '750ecff3-6e96-4c50-a594-474a9aaed2d3',
}
return headers
@pytest.mark.parametrize("clusterId, usageType", [(9, 3), (10, 4)])
def test_api(headers, clusterId, usageType):
with allure.step("发送请求"):
params = {
'clusterId': clusterId, 'usageType': usageType}
response = requests.get(
'https://192.168.190.222/hypercenter/statistics/getVmTopNUsageDataByClusterId',
headers=headers,
params=params,
verify=False
)
with allure.step("断言响应状态码"):
assert response.status_code == 200
with allure.step("断言响应数据"):
assert response.json()['code'] == 0
if __name__ == '__main__':
pytest.main()
- 运行测试并生成allure报告
在终端中运行以下命令:
pytest --alluredir=./allure-results
这将在当前目录下生成一个allure-results文件夹,并在其中保存测试结果。
- 查看allure报告
运行以下命令,以在默认浏览器中打开allure报告:
allure serve ./allure-results
这将在浏览器中打开allure报告,您可以在其中查看测试结果和测试执行步骤。
之后我们将给出的第二步拷贝保存成py文件就可以运行了,我们的自动化脚本就初步实现了,在根据需求进行更改就可以了
4.其他的一些需求也可以通过GPT来辅助我们实现,比如我们之前的shell脚本想增加一些并发操作等,都可以跟gpt说,如图:
gpt返回的结果:
您需要一个脚本来定时执行该命令,可以使用crontab来实现。
-
打开终端,输入 crontab -e,回车。如果是第一次使用,会提示选择编辑器。
-
在编辑器中加入以下内容:
*/5 * * * * curl ‘https://192.168.190.222/api/openstack/regionone/cinder/v3/48f0dd9acba84b088f5b85b09305a3e8/types’
-H ‘Accept: application/json, text/plain, /’
-H ‘Accept-Language: zh-CN,zh;q=0.9,en;q=0.8’
-H ‘Cache-Control: no-cache’
-H ‘Connection: keep-alive’
-H ‘Openstack-Api-Version: volume 3.59’
-H ‘Pragma: no-cache’
-H ‘Referer: https://192.168.190.222/base/overview-admin’
-H ‘Sec-Fetch-Dest: empty’
-H ‘Sec-Fetch-Mode: cors’
-H ‘Sec-Fetch-Site: same-origin’
-H ‘User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36’
-H ‘X-Auth-Token: gAAAAABkjmLu8CeOOTFDUIW7GEN074d4rgXk4eD5jtnWxqhAhaRnhNMv1jsikQpvPgJjUGy3kWLAb1P6NCDnYm67d6sM5g4au_eTnxQH-na0heReGOnezZaUQ7YpK0Wr5x-FVu3dd4b-NF4Ydhf0VYyYPVvQDbK2Vi-efoxY5ouqvgwPcUp9ZsbF_l_jTg92OhccxYoSR-w1’
-H ‘sec-ch-ua: “Not.A/Brand”;v=“8”, “Chromium”;v=“114”, “Google Chrome”;v=“114”’
-H ‘sec-ch-ua-mobile: ?0’
-H ‘sec-ch-ua-platform: “macOS”’
-H ‘token: 5106b47f-1bca-4e84-9baf-d7678273a8f8’
–compressed
–insecure
- 保存文件并退出编辑器。
这样,每5分钟执行一次该命令并输出结果。