歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Python自動化測試

Python自動化測試

日期:2017/3/1 10:35:39   编辑:Linux編程

Python自動化測試:

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

效果:

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

注意三點:

1. import unittest

2. 測試類要繼承unittest.Testcase

3. main中調用 unittest.main()

最最最最要注意的是:測試類的是測試函數也以test開頭..

Copyright © Linux教程網 All Rights Reserved