一、考试系统
1. 导入类
import unittest
import requests
from public.get_exam_cookie import get_exam_cookie
import time
2. 用例1-考试系统后台-登录
class MyTestCase(unittest.TestCase):
def test_008_exam_login(self):
'''考试系统后台-登录'''
url = 'http://182.92.178.83:8088/api/user/login'
payload_login = {"userName": "admin", "password": "123456", "remember": False}
login = requests.post(url, json=payload_login, timeout=30)
self.assertEqual('成功',login.json()['message'])
3. 用例2-考试系统后台-用户管理-管理员-添加
def test_009_new_admin(self):
'''考试系统后台-用户管理-管理员-添加'''
url = 'http://182.92.178.83:8088/api/admin/user/edit'
global now_time
now_time = int(time.time())
payload_new_admin ={"id":'null',
"userName":'大白猫%d'%now_time,
"password":now_time,
"realName":"大白猫",
"role":3,"status":1,"age":"2","sex":"1",
"birthDay":'2020-11-09 00:00:00',
"phone":'13434544567'}
global headers_admin
headers_admin ={'Cookie':get_exam_cookie()}
new_admin = requests.post(url,json=payload_new_admin,headers =headers_admin ,timeout =30)
response = new_admin.json()
global admin_id
admin_id = response['response']['id']
self.assertEqual('成功',response['message'])
global exp_userName
exp_userName = payload_new_admin['userName']
act_userName = response['response']['userName']
self.assertEqual(exp_userName, act_userName)
4. 用例3-考试系统后台-用户管理-管理员列表-查询
def test_010_admin_search(self):
'''考试系统后台-用户管理-管理员列表-查询'''
url = 'http://182.92.178.83:8088/api/admin/user/page/list'
payload_search = {"userName":exp_userName,"role":3,"pageIndex":1,"pageSize":10}
search = requests.post(url, json=payload_search, headers=headers_admin, timeout=30)
response = search.json()
self.assertEqual('成功', response['message'])
exp_search_name = payload_search['userName']
act_search_name = response['response']['list'][0]['userName']
self.assertEqual(exp_search_name, act_search_name)
5. 用例4-考试系统后台-用户管理-管理员-编辑
def test_011_admin_edit(self):
'''考试系统后台-用户管理-管理员-编辑'''
url = 'http://182.92.178.83:8088/api/admin/user/edit'
payload_admin_edit = {"id":admin_id,
"userUuid":"",
"userName":'胖白%d'%now_time,
"realName":"胖白",
"age":'3', "role":3,"sex":'2',
"birthDay":"2020-12-09 00:00:00",
"phone":'13412345567',
"lastActiveTime":"","createTime":"","modifyTime":"","status":1,
"userLevel":'null',
"imagePath":'null',
"password":"admin%d"%now_time}
admin_edit = requests.post(url,json=payload_admin_edit,headers=headers_admin ,timeout=30)
response = admin_edit.json()
self.assertEqual('成功', response['message'])
exp_userName = payload_admin_edit['userName']
act_userName = response['response']['userName']
self.assertEqual(exp_userName, act_userName)
6. 用例5-考试系统后台-用户管理-管理员-更改状态
def test_012_change_status(self):
'''考试系统后台-用户管理-管理员-更改状态'''
url = 'http://182.92.178.83:8088/api/admin/user/changeStatus/'+str(admin_id)
change_status = requests.post(url,headers=headers_admin ,timeout=30)
response = change_status.json()
self.assertEqual('成功',response['message'] )
7. 用例6-考试系统后台-用户管理-管理员-删除
def test_013_admin_delete(self):
'''考试系统后台-用户管理-管理员-删除'''
url = 'http://182.92.178.83:8088/api/admin/user/delete/'+str(admin_id)
admin_delete = requests.post(url,headers=headers_admin ,timeout=30)
response = admin_delete.json()
self.assertEqual('成功', response['message'])
8. get_exam_cookie.py
import requests
def get_exam_cookie():
url = 'http://182.92.178.83:8088/api/user/login'
payload_login = {"userName": "admin", "password": "123456", "remember": False}
login = requests.post(url, json=payload_login, timeout=30)
dict_cookie = requests.utils.dict_from_cookiejar(login.cookies)
cookie = 'SESSION='+dict_cookie['SESSION']
return cookie
二、V部落
1. 导入类
import unittest
import requests
from public.get_vblog_cookie import get_vblog_cookie
import time
2. 用例1-V部落-登录
class MyTestCase(unittest.TestCase):
def test_001_login_vblog(self):
'''V部落-登录'''
url = 'http://182.92.178.83:8081/login'
payload = {'username': 'sang', 'password': '123'}
login = requests.post(url, data=payload,timeout=30)
exp = '登录成功'
act = login.json()['msg']
self.assertEqual(exp, act)
3. 用例2-V部落-发表文章
def test_002_new_article(self):
'''V部落-发表文章'''
url1 = 'http://182.92.178.83:8081/article/'
global now_time
now_time = int(time.time())
payload = {'id': '-1', 'title': '咕噜咕噜%d' % now_time, 'mdContent': '春眠不觉晓',
'htmlContent': '', 'cid': '62','state': '1', 'dynamicTags': '猫'}
global vblog_headers
vblog_headers = {'Cookie': get_vblog_cookie()}
requests.post(url1, data=payload, headers=vblog_headers, timeout=30)
url2 = 'http://182.92.178.83:8081/article/all'
result = requests.get(url2, headers=vblog_headers, timeout=30)
exp = payload['title']
act = '123'
for chaxun_info in result.json()['articles']:
if chaxun_info['title'] == exp:
global article_id
article_id = chaxun_info['id']
act = chaxun_info['title']
self.assertEqual(exp, act)
4. 用例3-V部落-文章列表-已发表-编辑
def test_003_edit_atrtical(self):
'''V部落-文章列表-已发表-编辑'''
url = 'http://182.92.178.83:8081/article/'
global payload_edit
payload_edit = {'id': article_id, 'title': '胖白%d' % now_time, 'mdContent': '处处闻啼鸟',
'htmlContent': '', 'cid': '62', 'state': '1', 'dynamicTags': '大白猫'}
requests.post(url, data=payload_edit, headers=vblog_headers)
url2 = 'http://182.92.178.83:8081/article/all'
result = requests.get(url2, headers=vblog_headers, timeout=30)
exp = payload_edit['title']
act = '123'
for chaxun_info2 in result.json()['articles']:
if chaxun_info2['title'] == exp:
act = chaxun_info2['title']
self.assertEqual(exp, act)
5. 用例4-V部落-文章列表-已发表-删除
def test_004_delete_atrtical(self):
'''V部落-文章列表-已发表-删除'''
url = 'http://182.92.178.83:8081/article/dustbin'
payload3 = {'aids': article_id, 'state': '1'}
requests.put(url, data=payload3, headers=vblog_headers, timeout=30)
url2 = 'http://182.92.178.83:8081/admin/article/all?page=1&count=6&keywords='
result_delete = requests.get(url2, headers=vblog_headers, timeout=30)
act =True
delete_title = payload_edit['title']
for chaxun_info in result_delete.json()['articles']:
if chaxun_info['title'] == delete_title:
act=False
else:
continue
self.assertEqual(True, act)
6. 用例5-V部落-栏目管理-新增栏目
def test_005_new_catenames(self):
'''V部落-栏目管理-新增栏目'''
url = 'http://182.92.178.83:8081/admin/category/'
payload = {'cateName': 'lxh%d' % now_time}
requests.post(url, data=payload, headers=vblog_headers)
url_chaxun = 'http://182.92.178.83:8081/admin/category/all'
chaxun = requests.get(url_chaxun, headers=vblog_headers)
exp = payload['cateName']
act = '123'
for catenameinfo in chaxun.json():
if catenameinfo['cateName'] == exp:
global cateName_id
cateName_id = catenameinfo['id']
act = catenameinfo['cateName']
self.assertEqual(exp, act)
7. 用例6-V部落-栏目管理-编辑栏目
def test_006_edit_catename(self):
'''V部落-栏目管理-编辑栏目'''
url = 'http://182.92.178.83:8081/admin/category/'
payload_edit_catename = {'id':cateName_id,'cateName':'啊啊啊啊%d'%now_time}
requests.put(url,data=payload_edit_catename,headers=vblog_headers)
url_chaxun = 'http://182.92.178.83:8081/admin/category/all'
chaxun = requests.get(url_chaxun, headers=vblog_headers)
exp = payload_edit_catename['cateName']
act = 'abc'
for catenameinfo_edit in chaxun.json():
if catenameinfo_edit['id']==cateName_id:
act = catenameinfo_edit['cateName']
self.assertEqual(exp,act)
8. 用例7-V部落-栏目管理-删除栏目
def test_007_delete_catename(self):
'''V部落-栏目管理-删除栏目'''
url = 'http://182.92.178.83:8081/admin/category/'+str(cateName_id)
requests.delete(url,headers=vblog_headers)
url = 'http://182.92.178.83:8081/admin/category/all'
result_delete = requests.get(url,headers=vblog_headers)
act = True
delete_catename_id = cateName_id
for chaxun_info in result_delete.json():
if chaxun_info['id'] == delete_catename_id:
act = False
else:
continue
self.assertEqual(True, act)
9. get_vblog_cookie.py
import requests
def get_vblog_cookie():
url = 'http://182.92.178.83:8081/login'
payload = {'username': 'sang', 'password': '123'}
login = requests.post(url, data=payload)
cookies = login.cookies
dict_cookie = requests.utils.dict_from_cookiejar(cookies)
cookie = 'JSESSIONID=' + dict_cookie['JSESSIONID']
return cookie
三、testrunner.py
import unittest
import time
import os,sys
from report.HTMLTestRunner import HTMLTestRunner
dirname,filename=os.path.split(os.path.abspath(sys.argv[0]))
print(dirname,filename)
case_path = ".\\case\\"
result = dirname+"\\report\\"
def Creatsuite():
testunit = unittest.TestSuite()
discover = unittest.defaultTestLoader.discover(case_path, pattern='*.py', top_level_dir=None)
for test_suite in discover:
for casename in test_suite:
testunit.addTest(casename)
return testunit
test_case = Creatsuite()
now = time.strftime('%Y-%m-%d-%H_%M_%S', time.localtime(time.time()))
day = time.strftime('%Y-%m-%d', time.localtime(time.time()))
tdresult = result + day
if os.path.exists(tdresult):
filename = tdresult + "\\" + now + "_result.html"
fp = open(filename, 'wb')
runner = HTMLTestRunner(stream=fp,
title='测试报告',
description='执行情况:')
runner.run(test_case)
fp.close()
else:
os.mkdir(tdresult)
filename = tdresult + "\\" + now + "_result.html"
fp = open(filename, 'wb')
runner = HTMLTestRunner(stream=fp,
title='Selenium测试报告',
description='执行情况:')
runner.run(test_case)
fp.close()