`
sillycat
  • 浏览: 2556127 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

DiveIntoPython(十二)

阅读更多
DiveIntoPython(十二)

英文书地址:
http://diveintopython.org/toc/index.html

Chapter 13.Unit Testing

13.1.Introduction to Roman numerals
roman.py

13.2.Diving in
Python has a framework for unit testing, the appropriately-named unittest module.

13.3.Introducing romantest.py
example 13.1.romantest.py
import roman
import unittest

class KnownValues(unittest.TestCase):  
...snip..

def testToRomanKnownValues(self):                         
        """toRoman should give known result with known input"""
        for integer, numeral in self.knownValues:             
            result = roman.toRoman(integer)                   
            self.assertEqual(numeral, result)           
...snip...

if __name__ == "__main__":
    unittest.main() 

13.4.Testing for success
A test case should be able to...

1.run completely by itself, without any human input. Unit testing is about automation.
2.determine by itself whether the function it is testing has passed or failed, without a human interpreting the results.
3.run in isolation, separate from any other test cases (even if they test the same functions). Each test case is an island.

example 13.2.testToRomanKnownVallues
import roman
import unittest

class KnownValues(unittest.TestCase):                          
    knownValues = ( (1, 'I'),
                    (2, 'II'),
                    (3, 'III'),
                    (4, 'IV'),
                    (3999, 'MMMCMXCIX'))                       

    def testToRomanKnownValues(self):                          
        """toRoman should give known result with known input"""
        for integer, numeral in self.knownValues:             
            result = roman.toRoman(integer)                     
            self.assertEqual(numeral, result)
if __name__ == "__main__":
    unittest.main() 

13.5.Testing for Failure
the unittest module provides methods for testing whether a function raises a particular exception when given bad input.

example 13.3.Testing bad input to toRoman
class ToRomanBadInput(unittest.TestCase):                           
    def testTooLarge(self):                                         
        """toRoman should fail with large input"""                  
        self.assertRaises(roman.OutOfRangeError, roman.toRoman, 4000)

    def testZero(self):                                             
        """toRoman should fail with 0 input"""                      
        self.assertRaises(roman.OutOfRangeError, roman.toRoman, 0)   

    def testNegative(self):                                         
        """toRoman should fail with negative input"""               
        self.assertRaises(roman.OutOfRangeError, roman.toRoman, -1) 

    def testNonInteger(self):                                       
        """toRoman should fail with non-integer input"""            
        self.assertRaises(roman.NotIntegerError, roman.toRoman, 0.5) 

example 13.4.Testing bad input to fromRoman
class FromRomanBadInput(unittest.TestCase):                                     
    def testTooManyRepeatedNumerals(self):                                      
        """fromRoman should fail with too many repeated numerals"""             
        for s in ('MMMM', 'DD', 'CCCC', 'LL', 'XXXX', 'VV', 'IIII'):            
            self.assertRaises(roman.InvalidRomanNumeralError, roman.fromRoman, s)

    def testRepeatedPairs(self):                                                
        """fromRoman should fail with repeated pairs of numerals"""             
        for s in ('CMCM', 'CDCD', 'XCXC', 'XLXL', 'IXIX', 'IVIV'):              
            self.assertRaises(roman.InvalidRomanNumeralError, roman.fromRoman, s)

    def testMalformedAntecedent(self):                                          
        """fromRoman should fail with malformed antecedents"""                  
        for s in ('IIMXCC', 'VX', 'DCM', 'CMM', 'IXIV',
                  'MCMC', 'XCX', 'IVI', 'LM', 'LD', 'LC'):                      
            self.assertRaises(roman.InvalidRomanNumeralError, roman.fromRoman, s)

13.6.Testing for sanity   sanity ['sænəti] n. 明智;头脑清楚;精神健全;通情达理,合乎逻辑
usually in the form of conversion functions where one converts A to B and the other converts B to A. In these cases, it is useful to create a “sanity check” to make sure that you can convert A to B and back to A without losing precision, incurring rounding errors, or triggering any other sort of bug.

example 13.5.Testing toRoman against fromRoman
class SanityCheck(unittest.TestCase):       
    def testSanity(self):                   
        """fromRoman(toRoman(n))==n for all n"""
        for integer in range(1, 4000):        
            numeral = roman.toRoman(integer)
            result = roman.fromRoman(numeral)
            self.assertEqual(integer, result)

example 13.6.Testing for case
class CaseCheck(unittest.TestCase):                  
    def testToRomanCase(self):                       
        """toRoman should always return uppercase""" 
        for integer in range(1, 4000):               
            numeral = roman.toRoman(integer)         
            self.assertEqual(numeral, numeral.upper())        

    def testFromRomanCase(self):                     
        """fromRoman should only accept uppercase input"""
        for integer in range(1, 4000):               
            numeral = roman.toRoman(integer)         
            roman.fromRoman(numeral.upper())                   
            self.assertRaises(roman.InvalidRomanNumeralError,
                              roman.fromRoman, numeral.lower())

分享到:
评论

相关推荐

    《Dive Into Python 3中文版》PDF

    《Dive Into Python 3中文版》是一本深入学习Python 3编程语言的教程,适合初学者和有一定编程基础的开发者。这本书详细介绍了Python 3的各种特性,包括语法、数据结构、函数、类、模块、异常处理、输入/输出、网络...

    dive into python3 (中文版)

    Python是一种广泛使用的高级编程语言,以其简洁明了的语法和强大的功能而闻名。《深入Python3(中文版)》是一本系统介绍Python 3的书籍,旨在帮助读者深入学习Python 3的基本知识与应用。本文将根据给定文件的信息...

    Dive into Python3

    《Dive into Python3》的压缩包文件名为diveintopython3-r860-2010-01-13,这可能表示它是2010年1月13日发布的第860个修订版。这个版本可能包含了作者对初版的修正和更新,以适应Python 3的最新发展。 通过阅读这...

    Dive Into Python 中文译文版

    PDF版本的《Dive Into Python 中文译文版》(diveintopython-pdfzh-cn-5.4b.zip)提供了完整的书籍内容,涵盖了Python的基础知识到高级特性。书中通过实际案例引导读者深入学习,包括但不限于变量、数据类型、控制...

    DiveIntoPython

    《Dive Into Python》是一本深受编程初学者和有经验开发者喜爱的Python编程教程。这本书以其深入浅出的讲解方式,让学习者能够快速掌握Python编程语言的核心概念和实际应用,特别是对于想要涉足Web开发领域的读者,...

    dive into python(中文版)

    - **在线地址**:本书可通过官方网址http://diveintopython.org/(英文原版)及http://www.woodpecker.org.cn/diveintopython(中文版)获取。 - **版本更新**:建议通过官方渠道获取最新版本,确保内容的准确性和...

    深入Python (Dive Into Python)

    深入python,深入Python (Dive Into Python) 译者序 by limodou 主页(http://phprecord.126.com) Python论坛 本书英文名字为《Dive Into Python》,其发布遵守 GNU 的自由文档许可证(Free Document Lience)的...

    Dive into python

    dive into python英文原版,Dive Into Python 3 covers Python 3 and its differences from Python 2. Compared to Dive Into Python, it’s about 20% revised and 80% new material. The book is now complete, ...

    Dive Into Python 2 中文版

    《Dive Into Python 2 中文版》是一本深度探讨Python编程语言的教程,适合已经有一定编程基础,希望深入理解Python特性和应用的读者。这本书以其详尽的解释和丰富的实例,为Python初学者和进阶者提供了全面的学习...

    Dive Into Python 3

    《深入Python 3》是一本全面且深入介绍Python 3编程语言的电子书籍,旨在帮助读者从...压缩包中的文件“diveintomark-diveintopython3-793871b”很可能是该书的源代码或HTML文件,可以配合阅读,加深对书中示例的理解。

    Dive Into Python 3 无水印pdf

    Dive Into Python 3 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除

    Dive Into Python 3, r870 (2010).pdf

    Didyoureadtheoriginal“DiveIntoPython”?Didyoubuyit onpaper?(Ifso,thanks!)AreyoureadytotaketheplungeintoPython3?…Ifso,readon.(Ifnoneofthat istrue,you’dbebetteroffstartingatthebeginning.) Python3...

    Dive Into Python V5.4

    《Dive Into Python V5.4》是一本深入学习Python编程语言的经典教程,以其详尽的解释和丰富的实例深受程序员们的喜爱。这个版本是官方提供的最新版本,它不仅包含了PDF格式的完整书籍,还附带了书中所有示例代码,为...

    diveintopython-examples-5.4.rar

    diveintopython-examples-5.4.rardiveintopython-examples-5.4.rardiveintopython-examples-5.4.rardiveintopython-examples-5.4.rar

    diveintopython3

    在“diveintopython3-master”这个压缩包中,包含了这本书的所有源代码示例。通过这些代码,我们可以学习到以下关键知识点: 1. **Python基础**:包括变量、数据类型(如整型、浮点型、字符串、列表、元组、字典)...

    dive-into-python3 (英文版)+深入python3(中文版)

    《Dive Into Python3》和《深入Python3》是两本深受Python爱好者欢迎的书籍,分别提供了英文和中文的学习资源,旨在帮助读者全面理解和掌握Python3编程语言。这两本书覆盖了Python3的基础语法、高级特性以及实际应用...

    Dive Into Python中文版

    Dive Into Python中文版,精心整理,epub版本方便阅读,下载阅读.

    Dive Into Python 3 中文版

    ### Dive Into Python 3 中文版 - 安装Python 3 #### 标题解析 - **Dive Into Python 3 中文版**:这本书名表明了内容将深入讲解Python 3的各项特性和使用方法,适合希望深入了解Python 3编程语言的读者。 #### ...

Global site tag (gtag.js) - Google Analytics