zoukankan      html  css  js  c++  java
  • Python 测试代码 初学者笔记

    单元测试

    每完成一个单元测试,Python都会打印一个字符:

    测试通过打印一个句点;测试引发错误打印E;测试导致断言失败打印F

    模块unittest

    import unittest
    from name_function import get_formatted_name#导入要测试的函数
    
    class NamesTestCase(unittest.TestCase):#注意类的参数哦
        """方法名字开头test,测试时会自动执行"""
        
        def test_first_last_name(self):
            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()#让python运行这个文件测试
    

     方法setUP

    import unittest
    from survey import AnonymousSurvey
    
    class TestAnonymousSurvey(unittest.TestCase):
        """Tests for the class AnonymousSurvey."""
        
        def setUp(self):#TestCase类中包含了方法setUP Python将先运行它再运行以test_为前缀的方法
            """用来创建测试实例对象       """
            question = "What language did you first learn to speak?"
            self.my_survey = AnonymousSurvey(question)
            self.responses = ['English', 'Spanish', 'Mandarin']
            
        
        def test_store_single_response(self):
            """Test that a single response is stored properly."""
            self.my_survey.store_response(self.responses[0])
            self.assertIn(self.responses[0], self.my_survey.responses)
            
            
        def test_store_three_responses(self):
            """Test that three individual responses are stored properly."""
            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()
    

                                                             

  • 相关阅读:
    oracle 11g行转列 列转行
    System.Data.OracleClient 需要 Oracle 客户端软件 8.1.7 或更高版本
    ORA-00257: 归档程序错误。在释放之前仅限于内部连接
    java 回传参数
    Oracle 删除表分区
    Oracle 删除重复数据只留一条
    cmd下windows批处理,获取当前系统时间,生成日志文件名
    c# 应用程序部署发布
    eclipse 集成 STS 插件
    Mamen所需要的jar包怎么生成
  • 原文地址:https://www.cnblogs.com/MR---Zhao/p/12348960.html
Copyright © 2011-2022 走看看