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

yipeiwu_com5年前Python基础

本文实例讲述了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在每个测试方法运行时被调用

相关文章

在Python的web框架中编写创建日志的程序的教程

在Python的web框架中编写创建日志的程序的教程

在Web开发中,后端代码写起来其实是相当容易的。 例如,我们编写一个REST API,用于创建一个Blog: @api @post('/api/blogs') def api_cre...

两个命令把 Vim 打造成 Python IDE的方法

两个命令把 Vim 打造成 Python IDE的方法

运行下面两个命令,即可把 Vim(含插件)配置成 Python IDE。目前支持 MAC 和 Ubuntu。 curl -O https://raw.githubuserconten...

基于python的七种经典排序算法(推荐)

基于python的七种经典排序算法(推荐)

一、排序的基本概念和分类 所谓排序,就是使一串记录,按照其中的某个或某些关键字的大小,递增或递减的排列起来的操作。排序算法,就是如何使得记录按照要求排列的方法。 排序的稳定性: 经过某...

springboot配置文件抽离 git管理统 配置中心详解

springboot配置文件抽离,便于服务器读取对应配置文件,避免项目频繁更改配置文件,影响项目的调试与发布 1.创建统一配置中心项目conifg 1)pom配置依赖 <pa...

pytorch:torch.mm()和torch.matmul()的使用

如下所示: torch.mm(mat1, mat2, out=None) → Tensor torch.matmul(mat1, mat2, out=None) → Tensor...