• 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 。

  • 相关阅读:
    iOS 发送位置消息
    集成 jpush-react-native 常见问题汇总 (iOS 篇)
    RESTful API 最佳实践
    RESTful API 设计指南
    理解RESTful架构
    PHP 调用 shell
    Laravel Model updating&updated 事件使用注意事项
    sudo 命令报 unable to resolve host 导致反应速度变慢
    Nginx设置禁止通过IP访问服务器并且只能通过指定域名访问
    Homestead can not mount nfs on macos catalina
  • 原文地址:https://www.cnblogs.com/vwei/p/9880023.html
Copyright © 2020-2023  润新知