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

    Setting up and tearing down a test harness.With the following steps, we will setup and
    teardown a test harness for each test method.
    1. Create a new file called recipe2.py in which to put all our code for this recipe.

    2. Pick a class to test. In this case, we will use a slightly altered version of our Roman
    numeral converter, where the function, not the constructor, provides the input value
    to convert.

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

    4. Create a setUp method that creates an instance of the class under test.

    5. Create a tearDown method that destroys the instance of the class under test.

    6. Create all the test methods using self.converter.

    7. Make the entire script runnable and then use the test runner of unittest.
    if __name__ == "__main__":
    unittest.main()

    8. Run the file from the command line.

    附源码:

    Code

    结果:

    Creating a new RomanNumeralConverter...
    Destroying the RomanNumeralConverter...
    .Creating a new RomanNumeralConverter...
    Destroying the RomanNumeralConverter...
    .Creating a new RomanNumeralConverter...
    Destroying the RomanNumeralConverter...
    .Creating a new RomanNumeralConverter...
    Destroying the RomanNumeralConverter...
    .Creating a new RomanNumeralConverter...
    Destroying the RomanNumeralConverter...
    .Creating a new RomanNumeralConverter...
    Destroying the RomanNumeralConverter...
    .Creating a new RomanNumeralConverter...
    Destroying the RomanNumeralConverter...
    .Creating a new RomanNumeralConverter...
    Destroying the RomanNumeralConverter...
    .
    ----------------------------------------------------------------------
    Ran 8 tests in 0.001s

    OK

    由结果可以看出,每一个testcase均会重新装载setUp一次

  • 相关阅读:
    【图像处理】【SEED-VPM】7.ubuntu10.04下 TFTP,NFS 安装指南
    【图像处理】【SEED-VPM】6.文件目录结构
    【DIY】【外壳】木板 & 亚克力 加工
    【图像处理】【SEED-VPM】5.uImage的烧写 & NFS烧写文件系统
    【PCB】【AD使用】Altium Designer 的entry sheet ,offsheet和port作用
    【PCB】【项目记录】AWG任意波形产生器
    【图像处理】【SEED-VPM】4.串口调试信息
    【图像处理】【SEED-VPM】3.外设信息
    【图像处理】【SEED-VPM】2.接口
    【图像处理】【SEED-VPM】1.注意点
  • 原文地址:https://www.cnblogs.com/luhouxiang/p/2560034.html
Copyright © 2011-2022 走看看