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

    重组旧的测试代码,使用场景,之前有一段未使用unittest的测试代码,通过另一个包装文件使其运作起来,日常生活中极少用到。

    Retooling old test code to run inside unittest.

    Sometimes, we may have developed demo code to exercise our system. We don't have to
    rewrite it to run it inside unittest. Instead, it is easy to hook it up to the test framework and run
    it with some small changes.

    1. Create a file named recipe7.py in which to put our application code that we
    will be testing.

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

    3. Create a new file named recipe7_legacy.py to contain test code that doesn't use
    the unittest module.

    4. Create a set of legacy tests that are coded, based on Python's assert function, not
    with unittest, along with a runner.

    5. Run the legacy tests. What is wrong with this situation? Did all the test methods run?
    Have we caught all the bugs?

    6. Create a new file called recipe7_pyunit.py.

    7. Create a unittest set of tests, wrapping each legacy test method inside unittest's
    FunctionTestCase.

    8. Run the unittest test. Did all the tests run this time? Which test failed? Where is
    the bug?

    测试代码:

    Code


    Code
    Code

    输出结果:

    unittest.case.FunctionTestCase (simple_test) ... +++ Converting M to 1000
    ok
    unittest.case.FunctionTestCase (combo_test1) ... +++ Converting MMX to 2010
    FAIL
    unittest.case.FunctionTestCase (combo_test2) ... +++ Converting MMMMDCLXVIII to 4668
    FAIL
    unittest.case.FunctionTestCase (other_test) ... +++ Converting MMMM to 4000
    ok

    ======================================================================
    FAIL: unittest.case.FunctionTestCase (combo_test1)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "e:\study\python\4668_Code\Chapter 1\01\recipe7_legacy.py", line 17, in combo_test1
        assert self.cvt.convert_to_decimal("MMXX") == 2010
    AssertionError

    ======================================================================
    FAIL: unittest.case.FunctionTestCase (combo_test2)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "e:\study\python\4668_Code\Chapter 1\01\recipe7_legacy.py", line 22, in combo_test2
        self.check(val, 4668)
      File "e:\study\python\4668_Code\Chapter 1\01\recipe7_legacy.py", line 32, in check
        (actual, expected))
    AssertionError: 4667 doesn't equal 4668

    ----------------------------------------------------------------------
    Ran 4 tests in 0.001s

    FAILED (failures=2)


     

  • 相关阅读:
    常用搜索指令
    chrome浏览器常用快捷键
    倒排文档
    hdu4570Multi-bit Trie (间隙DP)
    HTTP工作原理
    腾讯和58都市“聘请”秘诀是什么?
    Atitit。团队建设--管理最佳实践--如何留住关键人才,防止人才外流 ??
    于Eclipse传导C/C++配置方法开发(20140721新)
    通过京东淘宝的技术发展和技术演进,探索未来的技术和体系结构
    C++ Primer 学习笔记_41_STL实践与分析(15)--先来看看算法【下一个】
  • 原文地址:https://www.cnblogs.com/luhouxiang/p/2560290.html
Copyright © 2011-2022 走看看