python-unittest(6)

python-unittest(6)

在测试模块中定义测试套件

Defining test suites inside the test module.

Each test module can provide one or more methods that define a different test suite. One
method can exercise all the tests in a given module; another method can define a particular
subset.

1. Create a new file called recipe6.py in which to put our code for this recipe.

2. Pick a class to test. In this case, we will use our Roman numeral converter.

3. Create a test class using the same name as the class under test with Test appended
to the end.

4. Write a series of test methods, including a setUp method that creates a new
instance of the RomanNumeralConverter for each test method.

5. Create some methods in the recipe’s module (but not in the test case) that define
different test suites.

6. Create a runner that will iterate over each of these test suites and run them through
unittest’s TextTestRunner.

7. Run the combination of test suites, and see the results.

测试代码:

python-unittest(6)
python-unittest(6)

Code
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):
        self.cvt = RomanNumeralConverter()

    def test_parsing_millenia(self):
        self.assertEquals(1000, \
                          self.cvt.convert_to_decimal("M"))

    def test_parsing_century(self):
        self.assertEquals(100, \
                          self.cvt.convert_to_decimal("C"))

    def test_parsing_half_century(self):
        self.assertEquals(50, \
                          self.cvt.convert_to_decimal("L"))

    def test_parsing_decade(self):
        self.assertEquals(10, \
                          self.cvt.convert_to_decimal("X"))

    def test_parsing_half_decade(self):
        self.assertEquals(5, self.cvt.convert_to_decimal("V"))

    def test_parsing_one(self):
        self.assertEquals(1, self.cvt.convert_to_decimal("I"))

    def test_empty_roman_numeral(self):
        self.assertTrue(self.cvt.convert_to_decimal("") == 0)
        self.assertFalse(self.cvt.convert_to_decimal("") > 0)

    def test_no_roman_numeral(self):
        self.assertRaises(TypeError, \
                          self.cvt.convert_to_decimal, None)

    def test_combo1(self):
        self.assertEquals(4000, \
                          self.cvt.convert_to_decimal("MMMM"))

    def test_combo2(self):
        self.assertEquals(2010, \
                          self.cvt.convert_to_decimal("MMX"))

    def test_combo3(self):
        self.assertEquals(4668, \
                self.cvt.convert_to_decimal("MMMMDCLXVIII"))

def high_and_low():
    suite = unittest.TestSuite()
    suite.addTest(\
       RomanNumeralConverterTest("test_parsing_millenia"))
    suite.addTest(\
       RomanNumeralConverterTest("test_parsing_one"))
    return suite

def combos():
    return unittest.TestSuite(map(RomanNumeralConverterTest,\
              ["test_combo1", "test_combo2", "test_combo3"]))

def all():
    return unittest.TestLoader().loadTestsFromTestCase(\
                               RomanNumeralConverterTest)

if __name__ == "__main__":
    for suite_func in [high_and_low, combos, all]:
        print "Running test suite '%s'" % suite_func.func_name
        suite = suite_func()
        unittest.TextTestRunner(verbosity=2).run(suite)

 

输出结果:

Running test suite ‘high_and_low’
test_parsing_millenia (__main__.RomanNumeralConverterTest) … ok
test_parsing_one (__main__.RomanNumeralConverterTest) … ok

———————————————————————-
Ran 2 tests in 0.000s

OK
Running test suite ‘combos’
test_combo1 (__main__.RomanNumeralConverterTest) … ok
test_combo2 (__main__.RomanNumeralConverterTest) … ok
test_combo3 (__main__.RomanNumeralConverterTest) … ok

———————————————————————-
Ran 3 tests in 0.000s

OK
Running test suite ‘all’
test_combo1 (__main__.RomanNumeralConverterTest) … ok
test_combo2 (__main__.RomanNumeralConverterTest) … ok
test_combo3 (__main__.RomanNumeralConverterTest) … ok
test_empty_roman_numeral (__main__.RomanNumeralConverterTest) … ok
test_no_roman_numeral (__main__.RomanNumeralConverterTest) … ok
test_parsing_century (__main__.RomanNumeralConverterTest) … ok
test_parsing_decade (__main__.RomanNumeralConverterTest) … ok
test_parsing_half_century (__main__.RomanNumeralConverterTest) … ok
test_parsing_half_decade (__main__.RomanNumeralConverterTest) … ok
test_parsing_millenia (__main__.RomanNumeralConverterTest) … ok
test_parsing_one (__main__.RomanNumeralConverterTest) … ok

———————————————————————-
Ran 11 tests in 0.001s

OK

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/110380.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 【科普贴】SPI接口详解

    【科普贴】SPI接口详解一、SPI接口简介SPI接口是一种同步串行总线(SerialPeripheralInterface)多用于Flash存储器(如NORFlash&NandFlashd),ADC、LCD控制器等外围器件的通讯接口。大大增强了处理器的外设扩展能力。SPI接口缩写SSEL:slaveselect,常常也被写作CS(chipselect)或SS(slaveselect)SCK:serialclock,常常也写作SCLK或SCLMISO:masterinputslaveoutpu

    2022年6月18日
    53
  • PyPDF2 | 利用 Python 实现 PDF 分割

    PyPDF2 | 利用 Python 实现 PDF 分割1.PDF分割由于疫情影响被迫在家上网课,因此教材也只能用电子版。但有一门教材是对开的扫描版,导致在iPad上阅读很不友好,因此决定寻找一个工具将PDF对半分开。图1分割前的PDF在百度了一番后,发现大多都是使用AdobeAcrobat软件进行剪裁,这完全不Pythonic,因此又找了用Python处理PDF文件的方法,最后发现了PyPDF2这个库,本…

    2022年6月23日
    46
  • 50道经典MySQL练习题(含解答)

    50道经典MySQL练习题(含解答)本文摘要:本篇主要分享50道经典MySQL练习题(含解答)。

    2022年9月18日
    2
  • QUOTEName用法[通俗易懂]

    QUOTEName用法[通俗易懂]  1.可以用在拼接字符串时候将参数名称替换为参数值到SQL里面,如下面代码:DECLARE@TableNameVARCHAR(50),@SqlNVARCHAR(MAX),@DJIDINT;SET@TableName=’DJ’;SET@DJID=1991906354;SET@sql=’SELECT*FROM’+QUOTENAME(…

    2022年7月25日
    7
  • 三极管原理

    内容来源为21ic整理,侵权请联系删除。1.基本结构和类型半导体三极管的结构示意图如图1所示。它有两种类型:NPN型和PNP型。包含三层半导体:基区(相连电极称为基极,用B或b表示);发射区(相连电极称为发射极,用E或e表示);集电区(相连电极称为集电极,用C或c表示)。E-B间的PN结称为发射结,C-B间的PN结称为集电结。图1两类三极管示意图及图形符号2.电流分配与放大半导体三极管…

    2022年4月5日
    81
  • 上下行harq概念

    上下行harq概念参考前人一些关于 harq 总结 得出自己能够理解的东西

    2025年7月18日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号