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 。

  • 相关阅读:
    MQTT Server搭建(apache-apollo)和MQtt Client搭建
    playbook 包含多个play
    ansible 批量重启服务
    无法执行 BACKUP LOG,因为当前没有数据库备份
    Microsoft.SqlServer.SmoExtended
    ACTIVEMQ主题、队列设置用户名密码
    Ansible的配置文件:
    MQTT协议之订阅及发布(使用paho-mqtt-client或mqttv3实现)
    采用基于MQTT的ActiveMQ实现消息推送
    ansible Introduction To Ad-Hoc Commands:
  • 原文地址:https://www.cnblogs.com/vwei/p/9880023.html
Copyright © 2011-2022 走看看