用python的requests 框架 unittest 实现
自动化测试,run_test.sh脚本启动,外部传需要测试的service服务的url前缀(ip+端口)
项目目录结构如下图:
代码:
spec.py:
#! /usr/bin/python3
import unittest
import json
import os
import os.path as p
import requests
URL = os.environ['TARGET_HOST']
class Spec(unittest.TestCase):
def test_find_all_services(self):
response = self.send('/services',
'GET',
expacted_status=200)
self.assertEqual(20, len(json.loads(response.text)))
def test_find_all_services_by_name(self):
response = self.send('/services',
'GET',
params={'name': 'customer profile service'},
expacted_status=200)
result = json.loads(response.text)
self.assertEqual(1, len(result))
self.assertEqual(23, result[0]['id'])
self.assertTrue('customer profile service' in result[0]['name'].lower())
def test_find_all_services_by_tag(self):
response = self.send('/services',
'GET',
params={'tag':'Oem'},
expacted_status=200)
result = json.loads(response.text)
self.assertEqual(2, len(result))
self.assertTrue('Oem'.lower() in (result[0]['tags']).lower())
self.assertTrue('Oem'.lower() in (result[1]['tags']).lower())
def test_find_all_services_by_contactName(self):
response = self.send('/services',
'GET',
params={'contactName':'zhu'},
expacted_status=200)
result = json.loads(response.text)
self.assertEqual(2, len(result))
self.assertTrue('zhu' in (result[0]['contactName']).lower())
self.assertTrue('zhu' in (result[1]['contactName']).lower())
def test_find_all_services_by_category(self):
response = self.send('/services',
'GET',
params={'category':'other'},
expacted_status=200)
result = json.loads(response.text)
self.assertEqual(2, len(result))
self.assertTrue('other' in (result[0]['categorys']).lower())
def test_find_one_services(self):
response = self.send('/services/20',
'GET',
expacted_status=200)
body = json.loads(response.text)
self.assertEqual(20, body['id'])
def test_creat_services(self):
service_data = {
"id": 2000,
"name": "Customer Profile Serivce [New Created]",
"contactName": None,
"contactEmail": None,
"tags": "aa,dd",
"categorys": "cc,ff",
"valueProposition": "Provide O2O business product info.",
"businessScenario": "xxx",
"serviceSummary": "test1,test2",
"domainModel": None
}
self.send('/services',
'POST',
json=service_data,
expacted_status=201)
self.send('/services/2000',
'GET',
expacted_status=200)
def test_update_services(self):
service_data = {
"id": 24,
"name": "Customer Profile Serivce [New update]",
"contactName": None,
"contactEmail": None,
"tags": "aa,dd",
"categorys": "Aftersales",
"valueProposition": "Provide O2O business product info.",
"businessScenario": "xxx",
"serviceSummary": "test1,test2",
"domainModel": None
}
self.send('/services/24',
'PUT',
json=service_data,
expacted_status=200)
response = self.send('/services/24',
'GET',
expacted_status=200)
body = json.loads(response.text)
self.assertEqual(24, body['id'])
self.assertEqual("Customer Profile Serivce [New update]", body['name'])
def test_delete_services(self):
response = self.send('/services/2000',
'DELETE',
expacted_status=200)
response = self.send('/services/2000',
'GET',
expacted_status=404)
def test_check_upload_file(self):
f = open(self.__get_file_path__(), 'rb')
self.send('/services/23/domainmodel',
'POST',
files={'file': f},
expacted_status=200)
f.close()
def __get_file_path__(self):
current = p.dirname(p.abspath(__file__))
return p.join(current, 'domainmodel/example.png')
def send(self, url, method, params={}, json={},files={}, expacted_status=-1):
url = "{}/service-masterdata{}".format(URL, url)
if method == 'GET':
response = requests.get(url, params=params)
if method == 'POST':
response = requests.post(url, files=files, json=json)
if method == 'PUT':
response = requests.put(url, json=json)
if method == 'DELETE':
response = requests.delete(url)
self.assertEqual(expacted_status, response.status_code)
return response
if __name__ == '__main__':
unittest.main()
run_tests.sh:
#! /bin/bash
PROD=prod
UAT=uat
SIT=sit
if [ "$PROD" == "$1" ]
then
export TARGET_HOST=${PROD_HOST}
elif [ "$UAT" == "$1" ]
then
export TARGET_HOST=${UAT_HOST}
elif [ "$SIT" == "$1" ]
then
export TARGET_HOST=${SIT_HOST}
else
export TARGET_HOST=http://localhost:8084
fi
echo "Test against the host: $TARGET_HOST"
python3 -m unittest discover . -p "spec.py"
运行方式:进入项目目录
./ run_tests.sh xxxx:port
参数是环境的参数,脚本会根据环境参数,去读取相关的环境变量的值,从而获取target-host;target-host存在环境变量中;
本测试效果:脚本不带参数 模式localhost服务