• python unittest自动测试框架


    编写函数或者类时进行测试,确保代码正常工作

    python  unittest 模块提供了代码测试工具。按照定义测试包括两部分:管理测试依赖库的代码(称为‘固件’)和测试本身。

    单元测试用于核实函数的某个方面没有问题;

    测试用例是一组单元测试,这些单元测试一起核实函数在各种情况选的行为都符合要求

    # Author:song
    import unittest
    
    class TestString(unittest.TestCase):
    
        def test_upper(self):
            self.assertEqual('Foo'.upper(),'FOO')
    
        def test_isupper(self):
            self.assertTrue('FOO'.isupper())
            self.assertFalse('Foo'.isupper())
    
        def test_split(self):
            s= 'hello world'
            self.assertEqual(s.split(),['hello','world'])
            with self.assertRaises(TypeError):
                s.split(2)
    
    
    if __name__=="__main__": #运行测试
        unittest.main()

      unittest.main():使用它可以将一个单元测试模块变为可直接运行的测试脚本,main()方法使用TestLoader类来搜索所有包含在该模块中以“test”命名开头的测试方法,并自动执行

     断言真值

    大部分测试会断言某个条件的真值

      Python在unittest.TestCase类中提供了很多断言方法

    常见几个

    测试固件

      固件是测试所需要的外部资源。例如一个类的所有测试可能需要另一个类的实例(用来提供配置设置)或另一个共享资源。配置固件需要覆盖setUp()。完成清理需要覆盖tearDown()

    import unittest
    
    class FixturesTest(unittest.TestCase):
        def setUp(self):
            print('in setup')
            self.fixture = range(1,10)
    
        def tearDown(self):
            print('in teardown')
            del self.fixture
    
        def test(self):
            print('in test')
            self.assertEqual(self.fixture,range(1,10))
    
    if __name__ =="__main__":
        unittest.main()

    结果:

    in setup
    in test
    in teardown
    .
    ----------------------------------------------------------------------
    Ran 1 test in 0.000s

    
    

    OK

    
    

      unittest.TestCase类包含方法setUp(),让我们只需创建这些对象一 次,并在每个测试方法中使用它们。

    AnonymousSurvey.py

    class AnonymousSurvey():#创建匿名调查的类
        def __init__(self,question):
            self.question = question
            self.responses = []
    
        def show_question(self):
            print(self.question)
    
        def store_response(self,new_response):
            self.responses.append(new_response)
    
        def show_results(self):
            print('Survey result:')
            for response in self.responses:
                print('-'+response)

    测试代码,

    import unittest
    class TestAnonymousSurvey(unittest.TestCase):
        def setUp(self):
            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): #测试单个答案会被妥善地存储
            self.my_survey.store_response(self.responses[0])
            self.assertIn(self.responses[0], self.my_survey.responses)
    
        def test_store_three_responses(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()

      方法setUp()做了两件事情:创建一个调查对象;创建一个答案列表。存储这两样东西的变量名包含前缀self(即存储在属性中),因此可在这个类的任何地方使用。这让两个测试方法都更简单,因为它们都不用创建调查对象和答案。方法setUp()让测试方法编写起来更容易,相比于在每个测试方法中都创 建实例并设置其属性,这要容易得多。

    运行结果

    ..
    ----------------------------------------------------------------------
    Ran 2 tests in 0.000s
    
    OK

      运行测试用例时,每完成一个单元测试,Python都打印一个字符:测试通过时打印一个 句点;测试引发错误时打印一个E;测试导致断言失败时打印一个F。这就是你运行测试 用例时,在输出的第一行中看到的句点和字符数量各不相同的原因。如果测试用例包含 很多单元测试,需要运行很长时间,就可通过观察这些结果来获悉有多少个测试通过了

     

    测试结果

    ok:测试通过。

    FAIL:测试没有通过产生了一个AssertionError异常。

    ERROR:测试产生了AssertionError以外的一个异常。

  • 相关阅读:
    期望的线性性
    排列组合问题选胡
    ABAP Control Framework(9): Tree
    ABAP Control Framework(8): Toolbar
    ABAP Control Framework(7): Picture
    ABAP Control Framework(6): Calendar
    ABAP Control Framework(5): 不同Control之间Drag & Drop事件
    ABAP Control Framework(4): ALV List
    ABAP Control Framework(3): Text Editer
    ABAP Control Framework(2): HTML Viewer
  • 原文地址:https://www.cnblogs.com/master-song/p/8908433.html
Copyright © 2020-2023  润新知