python自动化测试实例解析

yipeiwu_com7年前Python基础

本文实例讲述了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() 
   

程序运行效果如下:

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

OK 

此处注意三点:

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

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

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

相关文章

windows 10下安装搭建django1.10.3和Apache2.4的方法

windows 10下安装搭建django1.10.3和Apache2.4的方法

环境介绍 python3.5.2 64位 django 1.10.3 apache 2.4 64位 windows 10 重点在apache上。 python 和dj...

pandas DataFrame 警告(SettingWithCopyWarning)的解决

刚接触python不久,编程也是三脚猫,所以对常用的这几个工具还没有一个好的使用习惯,毕竟程序语言是头顺毛驴。所以最近在工作中使用的时候在使用pandas的DataFrame时遇到了以下...

python SVM 线性分类模型的实现

python SVM 线性分类模型的实现

运行环境:win10 64位 py 3.6 pycharm 2018.1.1 导入对应的包和数据 import matplotlib.pyplot as plt import num...

Python利用splinter实现浏览器自动化操作方法

利用Splinter开发浏览器自动化操作,编写代码比较简单。 案例一: from splinter import Browser with Browser() as brows...

python多线程之事件Event的使用详解

前言 小伙伴a,b,c围着吃火锅,当菜上齐了,请客的主人说:开吃!,于是小伙伴一起动筷子,这种场景如何实现 Event(事件) Event(事件):事件处理的机制:全局定义了一个内置标志...