欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python自动化测试之setUp与tearDown实例

程序员文章站 2022-06-21 07:54:57
本文实例讲述了python自动化测试之setup与teardown的用法,分享给大家供大家参考。具体如下: 实例代码如下: class romannumeral...

本文实例讲述了python自动化测试之setup与teardown的用法,分享给大家供大家参考。具体如下:

实例代码如下:

class romannumeralconverter(object): 
  def __init__(self): 
    self.digit_map = {"m":1000, "d":500, "c":100, "l":50, "x":10,  
             "v":5, "i":1} 
  def convert_to_decimal(self, roman_numeral): 
    val = 0 
    for char in roman_numeral: 
      val += self.digit_map[char] 
    return val 
 
   
import unittest 
class romannumeralconvertertest(unittest.testcase): 
  def setup(self): 
    print "create a new romannumeralconvertertest....." 
    self.cvt = romannumeralconverter() 
     
  def teardown(self): 
    print "destroying a romannumeralconvertertest...." 
    self.cvt = none 
     
  def test_parsing_millenia(self): 
    self.assertequals(1000, self.cvt.convert_to_decimal("m")) 
     
     
if __name__ == "__main__": 
  unittest.main()

输出结果如下:

create a new romannumeralconvertertest.....
destroying a romannumeralconvertertest....
.
----------------------------------------------------------------------
ran 1 test in 0.016s

ok

注:setup和teardown在每个测试方法运行时被调用