def run_test(): """ 定义运行测试的函数 :return: """ # 数据初始化 db = DB() with open('datas.ymal', 'r') as fr: datas = yaml.load(fr) db.init_data(datas) test_dir = '.' report_dir = "./reports/" # 执行测试 discovery = unittest.defaultTestLoader.discover( test_dir, pattern="test_django_restful.py", ) now = time.strftime("%Y-%m-%d_%H_%M_%S") report_path = report_dir + now + "test_report.html" with open(report_path, 'wb') as fw: runner = BSTestRunner(stream=fw, title="Django_api_test", description="Django_restful_API_Test_rport") logging.info("=========Start API Test==========") runner.run(discovery) logging.info("=========Finish API Test==========")
class GroupTest(unittest.TestCase): def setUp(self): self.base_url = 'http://127.0.0.1:8000/groups' self.auth = ('2018zxw', 'zxw20182018') # def test_001_get_group(self): logging.info('test_001_get_group') r = requests.get(self.base_url + '/1/', auth=self.auth) result = r.json() self.assertEqual(result['name'], 'Developer') def test_002_add_group(self): logging.info('test_002_add_group') from_data = {'name': 'Pm'} r = requests.post(self.base_url + '/', data=from_data, auth=self.auth) result = r.json() self.assertEqual(result['name'], 'Pm') def test_003_update_group(self): logging.info('test_003_update_group') from_data = {'name': 'Leader'} r = requests.patch(self.base_url + '/2/', data=from_data, auth=self.auth) result = r.json() self.assertEqual(result['name'], 'Leader') # def test_004_delete_group(self): logging.info('test_004_delete_group') r = requests.delete(self.base_url + '/1/', auth=self.auth) self.assertEqual(r.status_code, 204) def test_005_no_auth(self): logging.info('test_005_no_auth') r = requests.get(self.base_url) result = r.json() self.assertEqual(result['detail'], 'Authentication credentials were not provided.') # """调用mysql_action的DB()方法""" db = DB() f = open('datas.yaml', 'r') datas = yaml.load(f) db.init_data(datas)
# -*- coding: utf-8 -*- # __author__ = "yw" import unittest from BSTestRunner import BSTestRunner import time, yaml from mysql_action import DB import logging.config CON_LOG = 'log.conf' logging.config.fileConfig(CON_LOG) logging = logging.getLogger() # 数据初始化操作 db = DB() f = open('datas.yaml', 'r') datas = yaml.load(f) db.init_data(datas) # 指定测试用例和测试报告的路径 test_dir = '.' report_dir = './reports' #加载测试用例 discover = unittest.defaultTestLoader.discover( test_dir, pattern='test_django_restful.py') # 定义报告的文件格式 # now = time.strftime("%Y-%m-%d %H_%M_%S") now = time.strftime("%Y%m%d%H%M%S") report_name = report_dir + '/' + now + 'test_report.html'
def test_1(self): '''数据初始化''' self.db = DB() self.db.test_cz()
class UserTest(unittest.TestCase): #定义一个类继承unittest def setUp(self): #初始方法 self.base_url = data['api']['ip'] #接口地址 self.auth = (data['auth']['zhhao'], data['auth']['mm']) #登陆账号,密码 def test_1(self): '''数据初始化''' self.db = DB() self.db.test_cz() def test_001_no_auth2(self): '''没有登陆的查询的用例''' logging.info('test_001_no_auth2') self.b = Jk() r = self.b.get(self.base_url) result = r.json() self.assertEqual(result['detail'], data['test_001_no_auth2']['detail']) #断言 def test_002_add_user3(self): '''添加数据用例''' logging.info('test_002_add_user3') form_data = { 'username': data['test_002_add_user3']['username'], 'email': data['test_002_add_user3']['email'] } self.b = Jk() a = data['a1']['a'] r = self.b.post(self.base_url, form_data, self.auth, a) result = r.json() self.assertEqual(result['username'], data['test_002_add_user3']['username']) self.assertEqual(result['email'], data['test_002_add_user3']['email']) # def test_003_update_user4(self): '''更新email用例''' logging.info('test_003_update_user4') form_data = {'email': data['test_003_update_user4']['email']} self.b = Jk() a = data['a1']['a1'] r = self.b.patch(self.base_url, form_data, self.auth, a) result = r.json() self.assertEqual(result['email'], data['test_003_update_user4']['email']) def test_004_delete_user5(self): '''删除数据的用例''' logging.info('test_004_delete_user5') self.b = Jk() a = data['a1']['a2'] r = self.b.delete(self.base_url, self.auth, a) #r=requests.delete(self.base_url+'/2/',auth=self.auth) self.assertEqual(r.status_code, 204) def test_005_get_user6(self): '''查看数据的用例''' logging.info('test_005_get_user6') self.b = Jk() a = data['a1']['a1'] r = self.b.get1(self.base_url, self.auth, a) #r=requests.get(self.base_url+'/1/',auth=self.auth) result = r.json() self.assertEqual(result['username'], data['test_005_get_user6']['username'])