import requests
import unittest
class logintest(unittest.TestCase): #测试类名
def setUp(self): #用固件setUp初始化
self.url = "http://www.xxx.com/login.html"
def testlogin1(self):
form = {"username":1311111111,"password":123456}
r = requests.post(self.url,data=form)
self.assertEqual(r.text,"登录成功") #断言验证
def testlogin2(self):
form = {"username":"","password":123456}
r = requests.post(self.url,data=form)
self.assertEqual(r.test,"用户名不能为空")
def testlogin3(self):
form = {"username":1311111111,"password":""}
r= requests.post(self.url,date=form)
self.assertEqual(r.text,"密码不能为空")
def testlogin4(self):
form = {"username":13111111111,"password":"111111"}
r = requests.post(self.url,data=form)
self.assertEqual(r.text,"账户或密码错误")
# def suite(): #测试套件一:调用TestSuite()函数生成一个测试套件实例
# loginTestCase = unittest.TestSuite()
# loginTestCase.addTest(logintest("testlogin1"))
# loginTestCase.addTest(logintest("testlogin2"))
# loginTestCase.addTest(logintest("testlogin3"))
# loginTestCase.addTest(logintest("testlogin4")) #将logintest中的测试函数加入测试套件中
# return loginTestCase
def suite(): #测试套件二:通过makeSuite()函数将logintest中所有"test"开头的测试用例加入测试套件中
loginTestCase = unittest.makeSuite(logintest,"test")
return loginTestCase
"""
多个测试套件可以用TestSuite组合在一起,变成一个新的测试套件
class logintest(unittest.TestCase):
省略测试用例的代码
class loginouttest(unittest.TestCase):
省略测试用例的代码
def suite():
loginTestCase = unittest.makeSuite(logintest,"test")
loginoutTestCase = unittest.makeSuite(logintest,"test")
alltest = unittest.TestSuite(loginTestCase,loginoutTestCase)
return alltest
"""
if __name__ == "__main__": #运行测试
unittest.main()