Python自动化测试


Python自动化测试:

  1. import unittest  
  2.   
  3. ########################################################################   
  4. class RomanNumeralConverter(object):  
  5.     """converter the Roman Number"""  
  6.   
  7.     #----------------------------------------------------------------------   
  8.     def __init__(self, roman_numeral):  
  9.         """Constructor"""  
  10.         self.roman_numeral = roman_numeral  
  11.         self.digit_map = {"M":1000"D":500"C":100"L":50"X":10,  
  12.                           "V":5"I":1}  
  13.           
  14.           
  15.     def convert_to_decimal(self):  
  16.         val = 0  
  17.         for char in self.roman_numeral:  
  18.             val += self.digit_map[char]  
  19.         return val  
  20.       
  21.           
  22.       
  23. ########################################################################   
  24. class RomanNumeralConverterTest(unittest.TestCase):  
  25.     """test class"""  
  26.     def test_parsing_millenia(self):  
  27.         value = RomanNumeralConverter("M")  
  28.         self.assertEquals(1000, value.convert_to_decimal())  
  29.   
  30.    
  31.       
  32. if __name__ == "__main__":  
  33.     unittest.main()  
  34.       

效果:

[python]
  1. .  
  2. ----------------------------------------------------------------------  
  3. Ran 1 test in 0.000s  
  4.   
  5. OK  

注意三点:

1. import unittest

2.   测试类要继承unittest.Testcase

3.  main中调用 unittest.main()

最最最最要注意的是:测试类的是测试函数也以test开头..

相关内容