Python automated test case resolution

  • 2020-04-02 14:09:57
  • OfStack

This article illustrates the process of python automation test and shares it with you for your reference.

The specific code is as follows:


import unittest 
 
######################################################################## 
class RomanNumeralConverter(object): 
  """converter the Roman Number""" 
 
  #---------------------------------------------------------------------- 
  def __init__(self, roman_numeral): 
    """Constructor""" 
    self.roman_numeral = roman_numeral 
    self.digit_map = {"M":1000, "D":500, "C":100, "L":50, "X":10, 
             "V":5, "I":1} 
     
  def convert_to_decimal(self): 
    val = 0 
    for char in self.roman_numeral: 
      val += self.digit_map[char] 
    return val 
   
######################################################################## 
class RomanNumeralConverterTest(unittest.TestCase): 
  """test class""" 
  def test_parsing_millenia(self): 
    value = RomanNumeralConverter("M") 
    self.assertEquals(1000, value.convert_to_decimal()) 
   
if __name__ == "__main__": 
  unittest.main() 
   

The program runs as follows:


.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK 

Here are three points to note:

1. Import unittest
2. Test classes inherit from unittest.testcase
3. Main calls unittest.main()

The important thing to note here is that the test function of the test class also begins with test.

I hope this article has helped you with your Python programming.


Related articles: