zoukankan      html  css  js  c++  java
  • pythonunittest(8)

    将晦涩难懂的测试分解成简单的测试

    本篇的目的是解说晦涩的测试不易一目了然,然后用一个例子将晦涩的测试用例化为简单的测试用例,使得测试结果一目了然。

    Breaking down obscure tests into simple ones.

    Unittest provides the means to test the code through a series of assertions. I have often felt
    the temptation to exercise many aspects of a particular piece of code within a single test
    method. If any part fails, it becomes obscured as to which part failed. It is preferable to split
    things up into several smaller test methods, so that when some part of the code under test
    fails, it is obvious.

    1. Create a new file named recipe8.py in which to put our application code for this
    recipe.

    2. Pick a class to test. In this case, we will use an alternative version of the Roman
    numeral converter, which converts both ways.

    3. Create a new file called recipe8_obscure.py in which to put some longer
    test methods.

    4. Create some test methods that combine several test assertions.

    5. Run the obscure tests. Why did it fail? Where is the bug? It reports that II is not
    equal to I, so something appears to be off. If this the only bug?

    6. Create another file called recipe8_clear.py to create a more fine-grained set of
    test methods.

    7. Split up the assertions into separate test methods to give a higher fidelity of output.

    8. Run the clearer test suite. Is it a bit clearer where the bug is? What did we trade in to
    get this higher degree of test failure? Was it worth the effort?

    测试代码1:

    Code
    Code

    结果输出:(这样的结果让人弄不明白倒底哪些用例发生了错误,不如下面用例那样的一目了然)

    .F
    ======================================================================
    FAIL: test_convert_to_roman (__main__.RomanNumeralTest)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "e:\study\python\4668_Code\Chapter 1\01\recipe8_obscure.py", line 22, in test_convert_to_roman
        self.assertEquals("II", self.cvt.convert_to_roman(2))
    AssertionError: 'II' != 'I'

    ----------------------------------------------------------------------
    Ran 2 tests in 0.001s

    FAILED (failures=1)
    Process terminated with an exit code of 1

    测试代码2:

    Code


    结果输出:

    .FFFFF....
    ======================================================================
    FAIL: test_convert_to_roman2 (__main__.RomanNumeralTest)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "e:\study\python\4668_Code\Chapter 1\01\recipe8_clear.py", line 30, in test_convert_to_roman2
        self.assertEquals("II", self.cvt.convert_to_roman(2))
    AssertionError: 'II' != 'I'

    ======================================================================
    FAIL: test_convert_to_roman3 (__main__.RomanNumeralTest)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "e:\study\python\4668_Code\Chapter 1\01\recipe8_clear.py", line 33, in test_convert_to_roman3
        self.assertEquals("V", self.cvt.convert_to_roman(5))
    AssertionError: 'V' != 'IIII'

    ======================================================================
    FAIL: test_convert_to_roman4 (__main__.RomanNumeralTest)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "e:\study\python\4668_Code\Chapter 1\01\recipe8_clear.py", line 37, in test_convert_to_roman4
        self.cvt.convert_to_roman(12))
    AssertionError: 'XII' != 'XI'

    ======================================================================
    FAIL: test_convert_to_roman5 (__main__.RomanNumeralTest)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "e:\study\python\4668_Code\Chapter 1\01\recipe8_clear.py", line 41, in test_convert_to_roman5
        self.cvt.convert_to_roman(2010))
    AssertionError: 'MMX' != 'MMVIIII'

    ======================================================================
    FAIL: test_convert_to_roman6 (__main__.RomanNumeralTest)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "e:\study\python\4668_Code\Chapter 1\01\recipe8_clear.py", line 45, in test_convert_to_roman6
        self.cvt.convert_to_roman(4000))
    AssertionError: 'MMMM' != 'MMMDCCCCLXXXXVIIII'

    ----------------------------------------------------------------------
    Ran 10 tests in 0.001s

    FAILED (failures=5)
    Process terminated with an exit code of 1

  • 相关阅读:
    DFS+剪枝:N个蛋放入M个篮子并可以任意取
    笔试题:二叉树按层遍历&添加兄弟指针&求LCA&排序二叉树的查找
    Windows下部署BigBlueButton
    Gcc 下 MAX/MIN的安全宏定义
    Java NIO 笔记
    C++高效编程:内存与性能优化
    <<<EOT分界符怎么用?
    查询语句中不区分大小写和区分大小写及其模糊查询 的语句
    APPCAN本地打包时报有中文字符错误
    PHP中::、>、self、$this操作符的区别
  • 原文地址:https://www.cnblogs.com/luhouxiang/p/2560298.html
Copyright © 2011-2022 走看看