def test_2(self, *TestData): '''校验headers里的Connection值''' Method = self.RequestMethod if self.RequestData != '': DataAll = eval(self.RequestData) resp = base.get_response(self.url, Method, **DataAll) else: resp = base.get_response(self.url, Method) connect = resp[TestData[0]][TestData[1]] self.assertEqual(connect, TestData[2])
def test_params_headers(self, *TestData): '''验证浏览器是否Chrome''' '''给服务器发送请求''' Method = self.RequestMethod if self.RequestData != '': DataAll = eval(self.RequestData) resp = base.get_response(self.url, Method, **DataAll) else: resp = base.get_response(self.url, Method) User_Agent = resp['headers']['User-Agent'] self.assertIn(TestData[2], User_Agent)
def test_params_headers(self): '''验证浏览器是否chrome''' # params = {'show_env':1} # headers= { # 'User-Agent':'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Mobile Safari/537.36', # 'Accept-Encoding':'gzip, deflate', # 'Accept':'*.*', # 'Connection':'keep-alive' # } '''给服务器发送请求''' # resp1 = requests.get(self.url,params=params,headers=headers,stream=True) # DataALL = {'params':params,'headers':headers} #由于每个测试用例要传递的参数数量不一样,所以我们把参数封装成字典的形式 DataALL = eval( DataAll ) # 由于从excel文件里取到的 DataAll 是字符串,但本语句左边的大写的 DataALL 是可变参数(是字典),所以通过eval()把DataAll转换一下 # Method = 'get' #由于本文件上面 已经把Method已经取到,所以Method = RequestMethod即可,不用再直接把‘get’直接赋给Method Method = RequestMethod resp = base.get_response(self.url, Method, **DataALL) # **DataALL关键字参数 User_Agent = resp['headers'][ 'User-Agent'] #取一个实际结果保存为User_Agent,然后下面用断言进行比较 # self.assertIn('Mozilla',User_Agent) # Mozilla预期结果,User_Agent实际结果 # 上面已经取到预期结果 expectedresult ,self.assertIn('Mozilla',User_Agent),可以替换成下面的语句 self.assertIn(expectedresult, User_Agent) print(expectedresult)
def test_post_json(self): data = { "employees": [{ "firstName": "Bill", "lastName": "Gates" }, { "firstName": "George", "lastName": "Bush" }, { "firstName": "Thomas", "lastName": "Carter" }] } #发送请求 # r = requests.post(self.url,json=data) # resp = json.loads(r.text) #引用封装的类 DataALL = {'json': data} # resp = HttpService.MyHTTP(self.url).post(self.url, **DataALL) # 引用再次封装的函数 Method = 'post' resp = base.get_response(self.url, Method, **DataALL) #断言 employees = resp.get('data') self.assertIsInstance(employees, str)
def test_get_params2(self): params = {'show_env': 1} DataAll = {'params': params} Method = 'get' # resp=HttpService.MyHTTP().get(self.url,params) resp = base.get_response(self.url, Method, **DataAll) self.assertEqual(resp.headers['Content-Encoding'], 'gzip')
def test_post_json(self): json = { "info": { "code": 1, "sex": '男', "id": 1900, "name": "巧吧软件测试" }, "code": 1, "name": "巧吧软件测试", "sex": '女', "data": [{ "code": 1, "sex": '男', "id": 1900, "name": "巧吧软件测试" }, { "code": 1, "sex": '女', "id": 1900, "name": "巧吧软件测试" }], "id": 1900 } # r = requests.post(self.url,data=json.dumps(data)) DataALL = {'data': json} #由于每个测试用例要传递的参数数量不一样,所以我们把参数封装成字典的形式 Method = 'post' resp = base.get_response(self.url, Method, **DataALL) #**DataALL关键字参数 name = resp.get('data') self.assertIsInstance(name, str)
def test_get_params1(self): params = {'show_env': 1} DataAll = {'params': params} Method = 'get' # resp=HttpService.MyHTTP().get(self.url,params) resp = base.get_response(self.url, Method, **DataAll) self.assertEqual(resp.headers['Connection'], 'keep-alive')
def test_2(self,headers,key,result): '''校验header里host的值''' Method = self.RequestMethod resp = base.get_response(self.url, Method) print(TestData) # connectHost = resp['headers']['host'] connectHost = resp[headers][key] self.assertEqual(connectHost,result)
def test_2(self, headers, key, result): #校验headers里的Connection值 # r = requests.get(self.url) # resp = r.json() # connect = resp[headers][value] Method = self.RequestMethod resp = base.get_response(self.url, Method) connect = resp[headers][key] self.assertEqual(connect, result)
def test_get_params2(self): '''test_get_params2测试''' params = {'show_env': 1} '''给服务器发送请求''' DataALL = {'params': params} Method = 'get' resp = base.get_response(self.url, Method, **DataALL) connect = resp.get('headers').get('Connection') self.assertIsInstance(connect, str)
def test_get_params1(self): '''test_get_params1测试连接的主机''' # 产生的测试报告里的概要信息,就是此注释的内容 params = {'show_env': 1} '''给服务器发送请求''' # resp = HttpService.MyHTTP().get(self.url,params) DataALL = {'params': params} #由于每个测试用例要传递的参数数量不一样,所以我们把参数封装成字典的形式 Method = 'get' resp = base.get_response(self.url, Method, **DataALL) connecthost = resp.get('headers').get('Host') self.assertEqual(connecthost, "httpbin.org")
def test_get_params2(self): '''test_get_params测试''' params = {'show_env': 1} '''给服务器发送请求''' DataALL = {'params': params} #由于每个测试用例要传递的参数数量不一样,所以我们把参数封装成字典的形式 Method = 'get' resp = base.get_response(self.url, Method, **DataALL) connecthost = resp.get('headers').get('Host') self.assertIsInstance(connecthost, str) # 判断取得的结果的类型是不是str类型
def test_post_data_2(self): '''form值type类型判断''' params = {'show_env': 1} data = {'a': '巧吧软件测试', 'b': 'form-data'} DataALL = {'params': params, 'data': data} Method = 'post' resp = base.get_response(self.url, Method, **DataALL) # form = resp.get('form').get('a') form = resp['form']['a'] self.assertEqual(form, '巧吧软件测试') self.assertIsInstance(form, str)
def test_post_data_1(self): '''form值验证''' params = {'show_env':1} data = {'a':'巧吧软件测试','b':'form-data'} DataALL = {'params':params,'data':data} #由于每个测试用例要传递的参数数量不一样,所以我们把参数封装成字典的形式 Method = 'post' resp = base.get_response(self.url, Method, **DataALL) # **DataALL关键字参数 form = resp.get('form').get('a') # 第5步:定义assert断言,判断测试结果 self.assertEqual(form,'巧吧软件测试')
def test_post_data_1(self): '''form值验证''' params = {'show_env': 1} data = {'a': '巧吧软件测试', 'b': 'form-data'} DataALL = {'params': params, 'data': data} Method = 'post' resp = base.get_response(self.url, Method, **DataALL) form = resp.get('form').get('a') # 第5步:定义assert断言,判断测试结果 self.assertEqual(form, '巧吧软件测试')
def test_get_params1(self): '''test_get_params1测试''' params = {'show_env': 1} '''给服务器发送请求''' DataALL = {'params': params} Method = 'get' resp = base.get_response(self.url, Method, **DataALL) if base.get_mock_status() == 'ON': mockresp = mock.Mock(side_effect=mockFoo) resp = mockresp('success') mockresp.assert_called_with('success') print(mockresp.call_args_list) print(mockresp.called) mockresp.reset_mock() connect = resp.get('headers').get('Connection') self.assertEqual(connect, 'close')
def test_params_headers(self): #验证浏览器是否chrome # params={'show_env':1} # headers = { # "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36", # "Accept-Encoding": "gzip, deflate, sdch, br", # 'Accept':'*/*', # 'Connection':'keep-alive' # } #给服务器发送请求 # DataALL = {'params':params,'headers':headers} DataALL = eval(DataAll) #从excel中取出的是字符串,需要用eval函数转换成字典 #Method = 'get' # 请求类型 Method = RequestMethod # resp = HttpService.MyHTTP().get(self.url, **DataALL) resp = base.get_response(self.url, Method, **DataALL) print(resp) User_Agent = resp['headers']['User-Agent'] self.assertIn(Expectedresult, User_Agent)
def test_post_json(self): # params={'show_env':1} # json={"info":{"code":1,"sex":"男","id":1900,"name":"黄爱华"}, # "code":1, # "name":"huangaihau黄爱华huangaihau黄爱华黄爱华 ","sex":"女", # "data":[{"code":1,"sex":"男","id":1900,"name":"黄爱华"},{"code":1,"sex":"女","id":1900,"name":"黄爱华"}], # "id":1900 # } # DataALL = {'params':params,'json':json} # resp = HttpService.MyHTTP().post(self.url,**DataALL) # Method = 'post' # 请求类型 DataALL = eval(RequestData) Method = RequestMethod resp = base.get_response(self.url, Method, **DataALL) print(resp) name = resp.get('data') print(name) self.assertIsInstance(Expectedresult, str)
def test_post_params(self): #上传一个文件判断是否上传成功 os.chdir('..\\test_case\\upload') print(os.getcwd) files = {'field1': ("1.txt", open('1.txt', 'rb'))} DataAll = {'files': files} Method = 'postfile' r = base.get_response( self.url, Method, **DataAll, ) print(r) connect = r.get('Success') msg = r.get("Message") fileid = r.get('Data') print(connect) self.assertEqual(connect, True) self.assertEqual(msg, "上传成功")
def test_post_json(self): DataALL = eval(RequestData) Method = RequestMethod resp = base.get_response(self.url, Method, **DataALL) name = resp.get('data') self.assertIsInstance(Expectedresult, str)
def test_cookie1(self): cookie = {"freeform": "me"} r = base.get_response("get", self.url, cookies=cookie) self.log.info(r.text)
def test_post_json(self): DataALL = eval(RequestData) Method = RequestMethod resp = base.get_response(self.url, Method, **DataALL) name = resp['data'] self.assertIsInstance(name, str)