class TestMethod(unittest.TestCase): def setUp(self): self.userid = None self.run = RunMain() def test_02(self): url = 'http://coding.imooc.com/api/cate' data = { 'timestamp': '1507006626132', 'uid': '5249191', 'uuid': '5ae7d1a22c82fb89c78f603420870ad7', 'secret': '078474b41dd37ddd5efeb04aa591ec12', 'token': '0b4c502ba647664be04dfedb32ad4f3d', 'cid': '0' } res = self.run.run_main(url, 'POST', data) #print res self.assertEqual(res['errorCode'], 1000, "测试失败") print '这是第一个case' #@unittest.skip('test_02') def test_01(self): url = 'http://coding.imooc.com/api/cate' data = { 'timestamp': '1507006626132', 'uid': '5249192', 'uuid': '5ae7d1a22c82fb89c78f603420870ad7', 'secret': '078474b41dd37ddd5efeb04aa591ec12', 'token': '0b4c502ba647664be04dfedb32ad4f3d', 'cid': '0' } res = self.run.run_main(url, 'POST', data) self.assertEqual(res['errorCode'], 1000, "测试失败") print '这是第二个case'
class TestMethod(unittest.TestCase): # @classmethod # def setUpClass(cls): # print("类执行之前的方法") # @classmethod # def tearDownClass(cls): # print("类执行之后的方法") def setUp(self): print('test ---> setup') self.run = RunMain() # # def tearDown(self): # print('test -- > tearDown ') def test_01(self): print("这是第01个测试方法") url = 'https://m.gearbest.com/we/feed/group' data = {"group": 100, "page": 1, "pageSize": 10} res = self.run.run_main(url, 'POST', data) a = 0 for i in res: print(a, res[a]) a = a + 1 if res[2] == "SUCCESS": print('测试通过')
class TestMethod(unittest.TestCase): def setUp(self):#,每个case执行之前都要执行setup self.run = RunMain() #self.user_id = self.test_001() def test_001(self): url = 'http://127.0.0.1:8000/login/' data = {'username':'******', 'password':'******'} #res = self.run.run_main(url,"POST",data) #print(res) res = mock_test(self.run.run_main,data,'POST',url,data) #self.assertEqual(res['user'],'sarah') #globals()['userid'] = 10034 #定义全局变量 #return self.user_id print(res) @unittest.skip('test_002') #跳过test_002的方法 def test_002(self): url = 'http://127.0.0.1:8000/login/' data = {'username':'******', 'password':'******'} res = self.run.run_main(url,"POST",data) print(res) print(userid) def tearDown(self): pass
class TestMethod(unittest.TestCase): def setUp(self): self.run = RunMain() def test_01(self): header = { 'Content-Type': 'application/json', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1', 'Authorization': 'access_key=&^Rwqq%QSSx!HTnFm9XB@pFzL&8im$;app_id=2LaqRXRQvNrVN0Nl' } url = 'http://172.16.101.35:16000/user' data = { "id_card_name": "张全-", "id_card_image1": "https://images.coralglobal.cn/20171009/3628696048878239322.JPG", "id_card_image2": "https://images.coralglobal.cn/auth_images/20190410/3628705426294555.JPG", "merchant": "哇", "mobile": "16188880002", "bank_account": "612888528144912", "id_card_no": "622222194021144911", "email": "*****@*****.**", "bank_branch": "中国银行杭州分行", "company_en_name": "coralglobal", "id_card_image3": "https://images.coralglobal.cn/auth_images/20190410/3628705426294555.JPG", "type": "1" } res1 = self.run.run_main(url, "POST", json.dumps(data), header) self.assertEqual(eval(res1["code"]), 1105, "测试成功") print(res1.json()) def test_02(self): url = 'http://api.ishugui.com/asg/portal/call/265.do' data = {} res2 = self.run.run_main(url, 'POST', data) print("test02") print(res2) def test_03(self): print("我是第三个案例")
class TestMethod(unittest.TestCase): def setUp(self): self.run = RunMain() def test_01(self): url = 'http://op.juhe.cn/shanghai/police?key=eec88d051b6fcf5c5373f870a7b21ab9' data = {'key': 'ec88d051b6fcf5c5373f870a7b21ab9'} res = self.run.run_main(url, 'GET', data) self.assertEqual(res['error_code'], 10001, "测试失败") print("这是第一个测试") def test_02(self): url = 'http://op.juhe.cn/shanghai/police?key=eec88d051b6fcf5c5373f870a7b21ab9' data = {'key': 'ec88d051b6fcf5c5373f870a7b21ab9'} res = self.run.run_main(url, 'POST', data) self.assertEqual(res['error_code'], 10001, "测试失败") print('这是第二个测试方法')
def run_dependent(self,case_id): run=RunMain() row_num=self.ope_excel.get_row_num(case_id) url=self.data.get_url(row_num) method=self.data.get_request_method(row_num) request_data=self.data.get_data_for_json(row_num) headers=self.data.get_headers_for_json(row_num) res=run.run_main(url,method,request_data,headers) return res
class TestMethod(unittest.TestCase): # 定义基本属性,使每个方法都可以调用token temp_token = GetToken().get_token() def setUp(self): self.run = RunMain() # 工厂结果列表查询 def test_01(self): url = 'http://10.188.2.98:8188/ewap-auth/base/plant/querylistByPage' data = { "plantNo": "", "nameCn": "", "pageSize": "10", "pageIndex": 1 } header = { "Authorization": self.temp_token, "Content-Type": "application/json", } res = self.run.run_main(url, 'POST', data, header) self.assertEqual(res['code'], '10000', "测试不通过") # print(res) # print("这是第一个case") def test_02(self): url = 'http:/ewap-auth/base/plant/querylistByPage' data = { "plantNo": "", "nameCn": "", "pageSize": "10", "pageIndex": 1 } header = { "Authorization": self.temp_token, "Content-Type": "application/json", } res = self.run.run_main(url, 'POST', data, header) self.assertEqual(res['code'], '10000', "测试不通过")
class RunTest(object): def __init__(self): self.runmain = RunMain() self.data = getData() self.com_util = CommonUtil() def run(self): res = None row_counts = self.data.get_case_lines() # 获取excel表格行数 # print(row_counts) 5 print(row_counts) for row_count in range(1, row_counts): print( "==============================================================================" ) print(row_count) url = self.data.get_request_url(row_count) # y行不变遍历获取x列的请求地址 method = self.data.get_request_method(row_count) # y行不变遍历获取x列的请求方式 is_run = self.data.get_is_run(row_count) # y行不变遍历获取x列的是否运行 data = self.data.get_request_data( row_count ) # y行不变遍历获取x列的请求数据,这里面时三次调用,依次分别是get_data_for_json丶get_key_words丶get_request_data # header = self.data.get_is_header print(eval(data)) data = json.dumps(eval(data)) header = { 'Content-Type': 'application/json', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1', 'Authorization': 'access_key=&^Rwqq%QSSx!HTnFm9XB@pFzL&8im$;app_id=2LaqRXRQvNrVN0Nl' } expect = int(self.data.get_expect_data(row_count)) print('url:', url) print('method:', method) print('is_run:', is_run) print('data:', data) print("expect", expect) print(type(data)) # print('header:', header) if is_run: res = self.runmain.run_main(url, method, data, header) if self.com_util.is_contains(expect, res): print("测试通过") self.data.write_reality_data(row_count, "pass") else: print("测试失败") self.data.write_reality_data(row_count, "fail") print("*" * 60 + "分割线" + "*" * 60) return res
class TestMethod(unittest.TestCase): def setUp(self): self.run = RunMain() def test_01(self): url = 'http://coding.imooc.com/api/cate' data = { 'timestamp': '1507034803124', 'uid': '5249191', 'uuid': '5ae7d1a22c82fb89c78f603420870ad7', 'secrect': '078474b41dd37ddd5efeb04aa591ec12', 'token': '7d6f14f21ec96d755de41e6c076758dd', 'cid': '0', 'errorCode': 1001 } self.run.run_main = mock.Mock(return_value=data) res = mock_test(self.run.run_main, data, url, "POST", data) res = self.run.run_main(url, 'POST', data) print(res) self.assertEqual(res['errorCode'], 1001, '测试失败') print('test_01') def test_02(self): url = 'http://coding.imooc.com/api/cate' data = { 'timestamp': '1507034803124', 'uid': '5249191', 'uuid': '5ae7d1a22c82fb89c78f603420870ad7', 'secrect': '078474b41dd37ddd5efeb04aa591ec12', 'token': '7d6f14f21ec96d755de41e6c076758dd', 'cid': '0' } res = self.run.run_main(url, 'POST', data) print(res) self.assertEqual(res['errorCode'], 1001, '测试失败') print('test_02')
class TestMethod(unittest.TestCase): def setUp(self): self.run = RunMain() self.a = query() self.name = self.a[0] self.pwd = self.a[1] def test_01(self): url = 'https://class-rd.youke100.com/class_in/admin/user/login?account={n}&password={m}&isVaildCode=false' url = url.format(n=self.name, m=self.pwd) data = {} res1 = self.run.run_main(url, "POST", json.dumps(data)) print(res1) print(type(res1)) print(res1['statusCode']) self.assertEqual(res1['statusCode'], 0, "测试失败")
class TestMethod(unittest.TestCase): def setUp(self): self.run = RunMain() def test_01(self): url = 'http://127.0.0.1:8000/login/' data = {'username': '******', 'password': '******'} #self.run.run_main = mock.Mock(return_value=data) res = mock_test(self.run.run_main, data, url, 'POST', data) #res = self.run.run_main(url,'POST',data) print(res) self.assertEqual(res['password'], '5621232', '测试通过') def test_02(self): url = 'http://127.0.0.1:8000/login2/?username=lirundong&password=123123&token=DSADSA1111' res = self.run.run_main(url, 'GET') re = json.loads(res) self.assertEqual(re['user'], 'lirun2dong', '测试失败') print(re)
class TestMethod(unittest.TestCase): #创建测试类的时候要继承unittest类 def setUp(self): self.run = RunMain() def test_01(self): #所有的case要以test开头 url = 'http://coding.imooc.com/api/cate' data = { 'timestamp': '1111111111', 'uid': '5249191', 'uuid': '5ae7d1a22c82fb89c78f603420870ad7', 'secrect': '078474b41dd37ddd5efeb04aa591ec12', 'token': '7d6f14f21ec96d755de41e6c076758dd', 'cid': '0' } #self.run.run_main= mock.Mock(return_value=data) res = mock_test(self.run.run_main, data, url, "POST", data) # res =self.run.run_main(url,'POST',data) print(res) #若01的某个值在后面的case要被用到,可以声明为全局变量 globals(),见5-4 6分钟 #testcase的执行顺序是按照字母的(非上下顺序),有依赖时注意(尽量别依赖) #跳过case执行,@unittest.skip('test_02') def test_02(self): url = 'http://coding.imooc.com/api/cate' data = { 'timestamp': '2222222', 'uid': '5249192', 'uuid': '5ae7d1a22c82fb89c78f603420870ad7', 'secrect': '078474b41dd37ddd5efeb04aa591ec12', 'token': '7d6f14f21ec96d755de41e6c076758dd', 'cid': '0' } res = self.run.run_main(url, 'POST', data) print(res) print('直接返回的res类型:', type(res)) res = json.loads(res) #loads反序列化,把json字符串变为字典 print('处理后的的res类型:', type(res)) self.assertEqual(res['errorCode'], 1007, "msg ceode err")
class TestMethod(unittest.TestCase): def setUp(self): self.run = RunMain() def test_03(self): url = 'http://coding.ctoedu.com/api/cate' data = { 'timestamp': '1507034803124', 'uid': '5249191', 'uuid': '5ae7d1a22c82fb89c78f603420870ad7', 'secrect': '078474b41dd37ddd5efeb04aa591ec12', 'token': '7d6f14f21ec96d755de41e6c076758dd', 'cid': '0', 'errorCode': 1001 } # self.run.run_main = mock.Mock(return_value=data) res = mock_test(self.run.run_main, data, url, "POST", data) # res = self.run.run_main(url,'POST',data) print res self.assertEqual(res['errorCode'], 1001, "测试失败") print "这是第一个case" # @unittest.skip('test_02') def test_02(self): url = 'http://coding.ctoedu.com/api/cate' data = { 'timestamp': '1507034803124', 'uid': '5249191', 'uuid': '5ae7d1a22c82fb89c78f603420870ad7', 'secrect': '078474b41dd37ddd5efeb04aa591ec12', 'token': '7d6f14f21ec96d755de41e6c076758dd', 'cid': '0' } res = self.run.run_main(url, 'POST', data) self.assertEqual(res['errorCode'], 1001, "测试失败") print("这是第二个case")
class Run_Test(): def __init__(self): with open("./sheet_id.conf","rb") as f: conf_info=eval(f.read()) sheet_id=conf_info["sheet_id"] self.data=Get_data(sheet_id=sheet_id) self.run=RunMain() self.com_utl=CommonUtil() self.send=SendMail() def go_on_run(self): pass_count=[] fail_count=[] cow_count=self.data.get_base_lines() for i in range(1,cow_count): case=self.data.get_caseid(i) # print("case是%s"%case) url=self.data.get_url(i) # print("url是%s" % url) method=self.data.get_request_method(i) # print("method是%s" % method) data=self.data.get_data_for_json(i) # print("data是%s" % data) headers = self.data.get_headers_for_json(i) # print("headers是%s" % headers) expect=self.data.get_expect_data(i) # print("except是%s" % expect) is_run=self.data.get_is_run(i) dep_case=self.data.is_depend(i) dep_two=self.data.is_dependTwo(i) data_fmdat=self.data.get_dat_formata(i) dep_cookie=self.data.is_dependCook(i) mysql_expect=self.data.except_mysql(i) if is_run=="yes": if dep_case != None: if method == "post": # 自己根据redis解决依赖 self.depend_data = Deppenddent_data() self.depend_data.redis_isIn(dep_case) dep_value=self.depend_data.get_data_key(i) # print(" dep_values是%s" %dep_value) dep_key=self.data.get_depent_files(i) # print(" dep_key是%s" % dep_key) dp_case= dep_key.split(":")[0] if dp_case=="data": data[dep_key.split(":")[1]]=dep_value else: headers["Authorization"]="Bearer "+str(dep_value) if dep_two!=None: self.depend_data = Deppenddent_data() self.depend_data.redis_isIn(dep_two) dep_value = self.depend_data.get_data_twokey(i) dep_key = self.data.get_Twodepent_files(i) dep_len=len(dep_key.split(":")) header_ordata = dep_key.split(":")[0] #以下是为了解决a接中返回的值在b接口中是一一对应但不是这个值本身 比如false 在b接口中对应的是1 这样的关系 if header_ordata == "data": if dep_len==2: data[dep_key.split(":")[1]] = dep_value else: if dep_value==False: dep_value = 1 else: dep_value = 2 data[dep_key.split(":")[1]] = dep_value else: headers["Authorization"] = "Bearer " + dep_value if dep_cookie!=None: self.depend_data = Deppenddent_data() self.depend_data.redis_isIn(dep_cookie) # 获取所依赖的a接口headers中的value dep_value = self.depend_data.get_data_keyCookie(i) # 获取b接口中的key dep_key = self.data.get_CookDepent_files(i) # 将接口b headers中的dep_key=dep_value headers[dep_key] = dep_value #老师的方法解决依赖 # self.depend_data = Deppenddent_data(depent_case) # depend_response_data = self.depend_data.get_data_for_key(i) else: self.depend_data = Deppenddent_data() self.depend_data.redis_isIn( dep_case) dep_value = self.depend_data.get_data_key(i) url=url.format(dep_value) elif dep_cookie!=None: self.depend_data = Deppenddent_data() self.depend_data.redis_isIn(dep_cookie) #获取所依赖的a接口headers中的value dep_value = self.depend_data.get_data_keyCookie(i) #获取b接口中的key dep_key = self.data.get_CookDepent_files(i) #将接口b headers中的dep_key=dep_value headers[dep_key]=dep_value # print("headers是%s" %headers) res=self.run.run_main(url,method,data_fmdat,data,headers) res_content=res[0] res_headers=res[1] res_content=json.dumps(res_content) src = StrictRedis() src.set(case,res_content) case_headers=case+":headers" src.set(case_headers,str(res_headers)) res_content=json.loads(res_content) print(res_content) if self.com_utl.is_contain(expect,res_content): #判断是否需要进行数据库中验证 mysql_curso = self.data.rdom(i) # print("mysql_curso 是%s" %mysql_curso) if mysql_curso==None: self.data.write_value(i,"pass") pass_count.append(i) else: conn = connect(host="47.98.179.27", port=3306, user="******", password="******", database="shouji") cs1 = conn.cursor() count = cs1.execute(mysql_curso) if count==int(mysql_expect): self.data.write_value(i, "pass") pass_count.append(i) else: self.data.write_value(i, count) fail_count.append(i) else: scend_except=self.data.get_sce_excepet(i) if scend_except==None: res_content=str(res_content) self.data.write_value(i, res_content) fail_count.append(i) else: if self.com_utl.is_contain(scend_except, res_content): self.data.write_value(i, "pass") pass_count.append(i) else: self.data.write_value(i, res_content) fail_count.append(i)