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
2) Define a test case class: the class name must start with Test and inherit unittest.TestCase
3) Add setUp(), tearDown() functions, that is, test fixtures
4) Define the test method: the test method must start with test and must be written in the test case class
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
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:
operation result:
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:
operation result:
7. How to collect use cases
1) Take the test case as the dimension
2) Take the test class as the dimension
3) Take the module as the dimension