def test_1register_success(self): # 3、构造请求参数 do_request = HandleRequest() register_url = "http://api.lemonban.com/futureloan/member/register" headers_dict = { "X-Lemonban-Media-Type": "lemonban.v2", "User-Agent": "Mozilla/5.0 LookSky" } do_request.add_headers(headers_dict) request_param = {"mobile_phone": "18511112247", "pwd": "12345678"} # 4、发起请求 res = do_request.send("POST", register_url, json=request_param) # 5、提取响应数据,然后断言结果 expected_value = 0 real_code = res.json()["code"] # if expected_value == real_code: # print("恭喜您,测试成功,此接口没问题!") # else: # print("不好意思,测试失败,此接口有问题!") # self.assertEqual方法 # a、是从父类中继承的方法 # b、第一个参数,往往指定为期望值 # c、第二参数,往往指定为实际值 # d、第三参数,往往指定为断言失败之后的错误提示 # e、如果期望值和实际值不匹配,那么会抛出AssertionError # f、可以写多个断言方法,会依次去断言,只要断言失败,同一方法中的其他断言不会执行 self.assertEqual(expected_value, real_code, "测试注册接口的正向用例失败") self.assertIn("OK", res.text, "测试注册接口的正向用例失败")
class TestRegister(unittest.TestCase): # setUp覆盖父类中的方法 # a、测试用例实例方法有几个,那么setUp就会执行几次 # b、先执行setUp,然后再执行测试用例实例方法 # c、往往用于用例的初始化操作 def setUp(self): # 3、构造请求参数 print("setUp") self.do_request = HandleRequest() self.register_url = "http://api.lemonban.com/futureloan/member/register" headers_dict = { "X-Lemonban-Media-Type": "lemonban.v2", "User-Agent": "Mozilla/5.0 LookSky" } self.do_request.add_headers(headers_dict) # tearDown覆盖父类中的方法 # a、用例实例方法有几个,那么tearDown就会执行几次 # b、用例执行结束之后,然后再执行tearDown # c、往往用于资源释放操作 def tearDown(self): print("tearDown") self.do_request.close() # 2、创建测试用例测试方法,一定要以test_作为前缀 def test_1register_success(self): request_param = {"mobile_phone": "18511112239", "pwd": "12345678"} # 4、发起请求 res = self.do_request.send("POST", self.register_url, json=request_param) # 5、提取响应数据,然后断言结果 expected_value = 0 real_code = res.json()["code"] self.assertEqual(expected_value, real_code, "测试注册接口的正向用例失败") def test_2no_mobile(self): request_param = {"pwd": "12345678"} # 4、发起请求 res = self.do_request.send("POST", self.register_url, json=request_param) # 5、提取响应数据,然后断言结果 expected_value = 1 real_code = res.json()["code"] self.assertEqual(expected_value, real_code, "测试手机号为空失败")
def test_2no_mobile(self): # 3、构造请求参数 do_request = HandleRequest() register_url = "http://api.lemonban.com/futureloan/member/register" headers_dict = { "X-Lemonban-Media-Type": "lemonban.v2", "User-Agent": "Mozilla/5.0 LookSky" } do_request.add_headers(headers_dict) request_param = {"pwd": "12345678"} # 4、发起请求 res = do_request.send("POST", register_url, json=request_param) # 5、提取响应数据,然后断言结果 expected_value = 2 real_code = res.json()["code"] self.assertEqual(expected_value, real_code, "测试手机号为空失败")
def test_3_10_mobile(self): # 3、构造请求参数 do_request = HandleRequest() register_url = "http://api.lemonban.com/futureloan/member/register" headers_dict = { "X-Lemonban-Media-Type": "lemonban.v2", "User-Agent": "Mozilla/5.0 LookSky" } do_request.add_headers(headers_dict) request_param = {"mobile_phone": "1851111224", "pwd": "12345678"} # 4、发起请求 res = do_request.send("POST", register_url, json=request_param) # 5、提取响应数据,然后断言结果 expected_value = 1000 real_code = res.json()["code"] print("test_3_10_mobile") print(1 / 0)
@Company: 湖南省零檬信息技术有限公司 @Copyright: 柠檬班 ------------------------------------------------- """ from Py28_0427_unittest.handle_request import HandleRequest # 1、构造请求参数 do_request = HandleRequest() register_url = "http://api.lemonban.com/futureloan/member/register" headers_dict = { "X-Lemonban-Media-Type": "lemonban.v2", "User-Agent": "Mozilla/5.0 LookSky" } do_request.add_headers(headers_dict) request_param = {"mobile_phone": "18511112245", "pwd": "12345678"} # 2、发起请求 res = do_request.send("POST", register_url, json=request_param) # 3、提取响应数据,然后断言结果 expected_value = 0 real_code = res.json()["code"] if expected_value == real_code: print("恭喜您,测试成功,此接口没问题!") else: print("不好意思,测试失败,此接口有问题!")