zoukankan      html  css  js  c++  java
  • python从入门到实践-11章测试模块(测试函数出问题)

    #!/user/bin/env python
    # -*- coding:utf-8 -*-

    # 用python中unittes中工具来测试代码

    # 1.测试函数
    import unittest
    from name_function import get_formatted_name


    class NamesTestCase(unittest.TestCase): # 必须继承unittest.TestCase这个类
    # 测试name_function.py

    def test_first_last_name(self): # 能够正确处理 janis joplin 这样的姓名吗?
    formatted_name = get_formatted_name('janis', 'joplin')
    self.assertEqual(formatted_name, 'Janis Joplin') # 断言方法:用来核实得到结果与期望是否一样

    # def test_first_last_middle_name(self):
    # formatted_name = get_formatted_name('wolfgang', 'mozart', 'amadeus')
    # self.assertEqual(formatted_name, 'Wolfgang Amadeus Mozart')


    unittest.main()


    # 2.测试类

    # ———————————————————————unittest Module 中的断言方法——————————————————————
    # 方 法 用 途
    # ------------------------------------------------------------------------
    # assertEqual(a,b) 核实 a==b
    # assertNotEqual(a,b) 核实 a!=b
    # assertTure(x) 核实 x为Ture
    # assertFalse(x) 核实 x为False
    # assertIn(item, list) 核实item在list中
    # assertNotIn(item, list) 核实item不在list中

    # import unittest
    # from survey import AnonymouseSurvey
    #
    #
    # class TestAnonymouseSurvey(unittest.TestCase):
    # # def test_store_single_response(self):
    # # question = "What language did you first learn to speak?"
    # # my_survey = AnonymouseSurvey(question)
    # # my_survey.store_response('English')
    # #
    # # self.assertIn('English', my_survey.responses)
    # #
    # # def test_store_three_response(self):
    # # question = "What language did you first learn to speak?"
    # # my_survey = AnonymouseSurvey(question)
    # # responses = ['English', 'Spanish', 'Chinese', ]
    # # for response in responses:
    # # my_survey.store_response(response)
    # #
    # # for response in responses:
    # # self.assertIn(response, my_survey.responses)
    #
    # def setUp(self):
    # question = "What language did you first learn to speak?"
    # self.my_survey = AnonymouseSurvey(question)
    # self.responses = ['English', 'Spanish', 'Chinese', ]
    #
    # def test_store_single_response(self):
    # self.my_survey.store_response(self.responses[0])
    # self.assertIn(self.responses[0], self.my_survey.responses)
    #
    # def test_store_three_respones(self):
    # for response in self.responses:
    # self.my_survey.store_response(response)
    # for response in self.responses:
    # self.assertIn(response, self.my_survey.responses)
    #
    #
    # unittest.main

    # 注意:每完成一个单元测试python都会打印一个字符;通过打印 . ;引发错误打印 E ;断言失败打印 F 。

  • 相关阅读:
    【Python应用:基于PyQt5文本识别】调用百度AI对一张或多张图片、文件夹中的图片和屏幕区域截图进行识别(PDF转Word 小意思)
    【Ubuntu搭建Django编程环境】:创建python虚拟开发环境和配置pip国内镜像源
    23种设计模式上篇
    荷兰国旗问题
    文件复制多份
    mybatis批量更新
    数组小和
    常见排序算法
    福尔摩斯的约会
    小明上学
  • 原文地址:https://www.cnblogs.com/vwei/p/9880023.html
Copyright © 2011-2022 走看看