Detailed explanation of unittest for unit testing framework

1. Introduction to unittest

unittest is python's own unit testing framework, which is not only suitable for unit testing, but also for the development and execution of web, appium, and interface automation test cases.

 

2. The four core components of unittest

1) TestCase: Test case class, used to define use case functions

2) TestSuite: Test suite for collecting test cases

3) TestRunner: Test case running class, used to execute test cases, and execute in the dimension of test suite

4) TestFixture: test scaffolding (front and rear), pre-position conditions, and post-position for cleaning

 

3. Test process

1) First define the test case class and write the test method in the test case class

2) Define the test suite, collect the test methods in the test case class or file, and put them in the test suite

3) Define the use case runner, put the collected test suite into the use case runner for execution, and collect the test results

4) Do a good job of front and rear

 

4. How to write test cases

1) Import the unittest module

d3e685bb8cab4f97a20692e6c2340d6e.png

 2) Define a test case class: the class name must start with Test and inherit unittest.TestCase

781d600ce72a42f9a2bf078c2e1ee82b.png

 3) Add setUp(), tearDown() functions, that is, test fixtures

097ffaca2fa645c28d3cff5ad8a9f605.png

 4) Define the test method: the test method must start with test and must be written in the test case class

1d3dfaf3a273406ea7502e321c67da12.png

 5) The framework executes the entry main method: unittest.main(), which will automatically collect the test cases of the current py file, and then execute

e2695eff2b70407589b71b0707db8a87.png

 

5. Contents of test cases

1) Preconditions (if not written), such as: sql statement execution, database creation, instantiation of tool classes, etc.

2) Test steps (business logic), data replacement, data-driven, etc.

3) Test result assertion, including: corresponding result assertion, database data assertion

4) Post-conditions (if not written), such as: data cleaning, database connection closing, etc.

 

 6. Front and rear (test fixture)

1) Function level

setUp(): pre-executed once before each use case function is executed

tearDown(): After each use case function is executed, it is executed once

Just like a sandwich biscuit, the front and rear are equivalent to two biscuits with cream, and the cream in the middle is equivalent to the use case

Code example:

de1a871ca30f43efb7d6d757b6a0e9d5.png

 operation result:

59e74a302dc8483a895be4c0c35801dc.png

2) Class level

setUpClass(cls): Pre-executed once before all test cases in the current use case class are executed

tearDownClass(cls): After all the test cases in the current use case class are executed, execute once

Code example:

e3ea43a300984c09a3b97f1f8d150d76.png

operation result:

080916aef7a241e3ac4e879e1da734d5.png

 

 7. How to collect use cases

1) Take the test case as the dimension

85a8da8fce8248159f600ac38b81bd12.png

2) Take the test class as the dimension

0412b6264cf6413eb8b1de186a090ee5.png

 3) Take the module as the dimension

83826984ee13460b8daa9ccbb6ac0e31.png

 

 

 

 

Guess you like

Origin blog.csdn.net/m0_37794269/article/details/127392489
Recommended