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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • docker部署jenkins安装使用教程_docker安装python

    docker部署jenkins安装使用教程_docker安装python前言使用docker安装jenkins环境,jenkins构建的workspace目录默认是在容器里面构建的,如果我们想执行python3的代码,需进容器内部安装python3的环境。进jenki

    2022年7月30日
    4
  • 实体-联系图(ER图)_实体关系图

    实体-联系图(ER图)_实体关系图我们通常用实体、联系和属性这三个概念来理解现实问题,因此ER模型比较接近人的思维方式。此外,ER模型用简单的图形符号表达系统分析员对问题域的理解,不熟悉计算机技术的用户也能理解它,因此,ER模型可以作为用户与分析员之间有效的交流工具。一、ER模型ER模型中包含3种相互关联的信息:数据对象、数据对象的属性及数据对象彼此之间相互连接的关系。1.数据对象数据对象是对软件必须理解的复合信息…

    2022年9月15日
    0
  • js nextSibling函数「建议收藏」

    js nextSibling函数「建议收藏」<!DOCTYPEhtml><html><head><metacharset=”utf-8″><title>菜鸟教程(runoob.com)</title><scriptsrc=”https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js”>&…

    2022年7月13日
    12
  • Couchbase vs Redis,究竟哪个更胜一筹?

    Couchbase vs Redis,究竟哪个更胜一筹?

    2022年2月19日
    43
  • BS架构和CS架构的区别

    BS架构和CS架构的区别介绍BS:(Browser/Server,浏览器/服务器模式),web应用可以实现跨平台,客户端零维护,但是个性化能力低,响应速度较慢。CS:(Client/Server,客户端/服务器模式),桌面级应用响应速度快,安全性强,个性化能力强,响应数据较快区别硬件环境不同C/S用户固定,一般只应用于局域网中,要求拥有相同的操作系统,如果对于不同操作系统还要相应开发不同的版本,并且对…

    2022年6月15日
    28
  • 数组转对象2种方法有哪些_js 数组转对象

    数组转对象2种方法有哪些_js 数组转对象constarr=[{label:’男’,value:0},{label:’女’,value:1}]functionf(arr){//写代码//reducereturnarr.reduce((obj,item)=>{obj[item.value]=item.labelreturnobj},{})}constobj=f(arr)console.log(ob…

    2022年9月10日
    0

发表回复

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

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