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

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

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • 测试用例等价类划分法讲解_等价类分析法设计用例的方法

    测试用例等价类划分法讲解_等价类分析法设计用例的方法1.提交缺陷报告遇到的问题1.不知道是否全面测试了所有的内容(1)是不是所有的功能点都测试到了(2)是不是每个功能点都测试全面了2.存在大量冗余测试,影响测试效率(1)有些功能点可能测试多次3.对新版本的测试效果很难实施(1)每个版本测试的数据、步骤都不一样,随意性很强4.测试的覆盖率无法衡量(1)测试的好坏不得而知5.……为了避免以上问题,所以做测试用例,对测试过程可控,对测试质量有把握。2.什么是测试用例?(1)测试用例主要记录了测试的目的、步骤、输入的数据、预期结果等内容,它

    2022年10月17日
  • Sql server–事务

    Sql server–事务

  • 介绍篇 决策引擎环节

    介绍篇 决策引擎环节决策引擎概念简述在我理解上决策引擎类似是一个管道、运输系统,连通整个风控流程,所有的规则和评分卡以及流程都覆盖其中,分配到每一个环节(比如人工),将结果返回给决策引擎,走入下一个流程决策引擎的使用规则决策引擎的分流效果评分卡是内置在决策引擎当中,基于评分卡的分段,评分卡的使用具体参见:评分卡在策略中的使用,进行分流,分流决策的目的是为让好客户以及有借款欲望客户进一步走入下一流程决策引擎…

  • MATLAB 绘制折线图

    MATLAB 绘制折线图MATLAB绘制折线图想要绘制出如上图所示折线图,首先,先展示代码:x=0:10:50;a=[0,1.80,7.60,17.40,31.20,49.00]plot(x,a,’s-g’,’MarkerSize’,2,’MarkerFaceColor’,’g’,’MarkerEdgeColor’,’g’,’LineWidth’,2);gridb=[0,1.10,4.20,9.30,1…

  • python中sql拼接字符串过程

    python中sql拼接字符串过程a,b(是数字)sql="insertintostock_staffVALUES(a,b,c,d)"sql="insertintostock_staffVALUES("+a,b,c,d+")"sql="insertintostock_staffVALUES("+a+","+b+","+c+","

  • SprinBoot——SpringBoot项目WebSocket推送

    SprinBoot——SpringBoot项目WebSocket推送SprinBoot——SpringBoot项目WebSocket推送

发表回复

您的电子邮箱地址不会被公开。

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