• python3_unittest单元测试框架


    看见英文懵逼,强迫学习英语

    The Unittest suppots test automation,sharing of setup and shutdown code of tests, aggregation of tests into collections, and independence of the tests from the reporting framework

    (支持测试自动化,为测试共享设置和关闭代码,将测试集合到集合中,以及从报告框架中独立测试。)

    To achieve this,unittest supports some important concepts in an object_oriented way:

    (为了实现这一点,unittest以面向对象的方式支持一些重要的概念:)

    整理结构:unittest库提供了testcase,test suites,test fixtures, test runner:

    test fixture:

      A test fixture represents the preparation needed to perform one or more tests, and any associate cleanup actions. This may involve, for example, creating   temporary or proxy databases, directories, or starting a server process.

    test fixture代表执行一个或多个测试所需的准备工作,以及任何关联清理操作。这可能涉及到,例如创建临时代理数据库、目录或启动服务器进程。

    test case :

    A test case is the individual unit of testing. It checks for a specific response to a particular set of inputs. unittest provides a base class, TestCase, which may be used to create new test cases.

    测试用例是测试的各个单元。它检查对特别输入集合的特定响应。 unittest提供了一个基类, TestCase可以用来创建新的测试用例。

    test suite:

            A test suite is a collection of test cases, test suites, or both. It is used to aggregate tests that should be executed together.

    一个测试套件是测试案例,测试套件,或两者的集合。它被用来聚合应该一起执行的测试。

    test runner:

    A test runner is a component which orchestrates the execution of tests and provides the outcome to the user. The runner may use a graphical interface, a textual interface, or return a special value to indicate the results of executing the tests.

    测试运行是编排的测试的执行,并提供结果给用户的部件。test runner可以使用图形界面,文本界面,或返回一个特殊的值来表示执行测试的结果。

     

    The unittest module provides a rich set of tools for constructing and running tests. This section demonstrates that a small subset of the tools suffice to meet the needs of most users.

    unittest模块为构建和运行测试提供了一套丰富的工具。本节表明,一小部分工具足以满足大多数用户的需求。

    1、简单的例子

    Here is a short script to test three string methods:

     1 import unittest
     2 
     3 class TestStringMethods(unittest.TestCase):
     4     def test_upper(self):
     5         self.assertEqual('foo'.upper(), 'FOO')
     6 
     7     def test_isupper(self):
     8         self.assertTrue('FOO'.isupper())
     9         self.assertFalse('Foo'.isupper())
    10 
    11     def test_split(self):
    12         s = 'hello word'
    13         self.assertEqual(s.split(), ['hello', 'word'])
    14         # check that s.split fails when the separator is not a string
    15         with self.assertRaises(TypeError):
    16             s.split(2)
    17 
    18 if __name__ == '__main__':
    19     unittest.main()

    运行结果:

    ...
    ----------------------------------------------------------------------
    Ran 3 tests in 0.000s
    
    OK
    Passing the -v option to your test script will instruct unittest.main() to enable a higher level of verbosity, and produce the following output:
    test_isupper (__main__.TestStringMethods) ... ok
    test_split (__main__.TestStringMethods) ... ok
    test_upper (__main__.TestStringMethods) ... ok
    
    ----------------------------------------------------------------------
    Ran 3 tests in 0.001s
    
    OK

    2、命令行:

    从命令行中可以运行单元测试的模块,甚至单独的测试方法

    python -m unittest test_module1 test_module2
    python -m unittest test_module.TestClass
    python -m unittest test_module.TestClass.test_method

    测试模块也可以通过文件路径指定:

    python -m unittest tests/test_something.py

    You can run tests with more detail (higher verbosity) by passing in the -v flag(显示更详细的测试结果的说明使用[-v]flag:)

    ython3_server/python3_unittest# python3 -m unittest -v demo1.py 
    test_isupper (demo1.TestStringMethods) ... ok
    test_split (demo1.TestStringMethods) ... ok
    test_upper (demo1.TestStringMethods) ... ok
    
    ----------------------------------------------------------------------
    Ran 3 tests in 0.000s
    
    OK

    查看所有的命令行选项使用命令:

    python -m unittest -h

    The -s-p, and -t options can be passed in as positional arguments in that order. The following two command lines are equivalent:

    python -m unittest discover -s project_directory -p "*_test.py"
    python -m unittest discover project_directory "*_test.py"

    TestCase类提供了一些断言方法来检查并报告故障。下表列出了最常用的方法

    方法检查
    assertEqual(a, b) == b
    assertNotEqual(a, b) != b
    assertTrue(x) bool(x) is True
    assertFalse(x) bool(x) is False
    assertIs(a, b) is b
    assertIsNot(a, b) is not b
    assertIsNone(x) is None
    assertIsNotNone(x) is not None
    assertIn(a, b) in b
    assertNotIn(a, b) not in b
    assertIsInstance(a, b) isinstance(a, b)
    assertNotIsInstance(a, b) not isinstance(a, b)

    所有的assert方法都接受一个msg参数,如果指定的话,这个参数将被用作失败时的错误消息

    3、实际运用:

     

    参考:

    https://docs.python.org/3.6/library/unittest.html

    https://www.jianshu.com/p/8e22c1213260 

    
    
  • 相关阅读:
    OpenCV学习(7)--
    OpenCV学习(6)--更多形态转化、Hit-or-Miss变换、Hit-or-Miss变换、图像金字塔
    Linux基本操作
    设计模式
    利用Python进行数据分析:【Matplotlib】
    利用Python进行数据分析:【Pandas】(Series+DataFrame)
    利用Python进行数据分析:【NumPy】
    利用Python进行数据分析:【IPython】
    数据结构与算法(C/C++版)【排序】
    《操作系统》学习笔记
  • 原文地址:https://www.cnblogs.com/ranxf/p/8316499.html
Copyright © 2020-2023  润新知