结构
- setUp:test开始方法执行之前,首先会被执行
- tearDown:test开始方法执行之后,会被执行
注意:
测试类必须继承unittest.TestCase
测试用例方法,必须以test开头
# 1.导包 unittest requests import unittest import requests # 新建测试类-->unittest.TestCase class TestLogin(unittest.TestCase): # 3.setUp def setUp(self): # 获取session对象 self.session = requests.session() # 登录url self.url_login = "url_login" # 验证码url self.url_verify = "url_verify" # 4.tearDown def tearDown(self) : # 关闭session self.session.close() # 5.登录成功 def test_login_success(self): # 请求验证码-->获取cookies self.session.get(self.url_verify) # 请求登录 data = {...} r = self.session.post(self.url_login,data=data) try: # 断言 self.assertEqual("登录成功",r.json()['msg']) except AssertionError as e: print(e) # 6.登录失败,账号不存在 def test_username_not_exist(self): pass # 6.登录失败,密码错误 def test_password_error(self): pass if __name__ == '__main__': unittest.main()