Home > Backend Development > Python Tutorial > python自动化测试实例解析

python自动化测试实例解析

WBOY
Release: 2016-06-16 08:41:36
Original
988 people have browsed it

本文实例讲述了python自动化测试的过程,分享给大家供大家参考。

具体代码如下:

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() 
   
Copy after login

程序运行效果如下:

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

OK 

Copy after login

此处注意三点:

1. import unittest
2. 测试类要继承unittest.Testcase
3. main中调用 unittest.main()

这里需要注意的是:测试类的是测试函数也以test开头。

希望本文所述对大家的Python程序设计有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template