[Unittest] thick document read

reference:

https://blog.csdn.net/ljl6158999/article/details/80994979

 

1. The concept proposed

unittest originally inspired by Junit, it has a unit test and other similar language style. unittest supports test automation, sharing of setup and shutdown test code, the test polymerization to the collection, reporting and independent of the frame.

1 .unittest method has a global: setup and shutdown

2 plurality of. Testcase composition testsuite

3 .unittest testing and reporting are independent

 

Next, let's object-oriented perspective to understand some important concepts of unittest,

1.test fixture

test fixture pledged to prepare to run one or more test cases, or do after the end of the associated cleanup.

For example: Create a temporary / proxy databases, directories or start a service process

 

2.test case

Test smallest unit of content which will inputted bug check. We generate test case TestCase base class by inheriting

 

3.test suite

Testcase plurality composition testsuite, test suite to test for the polymerization and execute them together.

 

4.test runner

test runner is a component, a first role is to test tissue, the second is to provide the test results.

The test result may be a physical interface, a text interface, or return a special value. .

 

2.Base example

import unittest

class TestStringMethods(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'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split ( 2 )

if __name__ == '__main__':
    unittest.main()

 

1. To create a testcase, write a class to inherit unittest.TestCase. Example defines three methods test_upper (), test_isupper (), test_split (), which are the smallest test unit, the test cells together to form a test case. Pay attention to their names are required based on test beginning

2. assertEqual () : We used this method to check the desired result, by comparing the actual and expected values.

    assertTrue (), assetFalse () : Check a conditional expression, as judged by the expression True or False.

    assertRaises () : Verify that sparked a specified exception. An exception was thrown through, otherwise fail, as shown in the example s.split (2) raises a type error, we want to verify that the emergence of this error, case by!

. 3 the setUp (), the tearDown (). : Allows you to do something before or after each test method (the smallest test unit). General clean-up and initialization of ending.

4.  unittest.main () : providing a command line interface to execute test cases. At the command line, run the py file, it will return the test results report.

 

3. Command Line

...

...

 

4.Test Discovery

 For compatibility test discovery, all of the test files must be imported from a top-level project directory module or package.

test discovery inherited from TestLoader.discover (), you can use the command line, oh. Basic use:

cd project_directory
python -m unittest discover

 

Q: So this Test Discovery is doing it?

Recursive directory search from the beginning of the test module, the return value is a test module to test TestSuite these objects contain.

Q: What is imported from the project top level directory module or package?

do not know. . .

 

5.Organizing test code

 Continue...

 

 

 

 

 

 

 

 

 

 

 

   

 

Guess you like

Origin www.cnblogs.com/remly/p/12124051.html