python unittest初探

2023-01-31 02:01:12 python 初探 Unittest

测试的类:

class Request:
   url = ""
   method = ""
   paras = {}
   def __init__(self,url):
       self.url = url
       self.method = "post"
       self.paras = {}

   def setPincode(self,pincode):
       self.addParas("pincode",pincode)

   def getPincode(self):
       return self.paras.get("pincode")

   def addParas(self,key,value):
       if (self.paras.has_key(key)):
           del self.paras[key]
       self.paras[key] = value

==============================================

测试类:

import unittest
from Request import *

class MyTestCase(unittest.TestCase):


   def setUp(self):

       pass

   def tearDown(self):
       print "--------------End!"
       pass

   def test_Request(self):
       self.request = Request("Http://www.baidu.com")
       self.request.setPincode("pincode1")
       self.request.addParas("pincode","parauuuuuuuuuuuuuu1")
       self.assertEqual(self.request.getPincode(),"parauuuuuuuuuuuuuu1")
       for k,v in self.request.paras.items():
           print k,v

   def test_something(self):
       self.assertEqual(False, False)
       print "test_something1"

def suite():
   suite = unittest.TestSuite()
   print "1"
   suite.addTest(unittest.makeSuite(MyTestCase,"test"))
   print "2"
   return suite

if __name__ == '__main__':
#   unittest.main()
   suiteTest = suite()
   runner = unittest.TextTestRunner()
   runner.run(suiteTest)

==============================

运行结果:


1
..
2
----------------------------------------------------------------------
pincode parauuuuuuuuuuuuuu1
Ran 2 tests in 0.001s
--------------End!

test_something1
OK
--------------End!

Process finished with exit code 0

相关文章