• 第十一章 测试代码


    目的:学习如何使用Python模块unittest 中的工具来测试代码

       

    1. 单元测试和测试用例:

      单元测试 用于核实函数的某个方面没有问题;测试用例 是一组单元测试, 这些单元测试一起核实函数在各种情形下的行为都符合要求。

    2. 测试函数

      步骤:导入unittest模块→再创建一个继承unit.TestCase的类→编写一系列方法对不同的方面进行测试→利用断言方 法(assertEqual)用来核实得到的结果是否与期望的结果一致→unittest.main() 让Python运行这个文件中的测试

    • 测试成功时:

    01 import unittest

    02 from name_function import get_formatted_name;

    03 class NamesTestCase(unittest.TestCase):

    04 """测试Name_function.py"""

    05

    06 def test_first_last_name(self):

    07 """能够正确处理Janis Joplin这样的姓名吗"""

    08 formatted_name=get_formatted_name('janis','joplin');

    09 self.assertEqual(formatted_name,'Janis Joplin');

    10

    11

    12

    13 unittest.main()

    >>>

    .

    ----------------------------------------------------------------------

    Ran 1 test in 0.007s

       

    OK

    第1行的句点表明有一个测试通过了。 接下来的一行指出Python运行了一个测试, 消耗的时间不到0.007秒。 最后的OK 表明该测试用例中的所有单元测试都通过了

    • 测试失败时:

    F

    ======================================================================

    FAIL: test_first_last_name (__main__.NamesTestCase)

    能够正确处理Janis Joplin这样的姓名吗

    ----------------------------------------------------------------------

    Traceback (most recent call last):

    File "C:/Users/Franz/Desktop/prac.pth/test_name_function.py", line 9, in test_first_last_name

    self.assertEqual(formatted_name,'Janis Joplin');

    AssertionError: None != 'Janis Joplin'

       

    ----------------------------------------------------------------------

    Ran 1 test in 0.042s

       

    FAILED (failures=1)

    1. 测试类

      各种断言方法

      测试语句与测试函数类似,可以用 unittest.TestCase 类包含的方法setUp(),使得只需创建这些对象一次, 并在每个测试方法中使用它们。

    01 import unittest;

    02 from survey import AnonymousSurvey;

    03

    04 class TestAnonymouseSurvey(unittest.TestCase):

    05 """针对AnonyouseSurvey类的测试"""

    06

    07 def setUp(self):

    08 """

    09 创建一个调查对象和一组答案,供测试方法使用

    10 """

    11 question="What language did you first learn to speak?";

    12 self.my_survey=AnonymousSurvey(question);

    13 self.responses=['English','Spanish','Mandarin'];

    14

    15 def test_store_single_response(self):

    16 """测试单个答案是否能被妥善的存储"""

    17 self.my_survey.store_response('English');

    18 self.assertIn('English',self.my_survey.responses);

    19

    20 def test_store_three_responses(self):

    21 """测试三个函数会被妥善的保存吗"""

    22 for response in self.responses:

    23 self.my_survey.store_response(response);

    24 for response in self.responses:

    25 self.assertIn(response,self.my_survey.responses);

    26

    27 unittest.main()

    >>>

    ..

    ----------------------------------------------------------------------

    Ran 2 tests in 0.050s

       

    OK

       

       

       

       

       

       

       

       

       

    为更美好的明天而战!!!
  • 相关阅读:
    java 常用
    面试题目总结
    前端自动化构建工具gulp记录
    js面向对象学习笔记
    sass,compass学习笔记总结
    JS核心知识点:DOMBOMEVENT
    boost atomic
    boost thread
    boost function bind ref
    boost phoenix
  • 原文地址:https://www.cnblogs.com/lovely-bones/p/11024300.html
Copyright © 2020-2023  润新知