Python editing foundation and http interface test_9.3 chapter

HTTP status code classification query method + unit test

1  # StatusCodeType.py 
2  class StatusCodeType():
 3      def getStatusType(self,Status):
 4          if isinstance(Status,str):
 5              if Status.isdigit():
 6                  if Status[0]== ' 1 ' :
 7                      return  ' request request information received ' 
8                  elif Status[0]== ' 2 ' :
 9                      return  ' success ' 
10                  elif Status[0]== ' 3 ' :
 11                     return  ' redirect ' 
12                  elif Status[0]== ' 4 ' :
 13                      return  ' client error ' 
14                  elif Status[0]== ' 5 ' :
 15                      return  ' server error ' 
16                  else :
 17                      return  ' no Status code exists ' 
18              else :
 19                  return  ' Status code does not exist ' 
20          elif isinstance(Status,int):
 21              if Status>=100 andStatus<200 :
 22                  return   ' request request information received ' 
23              elif Status >=200 and Status <300 :
 24                  return  ' success ' 
25              elif Status >= 300 and Status < 400 :
 26                  return  ' redirect ' 
27              elif Status > = 400 and Status < 500 :
 28                  return  ' client error ' 
29              elif Status >= 500 and Status < 600 :
 30                  return ' Server error ' 
31              else :
 32                  return  ' Non-existing status code ' 
33          else :
 34              return  ' Non-existing status code '
 1 import StatusCodeType
 2 s=StatusCodeType.StatusCodeType()
 3 class test_StatusCode(unittest.TestCase):
 4     def setUp(self):
 5         pass
 6     def tearDown(self):
 7         pass
 8     def test_01(self):
 9         x=s.getStatusType(100)
10         self.assertEqual(x,'收到请求请求信息')
11     def test_02(self):
12         x=s.getStatusType(199)
13         self.assertEqual(x,' Receive request request information ' )
 14      def test_03(self):
 15          x=s.getStatusType(200 )
 16          self.assertEqual(x, ' success ' )
 17      # @unittest.skip('do not execute this use case') 
18      def test_04(self):
 19          x=s.getStatusType(299 )
 20          self.assertEqual(x, ' success ' )
 21      # @unittest.skipIf(1,'skip it') 
22      def test_05(self):
 23          x =s.getStatusType(300 )
 24         self.assertEqual(x, ' redirect ' )
 25      # @unittest.skipIf(0, 'execute it') 
26      def test_06(self):
 27          x=s.getStatusType(399 )
 28          self.assertEqual(x, ' re orientation ' )
 29      def test_07(self):
 30          x=s.getStatusType(400 )
 31          self.assertEqual(x, ' client error ' )
 32      def test_08(self):
 33          x=s.getStatusType(499 )
 34          self .assertEqual(x,' client error ' )
 35      def test_09(self):
 36          x=s.getStatusType(500 )
 37          self.assertEqual(x, ' server error ' )
 38      def test_10(self):
 39          x=s.getStatusType(599 )
 40          self.assertEqual(x, ' server error ' )
 41      def test_11(self):
 42          x=s.getStatusType(100.5 )
 43          self.assertEqual(x, ' non-existent status code ' )
 44      deftest_12(self):
 45          x=s.getStatusType(-5 )
 46          self.assertEqual(x, ' non-existent status code ' )
 47      def test_13(self):
 48          x=s.getStatusType( ' abc ' )
 49          self .assertEqual(x, ' non-existent status code ' )
 50      def test_14(self):
 51          x=s.getStatusType(99 )
 52          self.assertEqual(x, ' non-existent status code ' )
 53      def test_15(self) :
 54         x=s.getStatusType(600 )
 55          self.assertEqual(x, ' non-existent status code ' )
 56      def test_16(self):
 57          x=s.getStatusType( ' 302 ' )
 58          self.assertEqual(x, ' re orientation ' )
 59      def test_17(self):
 60          x=s.getStatusType( ' 302.5 ' )
 61          self.assertEqual(x, ' non-existent status code ' )
 62  if  __name__ == '__main__':
63     unittest.main()

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325258527&siteId=291194637