def __init__(self): self.method = RunMethod() self.data = GetData() self.com_result = CompareResult() self.config = ReadConfig() self.depend = DependData() self.sendemail = SendEmail()
def __init__(self): self.logger = MyLog() self.readconfig = ReadConfig() self.send_mail = SendEmail() # self.env = ResetEnv() self.is_send = self.readconfig.get_email("is_send") # 测试报告基本信息 self.testers = "Roman" self.title = "元丁接口测试报告" self.description = "正式/测试环境:Develop,IP地址:%s" % self.readconfig.get_base_url( ) # print(self.description) # 导入TestCase目录下的全部测试用例 self.discover = unittest.defaultTestLoader.discover(test_case_path, pattern='test*.py') # 导入指定测试用例列表文件 self.case_list_file = case_list_path self.case_list_list = [] # print(self.case_list_list) # 重置测试环境 self.is_env = True
def __init__(self, file_name=None): # print("打开excel") self.open_excel = OperationExcel() self.set_excel = ReadConfig() if file_name: self.open_json = OperationJson(file_name) else: self.open_json = OperationJson()
def setUp(self): self.data = ReadTestData(file_name) self.hea_data = ReadTestData() self.http = HttpMethod() self.config = ReadConfig() self.log = MyLog() self.json = OperationJson() self.sheet = 'app_test_case' self.row = list(range(2, 20)) self.log.info(message="----------测试开始----------", name="test01_OrcLogin.py")
def setUp(self): # print('执行开始') self.data = ReadTestData(file_name) self.hea_data = ReadTestData() self.http = HttpMethod() self.config = ReadConfig() self.log = MyLog() self.json = OperationJson() self.sheet = 'app_test_fenxi' self.row = list(range(2, 30)) self.log.info(message="----------测试开始----------", name="test_piyue.py")
class DependData: def __init__(self): self.get_dep_data = GetDependData() self.data = GetData() self.config = ReadConfig() self.run_method = RunMethod() # 执行依赖测试,获取结果 def run_depedn_test(self, case_id): run_host = self.config.get_http() row = self.get_dep_data.in_case_id_get_depend_Case_id_lines(case_id) request_method = self.data.get_request_method(row) url = str(run_host) + str(self.data.get_request_url(row)) data = self.data.get_request_data(row) header = self.data.get_request_header(row, self.config.get_token()) # print(run_host) # print(row) # print(request_method) # print(url) # print(data) # print(header) ress = self.run_method.run_main(method=request_method, url=url, data=data, header=header) return ress # 根据依赖的key获取执行依赖case的响应数据,然后返回 def get_data_in_key(self, row, case_id): depent_key = self.data.get_depend_return_data_key(row) print(depent_key) res_depent_data = self.run_depedn_test(case_id) # print(res_depent_data) dict_json = json.loads(res_depent_data) json_exe = parse(depent_key) madle = json_exe.find(dict_json) depent_data = [match.value for match in madle][0] #print(depent_data) return depent_data
def openbrowser(self, driver): browser = ReadConfig().get_string('browser_type', 'browserName') logger.info("选择的浏览器为: %s 浏览器" % browser) if browser == 'Firefox': driver = webdriver.Firefox() logger.info("启动火狐浏览器") elif browser == 'Chrome': driver = webdriver.Chrome(self.chrome_driver_path) logger.info("启动谷歌浏览器") elif browser == 'IE': driver = webdriver.Ie(self.ie_driver_path) logger.info("启动IE浏览器") url = ReadConfig().get_string('url', 'login_url') driver.get(url) logger.info("打开URL: %s" % url) driver.maximize_window() logger.info("全屏当前窗口") driver.implicitly_wait(5) logger.info("设置5秒隐式等待时间") return driver
class LoginTest(unittest.TestCase): def setUp(self): # print('执行开始') self.data = ReadTestData(file_name) self.hea_data = ReadTestData() self.http = HttpMethod() self.config = ReadConfig() self.log = MyLog() self.json = OperationJson() self.sheet = 'app_test_fenxi' self.row = list(range(2, 30)) self.log.info(message="----------测试开始----------", name="test_piyue.py") def tearDown(self): # print("我要走了") self.log.info(message="----------测试结束----------", name="test01_OrcLogin.py") def test_fenxi01(self): """获取考试班级/AnalyzeDateEntering/findExamOfAnalyzeDone""" self.log.info(message="test04-1", name="test01_OrcLogin.py", line=get_run_line()) # 获取测试数据 method = self.data.get_method(self.sheet, self.row[0]) url = self.config.get_base_url() + self.data.get_url( self.sheet, self.row[0]) headers = self.hea_data.get_header(self.sheet, self.row[0]) # data = self.data.get_request_data(self.sheet, self.row[0]) expect = self.data.get_expect_result(self.sheet, self.row[0]) self.log.info(message="第一步: 获取请求数据") self.log.info(message="请求方法:%s" % method) self.log.info(message="请求接口:%s" % url) # self.log.info(message="请求数据:%s" % data) self.log.info(message="期望结果:%s" % expect) # 发送请求 status_code, header_token, res_json = self.http.http_method( method=method, url=url, data=None, headers=headers) dict_json = json.loads(res_json) # 把json数据转换成字典对象 self.log.info(message="第二步:发送请求,获取返回数据:") self.log.info(message="%s" % res_json) self.log.info(message="第三步:断言") # 断言 self.assertEqual(status_code, 200, msg=">>>接口请求失败") self.assertEqual(dict_json["code"], '10000', msg=">>>断言失败,实际返回结果:%s" % dict_json["code"]) self.assertEqual(dict_json["msg"], expect["msg"], msg=">>>断言失败,实际返回结果:%s" % dict_json["msg"]) self.log.info(message="断言结束") def test_fenxi02(self): """获取已分析考试列表/AnalyzeDateEntering/findExamOfTeacher""" self.log.info(message="test04-2", name="test01_OrcLogin.py", line=get_run_line()) # 获取测试数据 method = self.data.get_method(self.sheet, self.row[1]) url = self.config.get_base_url() + self.data.get_url( self.sheet, self.row[1]) headers = self.hea_data.get_header(self.sheet, self.row[1]) # data = self.data.get_request_data(self.sheet, self.row[1]) expect = self.data.get_expect_result(self.sheet, self.row[1]) self.log.info(message="第一步: 获取请求数据") self.log.info(message="请求方法:%s" % method) self.log.info(message="请求接口:%s" % url) # self.log.info(message="请求数据:%s" % data) self.log.info(message="期望结果:%s" % expect) # 发送请求 status_code, header_token, res_json = self.http.http_method( method=method, url=url, data=None, headers=headers) dict_json = json.loads(res_json) # 把json数据转换成字典对象 self.log.info(message="第二步:发送请求,获取返回数据:") self.log.info(message="%s" % res_json) self.log.info(message="第三步:断言") # 断言 self.assertEqual(status_code, 200, msg=">>>接口请求失败") self.assertEqual(dict_json["code"], '10000', msg=">>>断言失败,实际返回结果:%s" % dict_json["code"]) self.assertEqual(dict_json["msg"], expect["msg"], msg=">>>断言失败,实际返回结果:%s" % dict_json["msg"]) self.log.info(message="断言结束") def test_fenxi03(self): """更新操作(点击某次考试)查看班级报告/ErrorQuestion/updateFirstNewStatus""" self.log.info(message="test04-3", name="test01_OrcLogin.py", line=get_run_line()) # 获取测试数据 method = self.data.get_method(self.sheet, self.row[2]) url = self.config.get_base_url() + self.data.get_url( self.sheet, self.row[2]) headers = self.hea_data.get_header(self.sheet, self.row[2]) data = self.data.get_request_data(self.sheet, self.row[2]) expect = self.data.get_expect_result(self.sheet, self.row[2]) self.log.info(message="第一步: 获取请求数据") self.log.info(message="请求方法:%s" % method) self.log.info(message="请求接口:%s" % url) self.log.info(message="请求数据:%s" % data) self.log.info(message="期望结果:%s" % expect) # 发送请求 status_code, header_token, res_json = self.http.http_method( method=method, url=url, data=data, headers=headers) dict_json = json.loads(res_json) # 把json数据转换成字典对象 self.log.info(message="第二步:发送请求,获取返回数据:") self.log.info(message="%s" % res_json) self.log.info(message="第三步:断言") # 断言 self.assertEqual(status_code, 200, msg=">>>接口请求失败") self.assertEqual(dict_json["code"], '10000', msg=">>>断言失败,实际返回结果:%s" % dict_json["code"]) self.assertEqual(dict_json["msg"], expect["msg"], msg=">>>断言失败,实际返回结果:%s" % dict_json["msg"]) self.log.info(message="断言结束") def test_fenxi04(self): """班级报告整体情况/getAnalysis""" self.log.info(message="test04-4", name="test01_OrcLogin.py", line=get_run_line()) # 获取测试数据 method = self.data.get_method(self.sheet, self.row[3]) url = self.config.get_base1_url() + self.data.get_url( self.sheet, self.row[3]) headers = self.hea_data.get_header(self.sheet, self.row[3]) data = self.data.get_request_data(self.sheet, self.row[3]) expect = self.data.get_expect_result(self.sheet, self.row[3]) self.log.info(message="第一步: 获取请求数据") self.log.info(message="请求方法:%s" % method) self.log.info(message="请求接口:%s" % url) self.log.info(message="请求数据:%s" % data) self.log.info(message="期望结果:%s" % expect) # 发送请求 status_code, header_token, res_json = self.http.http_method( method=method, url=url, data=json.dumps(data), headers=headers) dict_json = json.loads(res_json) # 把json数据转换成字典对象 self.log.info(message="第二步:发送请求,获取返回数据:") self.log.info(message="%s" % res_json) self.log.info(message="第三步:断言") # 断言 self.assertEqual(status_code, 200, msg=">>>接口请求失败") self.assertEqual(dict_json["code"], '10000', msg=">>>断言失败,实际返回结果:%s" % dict_json["code"]) self.assertEqual(dict_json["msg"], expect["msg"], msg=">>>断言失败,实际返回结果:%s" % dict_json["msg"]) self.log.info(message="断言结束") def test_fenxi05(self): """第一名学生历次考试成绩柱状图/getAnalysis""" self.log.info(message="test04-5", name="test01_OrcLogin.py", line=get_run_line()) # 获取测试数据 method = self.data.get_method(self.sheet, self.row[4]) url = self.config.get_base1_url() + self.data.get_url( self.sheet, self.row[4]) headers = self.hea_data.get_header(self.sheet, self.row[4]) data = self.data.get_request_data(self.sheet, self.row[4]) expect = self.data.get_expect_result(self.sheet, self.row[4]) self.log.info(message="第一步: 获取请求数据") self.log.info(message="请求方法:%s" % method) self.log.info(message="请求接口:%s" % url) self.log.info(message="请求数据:%s" % data) self.log.info(message="期望结果:%s" % expect) # 发送请求 status_code, header_token, res_json = self.http.http_method( method=method, url=url, data=json.dumps(data), headers=headers) dict_json = json.loads(res_json) # 把json数据转换成字典对象 self.log.info(message="第二步:发送请求,获取返回数据:") self.log.info(message="%s" % res_json) self.log.info(message="第三步:断言") # 断言 self.assertEqual(status_code, 200, msg=">>>接口请求失败") self.assertEqual(dict_json["code"], '10000', msg=">>>断言失败,实际返回结果:%s" % dict_json["code"]) self.assertEqual(dict_json["msg"], expect["msg"], msg=">>>断言失败,实际返回结果:%s" % dict_json["msg"]) self.log.info(message="断言结束") def test_fenxi06(self): """进退步趋势/getAnalysis""" self.log.info(message="test04-6", name="test01_OrcLogin.py", line=get_run_line()) # 获取测试数据 method = self.data.get_method(self.sheet, self.row[5]) url = self.config.get_base1_url() + self.data.get_url( self.sheet, self.row[5]) headers = self.hea_data.get_header(self.sheet, self.row[5]) data = self.data.get_request_data(self.sheet, self.row[5]) expect = self.data.get_expect_result(self.sheet, self.row[5]) self.log.info(message="第一步: 获取请求数据") self.log.info(message="请求方法:%s" % method) self.log.info(message="请求接口:%s" % url) self.log.info(message="请求数据:%s" % data) self.log.info(message="期望结果:%s" % expect) # 发送请求 status_code, header_token, res_json = self.http.http_method( method=method, url=url, data=json.dumps(data), headers=headers) dict_json = json.loads(res_json) # 把json数据转换成字典对象 self.log.info(message="第二步:发送请求,获取返回数据:") self.log.info(message="%s" % res_json) self.log.info(message="第三步:断言") # 断言 self.assertEqual(status_code, 200, msg=">>>接口请求失败") self.assertEqual(dict_json["code"], '10000', msg=">>>断言失败,实际返回结果:%s" % dict_json["code"]) self.assertEqual(dict_json["msg"], expect["msg"], msg=">>>断言失败,实际返回结果:%s" % dict_json["msg"]) self.log.info(message="断言结束") def test_fenxi07(self): """知识点儿情况/getAnalysis""" self.log.info(message="test04-7", name="test01_OrcLogin.py", line=get_run_line()) # 获取测试数据 method = self.data.get_method(self.sheet, self.row[6]) url = self.config.get_base1_url() + self.data.get_url( self.sheet, self.row[6]) headers = self.hea_data.get_header(self.sheet, self.row[6]) data = self.data.get_request_data(self.sheet, self.row[6]) expect = self.data.get_expect_result(self.sheet, self.row[6]) self.log.info(message="第一步: 获取请求数据") self.log.info(message="请求方法:%s" % method) self.log.info(message="请求接口:%s" % url) self.log.info(message="请求数据:%s" % data) self.log.info(message="期望结果:%s" % expect) # 发送请求 status_code, header_token, res_json = self.http.http_method( method=method, url=url, data=json.dumps(data), headers=headers) dict_json = json.loads(res_json) # 把json数据转换成字典对象 self.log.info(message="第二步:发送请求,获取返回数据:") self.log.info(message="%s" % res_json) self.log.info(message="第三步:断言") # 断言 self.assertEqual(status_code, 200, msg=">>>接口请求失败") self.assertEqual(dict_json["code"], '10000', msg=">>>断言失败,实际返回结果:%s" % dict_json["code"]) self.assertEqual(dict_json["msg"], expect["msg"], msg=">>>断言失败,实际返回结果:%s" % dict_json["msg"]) self.log.info(message="断言结束") def test_fenxi08(self): """更新操作(点击某次考试)查看评卷课件/ErrorQuestion/updateFirstNewStatus""" self.log.info(message="test04-8", name="test01_OrcLogin.py", line=get_run_line()) # 获取测试数据 method = self.data.get_method(self.sheet, self.row[7]) url = self.config.get_base_url() + self.data.get_url( self.sheet, self.row[7]) headers = self.hea_data.get_header(self.sheet, self.row[7]) data = self.data.get_request_data(self.sheet, self.row[7]) expect = self.data.get_expect_result(self.sheet, self.row[7]) self.log.info(message="第一步: 获取请求数据") self.log.info(message="请求方法:%s" % method) self.log.info(message="请求接口:%s" % url) self.log.info(message="请求数据:%s" % data) self.log.info(message="期望结果:%s" % expect) # 发送请求 status_code, header_token, res_json = self.http.http_method( method=method, url=url, data=data, headers=headers) dict_json = json.loads(res_json) # 把json数据转换成字典对象 self.log.info(message="第二步:发送请求,获取返回数据:") self.log.info(message="%s" % res_json) self.log.info(message="第三步:断言") # 断言 self.assertEqual(status_code, 200, msg=">>>接口请求失败") self.assertEqual(dict_json["code"], expect["code"], msg=">>>断言失败,实际返回结果:%s" % dict_json["code"]) self.assertEqual(dict_json["msg"], expect["msg"], msg=">>>断言失败,实际返回结果:%s" % dict_json["msg"]) self.log.info(message="断言结束") def test_fenxi09(self): """评卷课件情况总览/getCourseware""" self.log.info(message="test04-9", name="test01_OrcLogin.py", line=get_run_line()) # 获取测试数据 method = self.data.get_method(self.sheet, self.row[8]) url = self.config.get_base1_url() + self.data.get_url( self.sheet, self.row[8]) headers = self.hea_data.get_header(self.sheet, self.row[8]) data = self.data.get_request_data(self.sheet, self.row[8]) expect = self.data.get_expect_result(self.sheet, self.row[8]) self.log.info(message="第一步: 获取请求数据") self.log.info(message="请求方法:%s" % method) self.log.info(message="请求接口:%s" % url) self.log.info(message="请求数据:%s" % data) self.log.info(message="期望结果:%s" % expect) # 发送请求 status_code, header_token, res_json = self.http.http_method( method=method, url=url, data=json.dumps(data), headers=headers) dict_json = json.loads(res_json) # 把json数据转换成字典对象 self.log.info(message="第二步:发送请求,获取返回数据:") self.log.info(message="%s" % res_json) self.log.info(message="第三步:断言") # 断言 self.assertEqual(status_code, 200, msg=">>>接口请求失败") self.assertEqual(dict_json["code"], expect["code"], msg=">>>断言失败,实际返回结果:%s" % dict_json["code"]) self.assertEqual(dict_json["msg"], expect["msg"], msg=">>>断言失败,实际返回结果:%s" % dict_json["msg"]) self.log.info(message="断言结束") def test_fenxi10(self): """评卷课件试卷结构//analysisExamPic/getPcQuestionAll""" self.log.info(message="test04-10", name="test01_OrcLogin.py", line=get_run_line()) # 获取测试数据 method = self.data.get_method(self.sheet, self.row[9]) url = self.config.get_base_url() + self.data.get_url( self.sheet, self.row[9]) headers = self.hea_data.get_header(self.sheet, self.row[9]) data = self.data.get_request_data(self.sheet, self.row[9]) expect = self.data.get_expect_result(self.sheet, self.row[9]) self.log.info(message="第一步: 获取请求数据") self.log.info(message="请求方法:%s" % method) self.log.info(message="请求接口:%s" % url) self.log.info(message="请求数据:%s" % data) self.log.info(message="期望结果:%s" % expect) # 发送请求 status_code, header_token, res_json = self.http.http_method( method=method, url=url, data=data, headers=headers) dict_json = json.loads(res_json) # 把json数据转换成字典对象 self.log.info(message="第二步:发送请求,获取返回数据:") self.log.info(message="%s" % res_json) self.log.info(message="第三步:断言") # 断言 self.assertEqual(status_code, 200, msg=">>>接口请求失败") self.assertEqual(dict_json["code"], expect["code"], msg=">>>断言失败,实际返回结果:%s" % dict_json["code"]) self.assertEqual(dict_json["msg"], expect["msg"], msg=">>>断言失败,实际返回结果:%s" % dict_json["msg"]) self.log.info(message="断言结束") def test_fenxi11(self): """评卷课件试卷结构/讲评收藏的题目//analysisExamPic/getCollectionQuestion""" self.log.info(message="test04-11", name="test01_OrcLogin.py", line=get_run_line()) # 获取测试数据 method = self.data.get_method(self.sheet, self.row[10]) url = self.config.get_base_url() + self.data.get_url( self.sheet, self.row[10]) headers = self.hea_data.get_header(self.sheet, self.row[10]) data = self.data.get_request_data(self.sheet, self.row[10]) expect = self.data.get_expect_result(self.sheet, self.row[10]) self.log.info(message="第一步: 获取请求数据") self.log.info(message="请求方法:%s" % method) self.log.info(message="请求接口:%s" % url) self.log.info(message="请求数据:%s" % data) self.log.info(message="期望结果:%s" % expect) # 发送请求 status_code, header_token, res_json = self.http.http_method( method=method, url=url, data=data, headers=headers) dict_json = json.loads(res_json) # 把json数据转换成字典对象 self.log.info(message="第二步:发送请求,获取返回数据:") self.log.info(message="%s" % res_json) self.log.info(message="第三步:断言") # 断言 self.assertEqual(status_code, 200, msg=">>>接口请求失败") self.assertEqual(dict_json["code"], expect["code"], msg=">>>断言失败,实际返回结果:%s" % dict_json["code"]) self.assertEqual(dict_json["msg"], expect["msg"], msg=">>>断言失败,实际返回结果:%s" % dict_json["msg"]) self.log.info(message="断言结束") def test_fenxi12(self): """评卷课件试卷结构/讲评收藏的题目//analysisExamPic/getCollectionQuestion""" self.log.info(message="test04-12", name="test01_OrcLogin.py", line=get_run_line()) # 获取测试数据 method = self.data.get_method(self.sheet, self.row[11]) url = self.config.get_base_url() + self.data.get_url( self.sheet, self.row[11]) headers = self.hea_data.get_header(self.sheet, self.row[11]) data = self.data.get_request_data(self.sheet, self.row[11]) expect = self.data.get_expect_result(self.sheet, self.row[11]) self.log.info(message="第一步: 获取请求数据") self.log.info(message="请求方法:%s" % method) self.log.info(message="请求接口:%s" % url) self.log.info(message="请求数据:%s" % data) self.log.info(message="期望结果:%s" % expect) # 发送请求 status_code, header_token, res_json = self.http.http_method( method=method, url=url, data=data, headers=headers) dict_json = json.loads(res_json) # 把json数据转换成字典对象 self.log.info(message="第二步:发送请求,获取返回数据:") self.log.info(message="%s" % res_json) self.log.info(message="第三步:断言") # 断言 self.assertEqual(status_code, 200, msg=">>>接口请求失败") self.assertEqual(dict_json["code"], expect["code"], msg=">>>断言失败,实际返回结果:%s" % dict_json["code"]) self.assertEqual(dict_json["msg"], expect["msg"], msg=">>>断言失败,实际返回结果:%s" % dict_json["msg"]) self.log.info(message="断言结束") def test_fenxi13(self): """评卷课件试卷结构/讲评收藏的题目//analysisExamPic/getCollectionQuestion""" self.log.info(message="test04-13", name="test01_OrcLogin.py", line=get_run_line()) # 获取测试数据 method = self.data.get_method(self.sheet, self.row[12]) url = self.config.get_base1_url() + self.data.get_url( self.sheet, self.row[12]) headers = self.hea_data.get_header(self.sheet, self.row[12]) data = self.data.get_request_data(self.sheet, self.row[12]) expect = self.data.get_expect_result(self.sheet, self.row[12]) self.log.info(message="第一步: 获取请求数据") self.log.info(message="请求方法:%s" % method) self.log.info(message="请求接口:%s" % url) self.log.info(message="请求数据:%s" % data) self.log.info(message="期望结果:%s" % expect) # 发送请求 status_code, header_token, res_json = self.http.http_method( method=method, url=url, data=json.dumps(data), headers=headers) dict_json = json.loads(res_json) # 把json数据转换成字典对象 self.log.info(message="第二步:发送请求,获取返回数据:") self.log.info(message="%s" % res_json) self.log.info(message="第三步:断言") # 断言 self.assertEqual(status_code, 200, msg=">>>接口请求失败") self.assertEqual(dict_json["code"], expect["code"], msg=">>>断言失败,实际返回结果:%s" % dict_json["code"]) self.assertEqual(dict_json["msg"], expect["msg"], msg=">>>断言失败,实际返回结果:%s" % dict_json["msg"]) self.log.info(message="断言结束") def test_fenxi14(self): """查看成绩单/analysisExamPic/getScoreTableList""" self.log.info(message="test04-14", name="test01_OrcLogin.py", line=get_run_line()) # 获取测试数据 method = self.data.get_method(self.sheet, self.row[13]) url = self.config.get_base_url() + self.data.get_url( self.sheet, self.row[13]) headers = self.hea_data.get_header(self.sheet, self.row[13]) data = self.data.get_request_data(self.sheet, self.row[13]) expect = self.data.get_expect_result(self.sheet, self.row[13]) self.log.info(message="第一步: 获取请求数据") self.log.info(message="请求方法:%s" % method) self.log.info(message="请求接口:%s" % url) self.log.info(message="请求数据:%s" % data) self.log.info(message="期望结果:%s" % expect) # 发送请求 status_code, header_token, res_json = self.http.http_method( method=method, url=url, data=data, headers=headers) dict_json = json.loads(res_json) # 把json数据转换成字典对象 self.log.info(message="第二步:发送请求,获取返回数据:") self.log.info(message="%s" % res_json) self.log.info(message="第三步:断言") # 断言 self.assertEqual(status_code, 200, msg=">>>接口请求失败") self.assertEqual(dict_json["code"], expect["code"], msg=">>>断言失败,实际返回结果:%s" % dict_json["code"]) self.assertEqual(dict_json["msg"], expect["msg"], msg=">>>断言失败,实际返回结果:%s" % dict_json["msg"]) self.log.info(message="断言结束") def test_fenxi15(self): """查看学生简报/analysisExamPic/getScorePostList""" self.log.info(message="test04-15", name="test01_OrcLogin.py", line=get_run_line()) # 获取测试数据 method = self.data.get_method(self.sheet, self.row[14]) url = self.config.get_base_url() + self.data.get_url( self.sheet, self.row[14]) headers = self.hea_data.get_header(self.sheet, self.row[14]) data = self.data.get_request_data(self.sheet, self.row[14]) expect = self.data.get_expect_result(self.sheet, self.row[14]) self.log.info(message="第一步: 获取请求数据") self.log.info(message="请求方法:%s" % method) self.log.info(message="请求接口:%s" % url) self.log.info(message="请求数据:%s" % data) self.log.info(message="期望结果:%s" % expect) # 发送请求 status_code, header_token, res_json = self.http.http_method( method=method, url=url, data=data, headers=headers) dict_json = json.loads(res_json) # 把json数据转换成字典对象 self.log.info(message="第二步:发送请求,获取返回数据:") self.log.info(message="%s" % res_json) self.log.info(message="第三步:断言") # 断言 self.assertEqual(status_code, 200, msg=">>>接口请求失败") self.assertEqual(dict_json["code"], expect["code"], msg=">>>断言失败,实际返回结果:%s" % dict_json["code"]) self.assertEqual(dict_json["msg"], expect["msg"], msg=">>>断言失败,实际返回结果:%s" % dict_json["msg"]) self.log.info(message="断言结束") def test_fenxi16(self): """查看指定学生InStudentName/examManage/InStudentName""" self.log.info(message="test04-16", name="test01_OrcLogin.py", line=get_run_line()) # 获取测试数据 method = self.data.get_method(self.sheet, self.row[15]) url = self.config.get_base_url() + self.data.get_url( self.sheet, self.row[15]) headers = self.hea_data.get_header(self.sheet, self.row[15]) data = self.data.get_request_data(self.sheet, self.row[15]) expect = self.data.get_expect_result(self.sheet, self.row[15]) self.log.info(message="第一步: 获取请求数据") self.log.info(message="请求方法:%s" % method) self.log.info(message="请求接口:%s" % url) self.log.info(message="请求数据:%s" % data) self.log.info(message="期望结果:%s" % expect) # 发送请求 status_code, header_token, res_json = self.http.http_method( method=method, url=url, data=data, headers=headers) dict_json = json.loads(res_json) # 把json数据转换成字典对象 self.log.info(message="第二步:发送请求,获取返回数据:") self.log.info(message="%s" % res_json) self.log.info(message="第三步:断言") # 断言 self.assertEqual(status_code, 200, msg=">>>接口请求失败") self.assertEqual(dict_json["code"], expect["code"], msg=">>>断言失败,实际返回结果:%s" % dict_json["code"]) self.assertEqual(dict_json["msg"], expect["msg"], msg=">>>断言失败,实际返回结果:%s" % dict_json["msg"]) self.log.info(message="断言结束") def test_fenxi17(self): """查看指定学生getSureClass/examManage/verificationName""" self.log.info(message="test04-17", name="test01_OrcLogin.py", line=get_run_line()) # 获取测试数据 method = self.data.get_method(self.sheet, self.row[16]) url = self.config.get_base_url() + self.data.get_url( self.sheet, self.row[16]) headers = self.hea_data.get_header(self.sheet, self.row[16]) data = self.data.get_request_data(self.sheet, self.row[16]) expect = self.data.get_expect_result(self.sheet, self.row[16]) self.log.info(message="第一步: 获取请求数据") self.log.info(message="请求方法:%s" % method) self.log.info(message="请求接口:%s" % url) self.log.info(message="请求数据:%s" % data) self.log.info(message="期望结果:%s" % expect) # 发送请求 status_code, header_token, res_json = self.http.http_method( method=method, url=url, data=data, headers=headers) dict_json = json.loads(res_json) # 把json数据转换成字典对象 self.log.info(message="第二步:发送请求,获取返回数据:") self.log.info(message="%s" % res_json) self.log.info(message="第三步:断言") # 断言 self.assertEqual(status_code, 200, msg=">>>接口请求失败") self.assertEqual(dict_json["code"], expect["code"], msg=">>>断言失败,实际返回结果:%s" % dict_json["code"]) self.assertEqual(dict_json["msg"], expect["msg"], msg=">>>断言失败,实际返回结果:%s" % dict_json["msg"]) self.log.info(message="断言结束") def test_fenxi18(self): """查看指定学生verificationName/examManage/verificationName""" self.log.info(message="test04-18", name="test01_OrcLogin.py", line=get_run_line()) # 获取测试数据 method = self.data.get_method(self.sheet, self.row[17]) url = self.config.get_base_url() + self.data.get_url( self.sheet, self.row[17]) headers = self.hea_data.get_header(self.sheet, self.row[17]) data = self.data.get_request_data(self.sheet, self.row[17]) expect = self.data.get_expect_result(self.sheet, self.row[17]) self.log.info(message="第一步: 获取请求数据") self.log.info(message="请求方法:%s" % method) self.log.info(message="请求接口:%s" % url) self.log.info(message="请求数据:%s" % data) self.log.info(message="期望结果:%s" % expect) # 发送请求 status_code, header_token, res_json = self.http.http_method( method=method, url=url, data=data, headers=headers) dict_json = json.loads(res_json) # 把json数据转换成字典对象 self.log.info(message="第二步:发送请求,获取返回数据:") self.log.info(message="%s" % res_json) self.log.info(message="第三步:断言") # 断言 self.assertEqual(status_code, 200, msg=">>>接口请求失败") self.assertEqual(dict_json["code"], expect["code"], msg=">>>断言失败,实际返回结果:%s" % dict_json["code"]) self.assertEqual(dict_json["msg"], expect["msg"], msg=">>>断言失败,实际返回结果:%s" % dict_json["msg"]) self.log.info(message="断言结束") def test_fenxi19(self): """查看指定学生getScoreResult/examManage/getScoreResult""" self.log.info(message="test04-19", name="test01_OrcLogin.py", line=get_run_line()) # 获取测试数据 method = self.data.get_method(self.sheet, self.row[18]) url = self.config.get_base_url() + self.data.get_url( self.sheet, self.row[18]) headers = self.hea_data.get_header(self.sheet, self.row[18]) data = self.data.get_request_data(self.sheet, self.row[18]) expect = self.data.get_expect_result(self.sheet, self.row[18]) self.log.info(message="第一步: 获取请求数据") self.log.info(message="请求方法:%s" % method) self.log.info(message="请求接口:%s" % url) self.log.info(message="请求数据:%s" % data) self.log.info(message="期望结果:%s" % expect) # 发送请求 status_code, header_token, res_json = self.http.http_method( method=method, url=url, data=data, headers=headers) dict_json = json.loads(res_json) # 把json数据转换成字典对象 self.log.info(message="第二步:发送请求,获取返回数据:") self.log.info(message="%s" % res_json) self.log.info(message="第三步:断言") # 断言 self.assertEqual(status_code, 200, msg=">>>接口请求失败") self.assertEqual(dict_json["code"], expect["code"], msg=">>>断言失败,实际返回结果:%s" % dict_json["code"]) self.assertEqual(dict_json["msg"], expect["msg"], msg=">>>断言失败,实际返回结果:%s" % dict_json["msg"]) self.log.info(message="断言结束") def test_fenxi20(self): """查看全班同学综合报告结果/examManage/publishedResults""" self.log.info(message="test04-20", name="test01_OrcLogin.py", line=get_run_line()) # 获取测试数据 method = self.data.get_method(self.sheet, self.row[19]) url = self.config.get_base_url() + self.data.get_url( self.sheet, self.row[19]) headers = self.hea_data.get_header(self.sheet, self.row[19]) data = self.data.get_request_data(self.sheet, self.row[19]) expect = self.data.get_expect_result(self.sheet, self.row[19]) self.log.info(message="第一步: 获取请求数据") self.log.info(message="请求方法:%s" % method) self.log.info(message="请求接口:%s" % url) self.log.info(message="请求数据:%s" % data) self.log.info(message="期望结果:%s" % expect) # 发送请求 status_code, header_token, res_json = self.http.http_method( method=method, url=url, data=data, headers=headers) dict_json = json.loads(res_json) # 把json数据转换成字典对象 self.log.info(message="第二步:发送请求,获取返回数据:") self.log.info(message="%s" % res_json) self.log.info(message="第三步:断言") # 断言 self.assertEqual(status_code, 200, msg=">>>接口请求失败") self.assertEqual(dict_json["code"], expect["code"], msg=">>>断言失败,实际返回结果:%s" % dict_json["code"]) self.assertEqual(dict_json["msg"], expect["msg"], msg=">>>断言失败,实际返回结果:%s" % dict_json["msg"]) self.log.info(message="断言结束") def test_fenxi21(self): """查看全班同学综合报告分析/AnalyzeDateEntering/findExamAnalyzeId/6803""" self.log.info(message="test04-21", name="test01_OrcLogin.py", line=get_run_line()) # 获取测试数据 method = self.data.get_method(self.sheet, self.row[20]) url = self.config.get_base_url() + self.data.get_url( self.sheet, self.row[20]) headers = self.hea_data.get_header(self.sheet, self.row[20]) # data = self.data.get_request_data(self.sheet, self.row[20]) expect = self.data.get_expect_result(self.sheet, self.row[20]) self.log.info(message="第一步: 获取请求数据") self.log.info(message="请求方法:%s" % method) self.log.info(message="请求接口:%s" % url) # self.log.info(message="请求数据:%s" % data) self.log.info(message="期望结果:%s" % expect) # 发送请求 status_code, header_token, res_json = self.http.http_method( method=method, url=url, data=None, headers=headers) dict_json = json.loads(res_json) # 把json数据转换成字典对象 self.log.info(message="第二步:发送请求,获取返回数据:") self.log.info(message="%s" % res_json) self.log.info(message="第三步:断言") # 断言 self.assertEqual(status_code, 200, msg=">>>接口请求失败") self.assertEqual(dict_json["code"], expect["code"], msg=">>>断言失败,实际返回结果:%s" % dict_json["code"]) self.assertEqual(dict_json["msg"], expect["msg"], msg=">>>断言失败,实际返回结果:%s" % dict_json["msg"]) self.log.info(message="断言结束")
def __init__(self): self.get_dep_data = GetDependData() self.data = GetData() self.config = ReadConfig() self.run_method = RunMethod()
def __get_datalist(fileName): readconfig = ReadConfig(os.path.join(dirpath, "interface", fileName)) datalist = [(readconfig.get('DATA', 'Data' + str(i)), readconfig.get('ASSERT', 'assert' + str(i)), readconfig.get('HTTP', 'url'), readconfig.get('HTTP', 'header'), readconfig.get('HTTP', 'method')) for i in range(1, (1 + int(readconfig.get('HTTP', 'dataNum'))))] return datalist
class RunTest: def __init__(self): self.method = RunMethod() self.data = GetData() self.com_result = CompareResult() self.config = ReadConfig() self.depend = DependData() self.sendemail = SendEmail() def start_on_run(self): res = None result = None pass_num = [] fail_num = [] no_run_num = [] rows_count = self.data.get_case_lines() run_host = self.config.get_http() if run_host: self.data.write_run_env(1, run_host) print("总共 %s 行" % (rows_count)) print("总共 %s 个测试用例" % (rows_count - 2)) print("测试结果:") for i in range(3, rows_count + 1): #time.sleep(5) is_run = self.data.get_is_run(i) is_depend = self.data.get_is_depend(i) request_method = self.data.get_request_method(i) case_id = self.data.get_case_id(i) data = self.data.get_request_data(i) # print(case_id) # print("是否有依赖:%s"%is_depend) if is_run and is_depend: url = run_host + self.data.get_request_url(i) + self.depend.get_data_in_key(i, case_id) # print("编号%s的url:%s" % (case_id, url)) else: url = run_host + self.data.get_request_url(i) # print("编号%s的url:%s"%(case_id, url)) if case_id == "DEV-001" or case_id == "CMB-001": header = self.data.get_request_header(i) else: header = self.data.get_request_header(i, self.config.get_token()) expect_result = self.data.get_expect_result(i) if is_run: run_case_id = self.data.get_case_id(i) run_case_title = self.data.get_case_title(i) res = self.method.run_main(method=request_method, url=url, data=data, header=header) # print("====>>>") # print(res) # if i == 3: if case_id == "DEV-001": dict_json = json.loads(res) # 把json数据转换成字典对象 orc_token = dict_json['orchestrator_admin_token'] # print(dict_json) self.config.write_token(orc_token) # print(orc_token) if case_id == "CMB-001": dict_json = json.loads(res) # 把json数据转换成字典对象 ten_token = dict_json['tenant_admin_token'] # print(dict_json) self.config.write_token(ten_token) # print(orc_token) if self.com_result.is_contain(res, expect_result): print("编号%s(%s): 测试通过" % (run_case_id, run_case_title)) self.data.write_data_actual(i, 'Pass') self.data.write_data_run(i, res) pass_num.append(i) else: print("编号%s(%s): 测试不通过" % (run_case_id, run_case_title)) self.data.write_data_actual(i, 'Fail') self.data.write_data_run(i, res) fail_num.append(i) else: no_run_case_id = self.data.get_case_id(i) no_run_case_title = self.data.get_case_title(i) print("编号%s(%s): 不需要执行" % (no_run_case_id, no_run_case_title)) self.data.write_data_actual(i, 'NA') no_run_num.append(i) print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>") print("%s 个测试用例被执行" % (len(pass_num+fail_num))) print("%s 个测试用例没有执行" % (len(no_run_num))) # 以邮件方式发送测试报告 self.sendemail.start_send(pass_num, fail_num)
# coning = utf-8 __author__ = 'Aimee' from config.readConfig import ReadConfig import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart import time from config.project_path import report_path, data_path, log_path from common.loger import Log log = Log() now_time = time.strftime('%Y-%m-%d %H:%M:%S') #获取配置信息 info = ReadConfig() email_host = info.get_email('mail_host') send_user = info.get_email('mail_send') password = info.get_email('mail_sender_password') sub = info.get_email('subject') user = "******" + "<" + send_user + ">" receivers = info.get_email('receiver').split(';') filepath = [report_path, data_path, log_path] class sendEmail(): def send_mail(self, receivers, filepath): message = MIMEMultipart() message['Subject'] = sub message['From'] = user message['To'] = ','.join(receivers) #邮件正文
class ReadTestData: def __init__(self, file_name=None): # print("打开excel") self.open_excel = OperationExcel() self.set_excel = ReadConfig() if file_name: self.open_json = OperationJson(file_name) else: self.open_json = OperationJson() def get_module(self, sheet_name, row): cell = self.set_excel.get_excel('module') module = self.open_excel.from_ab_get_data(sheet_name, cell, row) return module def get_api_name(self, sheet_name, row): cell = self.set_excel.get_excel('api_name') case_id = self.open_excel.from_ab_get_data(sheet_name, cell, row) return case_id def get_case_id(self, sheet_name, row): cell = self.set_excel.get_excel('case_id') case_id = self.open_excel.from_ab_get_data(sheet_name, cell, row) return case_id def get_case_name(self, sheet_name, row): cell = self.set_excel.get_excel('case_name') case_title = self.open_excel.from_ab_get_data(sheet_name, cell, row) return case_title def get_url(self, sheet_name, row): cell = self.set_excel.get_excel('url') url = self.open_excel.from_ab_get_data(sheet_name, cell, row) return url def get_premise(self, sheet_name, row): cell = self.set_excel.get_excel('premise') premise = self.open_excel.from_ab_get_data(sheet_name, cell, row) return premise def get_header(self, sheet_name, row): cell = self.set_excel.get_excel('header') headers_key = self.open_excel.from_ab_get_data(sheet_name, cell, row) headers = self.open_json.key_get_data(headers_key) return headers def get_method(self, sheet_name, row): # print("我在这里") cell = self.set_excel.get_excel('method') # print(cell) method = self.open_excel.from_ab_get_data(sheet_name, cell, row) return method def get_request_data(self, sheet_name, row): cell = self.set_excel.get_excel('data') #获取excel表格表头 request_key = self.open_excel.from_ab_get_data(sheet_name, cell, row) request_data = self.open_json.key_get_data(request_key) return request_data def get_param(self, sheet_name, row): cell = self.set_excel.get_excel('param') param = self.open_excel.from_ab_get_data(sheet_name, cell, row) request_param = self.open_json.key_get_data(param) return request_param def get_check(self, sheet_name, row): cell = self.set_excel.get_excel('check') check = self.open_excel.from_ab_get_data(sheet_name, cell, row) return check def get_expect_result(self, sheet_name, row): cell = self.set_excel.get_excel('expected') expect_result = self.open_excel.from_ab_get_data(sheet_name, cell, row) expect_result_dict = eval(expect_result) return expect_result_dict def get_return_data(self, sheet_name, row): cell = self.set_excel.get_excel('return') return_data = self.open_excel.from_ab_get_data(sheet_name, cell, row) return return_data def get_rely_data(self, sheet_name, row): cell = self.set_excel.get_excel('rely') data = self.open_excel.from_ab_get_data(sheet_name, cell, row) rely_data = self.open_json.key_get_data(data) return rely_data def get_t_id(self,sheet_name,row): cell = self.set_excel.get_t_id('t_id') t_id = self.open_excel.from_ab_get_data(sheet_name,cell,row) return t_id def get_studentId(self,sheet_name,row): cell = self.set_excel.get_t_id('studentId') studentId = self.open_excel.from_ab_get_data(sheet_name,cell,row) return studentId
#!/usr/bin/python3 # coding=utf-8 import requests import json from common.myLog import MyLog from common.operationJson import OperationJson from config.readConfig import ReadConfig log = MyLog() config = ReadConfig() class ResetEnv: def __init__(self): self.op_json = OperationJson() self.omp = DeleteOmpName() self.headers = self.op_json.key_get_data("orc_token_header") def get_db_list(self): url = config.get_base_url() + "/tenant" res_json = requests.get(url=url, headers=self.headers).json() return json.dumps(res_json, ensure_ascii=False, sort_keys=False, indent=2) # def delete_db(self): # """清理所有DB""" # all_db_dict = json.loads(self.get_db_list()) # if all_db_dict["status"]: # all_db_list = [] # db_number = len(all_db_dict["results"]) # for i in range(db_number): # db_id = all_db_dict["results"][i]["tenant_name"]
class LoginTest(unittest.TestCase): def setUp(self): self.data = ReadTestData(file_name) self.hea_data = ReadTestData() self.http = HttpMethod() self.config = ReadConfig() self.log = MyLog() self.json = OperationJson() self.sheet = 'app_test_case' self.row = list(range(2, 20)) self.log.info(message="----------测试开始----------", name="test01_OrcLogin.py") def tearDown(self): self.log.info(message="----------测试结束----------", name="test01_OrcLogin.py") def test_login01(self): """登录成功/userManage/signIn""" self.log.info(message="test01-1", name="test01_OrcLogin.py", line=get_run_line()) # 获取测试数据 method = self.data.get_method(self.sheet, self.row[0]) url = self.config.get_base_url() + self.data.get_url(self.sheet, self.row[0]) headers = self.hea_data.get_header(self.sheet, self.row[0]) data = self.data.get_request_data(self.sheet, self.row[0]) self.log.info(message="第一步: 获取请求数据") self.log.info(message="请求方法:%s" % method) self.log.info(message="请求接口:%s" % url) self.log.info(message="请求数据:%s" % data) status_code,header_token,res_json = self.http.http_method(method=method, url=url, data=data) dict_json = json.loads(res_json) # 把json数据转换成字典对象 self.log.info(message="第二步:发送请求,获取返回数据:") self.log.info(message="%s" % res_json) if dict_json["data"]: orc_token = header_token["Token"] # 提取orc_token self.log.info(message="提取token", name="test_login01") self.log.info(message="%s" % orc_token, name="test_login01") authorization = orc_token # self.json.write_data(authorization, "orc_token_header", "Authorization") # 把orc_token写入json文件 self.json.write_data(authorization, "login_header_token", "Token") self.json.write_data(authorization, "login_header_piyue", "Token") self.json.write_data(authorization, "login_header_fenxi", "Token") self.json.write_data(authorization, "fenxi", "Token") # 断言 self.log.info(message="第三步:断言") self.assertEqual(status_code, 200, msg=">>>接口请求失败") self.assertEqual(dict_json["data"], "登录成功",msg=">>>断言失败,实际返回结果:%s" % dict_json) self.assertEqual(dict_json["msg"], "OK", msg=">>>断言失败,实际返回值是:%s" % dict_json["msg"]) self.log.info(message="断言结束") def test_login02(self): """获取用户信息/userManage/getInfo""" self.log.info(message="test02-1", name="test01_OrcLogin.py", line=get_run_line()) # 获取测试数据 method = self.data.get_method(self.sheet, self.row[1]) url = self.config.get_base_url() + self.data.get_url(self.sheet, self.row[1]) headers = self.hea_data.get_header(self.sheet, self.row[1]) # data = self.data.get_request_data(self.sheet, self.row[1]) expect = self.data.get_expect_result(self.sheet, self.row[1]) self.log.info(message="第一步: 获取请求数据") self.log.info(message="请求方法:%s" % method) self.log.info(message="请求接口:%s" % url) # self.log.info(message="请求数据:%s" % data) self.log.info(message="期望结果:%s" % expect) # 发送请求 status_code,header_token, res_json = self.http.http_method(method=method, url=url,headers=headers) dict_json = json.loads(res_json) # 把json数据转换成字典对象 self.log.info(message="第二步:发送请求,获取返回数据:") self.log.info(message="%s" % res_json) self.log.info(message="第三步:断言") # 断言 self.assertEqual(status_code, 200, msg=">>>接口请求失败") self.assertEqual(dict_json["code"], '10000',msg=">>>断言失败,实际返回结果:%s" %dict_json["code"]) self.assertEqual(dict_json["data"]["userName"], expect["userName"],msg=">>>断言失败,实际返回结果:%s" % dict_json["data"]["userName"]) self.log.info(message="断言结束") def test_login03(self): """获取首页右上角信息/ErrorQuestion/getIndexBaseInfo""" self.log.info(message="test02-2", name="test01_OrcLogin.py", line=get_run_line()) # 获取测试数据 method = self.data.get_method(self.sheet, self.row[2]) url = self.config.get_base_url() + self.data.get_url(self.sheet, self.row[2]) headers = self.hea_data.get_header(self.sheet, self.row[2]) # data = self.data.get_request_data(self.sheet, self.row[2]) expect = self.data.get_expect_result(self.sheet, self.row[2]) self.log.info(message="第一步: 获取请求数据") self.log.info(message="请求方法:%s" % method) self.log.info(message="请求接口:%s" % url) # self.log.info(message="请求数据:%s" % data) self.log.info(message="期望结果:%s" % expect) # 发送请求 status_code,header_token, res_json = self.http.http_method(method=method, url=url, data=None, headers=headers) dict_json = json.loads(res_json) # 把json数据转换成字典对象 self.log.info(message="第二步:发送请求,获取返回数据:") self.log.info(message="%s" % res_json) self.log.info(message="第三步:断言") # 断言 self.assertEqual(status_code, 200, msg=">>>接口请求失败") self.assertEqual(dict_json["code"],'10000', msg=">>>断言失败,实际返回结果:%s") self.assertEqual(dict_json["data"]["schoolName"], expect["schoolName"], msg=">>>断言失败,实际返回结果:%s" % dict_json["data"]["schoolName"]) self.log.info(message="断言结束") def test_login04(self): """获取考试列表/examManage/getExamlist?type=2""" self.log.info(message="test02-3", name="test01_OrcLogin.py", line=get_run_line()) # 获取测试数据 method = self.data.get_method(self.sheet, self.row[3]) url = self.config.get_base_url() + self.data.get_url(self.sheet, self.row[3]) headers = self.hea_data.get_header(self.sheet, self.row[3]) # data = self.data.get_request_data(self.sheet, self.row[3]) expect = self.data.get_expect_result(self.sheet, self.row[3]) self.log.info(message="第一步: 获取请求数据") self.log.info(message="请求方法:%s" % method) self.log.info(message="请求接口:%s" % url) # self.log.info(message="请求数据:%s" % data) self.log.info(message="期望结果:%s" % expect) # 发送请求 status_code,header_token, res_json = self.http.http_method(method=method, url=url, data=None, headers=headers) dict_json = json.loads(res_json) # 把json数据转换成字典对象 self.log.info(message="第二步:发送请求,获取返回数据:") self.log.info(message="%s" % res_json) self.log.info(message="第三步:断言") # 断言 self.assertEqual(status_code, 200, msg=">>>接口请求失败") self.assertEqual(dict_json["code"], '10000',msg=">>>实际返回结果:%s" %dict_json["code"]) self.assertEqual(dict_json["data"][0]["exam_name"], expect["exam_name"], msg=">>>断言失败,实际返回结果:%s" % dict_json["data"][0]["exam_name"]) self.log.info(message="断言结束") def test_login05(self): """获取电教老师获取电教老师""" self.log.info(message="test02-4", name="test01_OrcLogin.py", line=get_run_line()) # 获取测试数据 method = self.data.get_method(self.sheet, self.row[4]) url = self.config.get_base_url() + self.data.get_url(self.sheet, self.row[4]) headers = self.hea_data.get_header(self.sheet, self.row[4]) # data = self.data.get_request_data(self.sheet, self.row[4]) expect = self.data.get_expect_result(self.sheet, self.row[4]) self.log.info(message="第一步: 获取请求数据") self.log.info(message="请求方法:%s" % method) self.log.info(message="请求接口:%s" % url) # self.log.info(message="请求数据:%s" % data) self.log.info(message="期望结果:%s" % expect) # 发送请求 status_code,header_token, res_json = self.http.http_method(method=method, url=url, data=None, headers=headers) dict_json = json.loads(res_json) # 把json数据转换成字典对象 self.log.info(message="第二步:发送请求,获取返回数据:") self.log.info(message="%s" % res_json) self.log.info(message="第三步:断言") # 断言 self.assertEqual(status_code, 200, msg=">>>接口请求失败") self.assertEqual(dict_json["code"],'10000', msg=">>>断言失败,实际返回结果:%s" % dict_json["code"]) self.assertEqual(dict_json["data"][6]["userName"], expect["userName"], msg=">>>断言失败,实际返回结果:%s" % dict_json["data"][5]["userName"]) self.log.info(message="断言结束")
#coding=utf-8 ''' Created on 2017/9/29 @author: chengww3 Project:执行接口自动化测试项目 ''' import os from common.createSuite import CreateSuite from common.createReport import CreatReport from common.sendMail import SendMail from config.readConfig import ReadConfig def getRootPath(): #获取根目录路径 rootpath = os.path.dirname(os.path.realpath(__file__)) return rootpath if __name__ == '__main__': rootpath = getRootPath() testsuite = CreateSuite().creatSuite(rootpath) # 获取测试套件 # 搜索测试用例,执行测试,生成测试报告 CreatReport().createReport(rootpath,u'接口自动化测试报告', u'test', testsuite) # 获取email的配置信息 sender, receiver, subject, username, password = ReadConfig().getEmailConfig() SendMail().sendMail(rootpath,sender, receiver, subject, username, password)
# -*- coding: utf-8 -*- # @Time : 2018/9/5/005 16:20 # @Author : Administor # @File : page.py # @Software: PyCharm from selenium.webdriver.common.by import By from config.readConfig import ReadConfig loc = (By.XPATH, ReadConfig().get_string('xpath', 'username')) loc1 = (By.XPATH, ReadConfig().get_string('xpath', 'passwd')) user_name = ReadConfig().get_string('login_state', 'user_name') passwd = ReadConfig().get_string('login_state', 'passwd') login_button = (By.XPATH, ReadConfig().get_string('xpath', 'login')) login = ReadConfig().get_string('url', 'login') h5_url = ReadConfig().get_string('url', 'h5_url') pc_url = ReadConfig().get_string('url', 'pc_url') app_url = ReadConfig().get_string('url', 'app_url') banner_management = (By.XPATH, ReadConfig().get_string('xpath', 'banner_management')) banner_channel = (By.XPATH, ReadConfig().get_string('xpath', 'banner_channel')) h5_page = (By.XPATH, ReadConfig().get_string('xpath', 'h5_page')) pc_page = (By.XPATH, ReadConfig().get_string('xpath', 'pc_page')) app_page = (By.XPATH, ReadConfig().get_string('xpath', 'app_page')) banner_limit = (By.XPATH, ReadConfig().get_string('xpath', 'banner_limit')) query_button = (By.XPATH, ReadConfig().get_string('xpath', 'query_button')) reset_search = (By.XPATH, ReadConfig().get_string('xpath', 'reset_search')) product_FAQ = (By.XPATH, ReadConfig().get_string('xpath', 'product_FAQ')) product_categories = (By.XPATH, ReadConfig().get_string('xpath', 'product_categories'))
def homePage(selenium): homePage = HomePage(selenium, root_uri=ReadConfig().get_http('url')) homePage.get('/') time.sleep(1) return homePage
class LoginTest(unittest.TestCase): def setUp(self): # print('执行开始') self.data = ReadTestData(file_name) self.hea_data = ReadTestData() self.http = HttpMethod() self.config = ReadConfig() self.log = MyLog() self.json = OperationJson() self.sheet = 'app_test_piyue' self.sheet_id = 'app_test_data' self.row = list(range(2, 20)) self.log.info(message="----------测试开始----------", name="test_piyue.py") def tearDown(self): # print("我要走了") self.log.info(message="----------测试结束----------", name="test01_OrcLogin.py") def test_piyue01(self): """试卷结构获取/examManage/getPcQuestionAll?examId=6272&studentId= """ self.log.info(message="test03-2", name="test01_OrcLogin.py", line=get_run_line()) # 获取测试数据 # print("kaishi") method = self.data.get_method(self.sheet, self.row[0]) # print(method) url = self.config.get_base_url() + self.data.get_url( self.sheet, self.row[0]) # print(url) headers = self.hea_data.get_header(self.sheet, self.row[0]) # print(headers) data = self.data.get_request_data(self.sheet, self.row[0]) # print(data) expect = self.data.get_expect_result(self.sheet, self.row[0]) # print(expect) self.log.info(message="第一步: 获取请求数据") self.log.info(message="请求方法:%s" % method) self.log.info(message="请求接口:%s" % url) self.log.info(message="请求数据:%s" % data) self.log.info(message="期望结果:%s" % expect) # res = requests.request(method,url,data = data,headers = headers) # print(res.text) # 发送请求 status_code, header_token, res_json = self.http.http_method( method=method, url=url, data=data, headers=headers) dict_json = json.loads(res_json) # 把json数据转换成字典对象 self.log.info(message="第二步:发送请求,获取返回数据:") self.log.info(message="%s" % res_json) self.log.info(message="第三步:断言") # 断言 self.assertEqual(status_code, 200, msg=">>>接口请求失败") self.assertEqual(dict_json["code"], '10000', msg=">>>断言失败,实际返回结果:%s" % dict_json["code"]) self.assertEqual(dict_json["msg"], expect["msg"], msg=">>>断言失败,实际返回结果:%s" % dict_json["msg"]) self.log.info(message="断言结束") def test_piyue02(self): """获取已批阅列表/ErrorQuestion/getAnalyAndReportExamList""" self.log.info(message="test03-1", name="test01_OrcLogin.py", line=get_run_line()) # 获取测试数据 # print("kaishi") method = self.data.get_method(self.sheet, self.row[1]) # print(method) url = self.config.get_base_url() + self.data.get_url( self.sheet, self.row[1]) # print(url) headers = self.hea_data.get_header(self.sheet, self.row[1]) # print(headers) data = self.data.get_request_data(self.sheet, self.row[1]) # print(data) expect = self.data.get_expect_result(self.sheet, self.row[1]) # print(expect) self.log.info(message="第一步: 获取请求数据") self.log.info(message="请求方法:%s" % method) self.log.info(message="请求接口:%s" % url) self.log.info(message="请求数据:%s" % data) self.log.info(message="期望结果:%s" % expect) # res = requests.request(method,url,data = data,headers = headers) # print(res.text) # 发送请求 status_code, header_token, res_json = self.http.http_method( method=method, url=url, data=data, headers=headers) dict_json = json.loads(res_json) # 把json数据转换成字典对象 self.log.info(message="第二步:发送请求,获取返回数据:") self.log.info(message="%s" % res_json) self.log.info(message="第三步:断言") # 断言 self.assertEqual(status_code, 200, msg=">>>接口请求失败") self.assertEqual(dict_json["code"], '10000', msg=">>>断言失败,实际返回结果:%s" % dict_json["code"]) self.assertEqual(dict_json["msg"], expect["msg"], msg=">>>断言失败,实际返回结果:%s" % dict_json["msg"]) self.log.info(message="断言结束") def test_piyue03(self): """获取某题整体情况/examQuestion/getUncheckedTopic/6272/27170/0""" self.log.info(message="test03-3", name="test01_OrcLogin.py", line=get_run_line()) # 获取测试数据 # print("kaishi") method = self.data.get_method(self.sheet, self.row[2]) # print(method) url = self.config.get_base_url() + self.data.get_url( self.sheet, self.row[2]) # print(url) headers = self.hea_data.get_header(self.sheet, self.row[2]) # print(headers) # data = self.data.get_request_data(self.sheet, self.row[5]) # print(data) expect = self.data.get_expect_result(self.sheet, self.row[2]) # print(expect) self.log.info(message="第一步: 获取请求数据") self.log.info(message="请求方法:%s" % method) self.log.info(message="请求接口:%s" % url) # self.log.info(message="请求数据:%s" % data) self.log.info(message="期望结果:%s" % expect) # res = requests.request(method,url,data = data,headers = headers) # print(res.text) # 发送请求 status_code, header_token, res_json = self.http.http_method( method=method, url=url, data=None, headers=headers) dict_json = json.loads(res_json) # 把json数据转换成字典对象 self.log.info(message="第二步:发送请求,获取返回数据:") self.log.info(message="%s" % res_json) self.log.info(message="第三步:断言") # 断言 self.assertEqual(status_code, 200, msg=">>>接口请求失败") self.assertEqual(dict_json["code"], '10000', msg=">>>断言失败,实际返回结果:%s" % dict_json["code"]) self.assertEqual(dict_json["msg"], expect["msg"], msg=">>>断言失败,实际返回结果:%s" % dict_json["msg"]) self.log.info(message="断言结束") def test_piyue04(self): """获取某提批改列表/examQuestion/getStudentNamePicAll/6272/27170""" self.log.info(message="test03-4", name="test01_OrcLogin.py", line=get_run_line()) # 获取测试数据 # print("kaishi") # t_id = self.data.get_t_id(self.sheet_id,self.row[0]) # studentId = self.data.get_studentId(self.sheet_id,self.row[0]) method = self.data.get_method(self.sheet, self.row[3]) # print(method) url = self.config.get_base_url() + self.data.get_url( self.sheet, self.row[3]) # print(url) headers = self.hea_data.get_header(self.sheet, self.row[3]) # print(headers) # data = self.data.get_request_data(self.sheet, self.row[3]) # print(data) expect = self.data.get_expect_result(self.sheet, self.row[3]) # print(expect) self.log.info(message="第一步: 获取请求数据") self.log.info(message="请求方法:%s" % method) self.log.info(message="请求接口:%s" % url) # self.log.info(message="请求数据:%s" % data) self.log.info(message="期望结果:%s" % expect) # res = requests.request(method,url,data = data,headers = headers) # print(res.text) # 发送请求 status_code, header_token, res_json = self.http.http_method( method=method, url=url, data=None, headers=headers) dict_json = json.loads(res_json) # 把json数据转换成字典对象 self.log.info(message="第二步:发送请求,获取返回数据:") self.log.info(message="%s" % res_json) self.log.info(message="第三步:断言") # 断言 self.assertEqual(status_code, 200, msg=">>>接口请求失败") # print(dict_json["code"]) self.assertEqual(dict_json["code"], expect["code"], msg=">>>断言失败,实际返回结果:%s" % dict_json["code"]) self.assertEqual(dict_json["msg"], expect["msg"], msg=">>>断言失败,实际返回结果:%s" % dict_json["msg"]) id_data = dict_json["data"] id_list = [] # studentId_list = [] score_list = [] examID_list = [] questionId_list = [] for k in range(len(id_data)): id_list.append(id_data[k]["id"]) # studentId_list.append(id_data[k]["studentId"]) examID_list.append(6272) # print(examID_list) questionId_list.append(27170) score_data = random.randint(0, 3) score_list.append(score_data) #把需要批改的题块的id和studentid取出来放到excel表格中,方便下一步进行批改 write_excel.write_excel_data(examID_list, id_list, questionId_list, score_list) self.assertEqual(status_code, 200, msg=">>>接口请求失败") self.assertEqual(dict_json["code"], '10000', msg=">>>断言失败,实际返回结果:%s" % dict_json["code"]) self.assertEqual(dict_json["msg"], expect["msg"], msg=">>>断言失败,实际返回结果:%s" % dict_json["msg"]) self.log.info(message="断言结束") def test_piyue05(self): """批改学生试卷/examQuestion/updateStudentUnionExam""" self.log.info(message="test03-6", name="test01_OrcLogin.py", line=get_run_line()) # 获取测试数据 sheetname = "app_test_data" #读取excel测试数据,获取给分请求参数 get_data = ExcelData(data_path, sheetname) datas = get_data.readExcel() method = self.data.get_method(self.sheet, self.row[5]) url = self.config.get_base_url() + self.data.get_url( self.sheet, self.row[5]) headers = self.hea_data.get_header(self.sheet, self.row[5]) expect = self.data.get_expect_result(self.sheet, self.row[5]) self.log.info(message="请求方法:%s" % method) self.log.info(message="请求头:%s" % headers) self.log.info(message="请求接口:%s" % url) self.log.info(message="期望结果:%s" % expect) for i in range(0, len(datas)): data = datas[i] self.log.info(message="第一步: 获取请求数据:%s" % data) status_code, header_token, res_json = self.http.http_method( method=method, url=url, data=json.dumps(data), headers=headers) dict_json = json.loads(res_json) # 把json数据转换成字典对象 self.log.info(message="第二步:发送请求,获取返回数据:") self.log.info(message="%s" % res_json) self.log.info(message="第三步:断言") self.assertEqual(status_code, 200, msg=">>>接口请求失败") # print(dict_json["code"]) self.assertEqual(dict_json["code"], expect["code"], msg=">>>断言失败,实际返回结果:%s" % dict_json["code"]) self.assertEqual(dict_json["msg"], expect["msg"], msg=">>>断言失败,实际返回结果:%s" % dict_json["msg"]) self.log.info(message="断言结束")
#该文件是构造发送邮件的方法,用到python标准库smtplib和email #可以获取最新的测试报告,把最新的测试报告以文本和附件的形式发送 import smtplib import os from common.myLog import MyLog from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.header import Header from config.readConfig import ReadConfig # 路径 path_dir = str( os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))) reportpath = path_dir + '/report' local_readConfig = ReadConfig() class SendEmail: def __init__(self): global host, user, password, sender, title host = local_readConfig.get_email('mail_host') # 邮箱服务器 user = local_readConfig.get_email('mail_user') # 发件人用户名 password = local_readConfig.get_email('mail_pass') # 发件人邮箱授权码,非登录密码 sender = local_readConfig.get_email('sender') # 发件人邮箱 title = local_readConfig.get_email('title') # 邮件标题 self.logger = MyLog() self.receive_user = local_readConfig.get_email('receive_user') # 收件人邮箱 self.receive_user_list = [] for i in str(self.receive_user).split('/'): self.receive_user_list.append(i)
class RunTest: def __init__(self): self.logger = MyLog() self.readconfig = ReadConfig() self.send_mail = SendEmail() # self.env = ResetEnv() self.is_send = self.readconfig.get_email("is_send") # 测试报告基本信息 self.testers = "Roman" self.title = "元丁接口测试报告" self.description = "正式/测试环境:Develop,IP地址:%s" % self.readconfig.get_base_url( ) # print(self.description) # 导入TestCase目录下的全部测试用例 self.discover = unittest.defaultTestLoader.discover(test_case_path, pattern='test*.py') # 导入指定测试用例列表文件 self.case_list_file = case_list_path self.case_list_list = [] # print(self.case_list_list) # 重置测试环境 self.is_env = True # self.is_env = self.env.delete_db() def get_case_list(self): """获取需要进行运行的测试用例列表""" fb = open(self.case_list_file) for i in fb.readlines(): data = str(i) if data != '' and not data.startswith('#'): self.case_list_list.append(data.replace('\n', '')) fb.close() # print(self.case_list_list) def set_test_suite(self): """设置添加测试套件""" self.get_case_list() test_suite = unittest.TestSuite() suite_module = [] for case in self.case_list_list: case_name = case.split('/')[-1] print(case_name + '.py') discover = unittest.defaultTestLoader.discover(test_case_path, pattern=case_name + '.py') suite_module.append(discover) if len(suite_module) > 0: for suite in suite_module: for test_name in suite: test_suite.addTest(test_name) else: return None return test_suite def run_test(self): """执行测试""" if self.is_env: try: test_suite = self.set_test_suite() # 获取测试套件 now = time.strftime("%Y-%m-%d_%H-%M-%S", time.localtime(time.time())) # 获取当前日期时间 public_path = os.path.dirname(os.path.abspath(sys.argv[0])) # filename = public_path + "/report/" + now + "_report.html" # 保存的报告路径和名称 filename = public_path + "/report/" + "index.html" # 保存的报告路径和名称 print("测试报告目录:%s" % filename) fp = open(filename, 'wb') runner = HTMLTestRunner(stream=fp, tester=self.testers, title=self.title, description=self.description) if test_suite is not None: runner.run(test_suite) # 执行指定添加的测试用例套件 # runner.run(self.discover) # 执行TestCase目录下的全部测试用例 else: self.logger.info("Have no case to test.") except Exception as e: self.logger.error(str(e)) finally: self.logger.warning( "---------------All Test End---------------") fp.close() # 发送电子邮件 if self.is_send == 'yes': self.send_mail.send_email() self.logger.warning("测试报告已发送电子邮件!") elif self.is_send == 'no': self.logger.warning("测试报告不发送电子邮件!") else: self.logger.error("测试报告发送电子邮件为未知状态,请检查配置!") else: self.logger.warning("测试环境清理失败的,无法继续执行测试!!!")
import yagmail import time from config.readConfig import ReadConfig emall = ReadConfig() on_off = emall.get_emall('on_off') # 开关 subject = emall.get_emall('subject') # 标题 smtp = emall.get_emall('smtp') # 邮箱服务器 emall_user = emall.get_emall('emall_user') # 邮箱账号 emall_pass = emall.get_emall('emall_pass') # 邮箱密码 emall_to = emall.get_emall('emall_to') # 接收者邮箱 class operate_emall(): def send_emall(self, file_name=None): em = yagmail.SMTP(user=emall_user, password=emall_pass, host=smtp) current_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) content = '接口自动化报告!!!' em.send(to=emall_to, subject=subject + current_time, contents=content, attachments=file_name) if __name__ == '__main__': operate_emall().send_emall()
import os import common.log from config import getPathInfo from common.configEmall import operate_emall from config.readConfig import ReadConfig from common.HTMLTestRunner import HTMLTestRunner from unite.testSuite import testSuite log = common.log.logger path = getPathInfo.get_path() resultPath = os.path.join(path, 'result', 'report.html') send_emall = operate_emall() on_off = ReadConfig().get_emall('on_off') def run(): try: report = open(resultPath, 'wb') suite = testSuite().set_case_suite() if suite is not None: runny = HTMLTestRunner(stream=report, title='接口自动化报告', description='接口自动化报告描述', verbosity=2) runny.run(suite) else: print('Have no case to test.') except Exception as e: print('e:', str(e))