How unittest works, the order of execution in multiple test classes, and how to simplify

unit test
Unit testing refers to the inspection and verification of the smallest testable unit of software. For the meaning of a unit in unit testing, generally speaking, it is necessary to determine its specific meaning according to the actual situation. For example , a unit in C language refers to a function, in Java , a unit refers to a class, and graphical software can refer to a window or a menu. Wait. In general, the unit is the smallest artificially specified
The functional module under test.
unit testing framework
Before the advent of unit testing frameworks, developers struggled to create executable tests. The initial approach was to create a window in the application with a " test harness" . It's just a window with a button for each test. The result of these tests is either a message box or some kind of display result directly on the form itself. Since each test requires a button, these windows can quickly become crowded and unmanageable.
Unit testing frameworks provide a unified programming model by which tests can be defined as simple classes whose methods call the application code that you wish to test. Developers don't need to write their own test control tools; the unit testing framework provides a test runner that executes all tests with the click of a button. A unit testing framework makes it easy to plug in, set up, and decompose test-related functionality. When a test fails, the test runner can provide information about the failure, including any exploitable exception information and stack trace. Different programming languages ​​have different unit testing frameworks, such as Java 's Junit, TestNg, C# 's Nunit, Python 's unittest, Pyunit, testtools, subunit....  

  • An instance of TestCase is a test case.
  • What is a test case? It is a complete test process, including the setup of the pre-test preparation environment (setUp), the execution of the test code (run), and the restoration of the post-test environment (tearDown).
  • The essence of unit test is here. A test case is a complete test unit. By running this test unit, a certain problem can be verified.
  • A collection of multiple test cases is called TestSuite, and TestSuite can also nest TestSuite.
  • TestLoader is used to load TestCases into TestSuite. There are several loadTestsFrom__() methods, which are to find TestCases from various places, create their instances, add them to TestSuite, and return a TestSuite instance.
  • TextTestRunner is used to execute test cases, in which run(test) will execute the run(result) method in TestSuite/TestCase.
  • The results of the test will be saved to the TextTestResult instance, including how many test cases were run, how many were successful, and how many failed.
  • So what is a test fixture?
  • Test authors should subclass TestCase for their own tests. Construction and deconstruction of the test's environment ('fixture') can be implemented by overriding the 'setUp' and 'tearDown' methods respectively.
It can be seen that the construction and destruction of a test case environment is a fixture, which is realized by overriding the setUp() and tearDown() methods of TestCase.
What's the use of this?
(1) For example, if you need to access the database in this test case, you can establish a database connection and perform some initialization in setUp(), clear the data generated in the database in tearDown(), and then close the connection. It is important to note the tearDown process to leave a clean environment for future TestCases.
(2) Regarding fixtures, there is also a special library function called fixtures, which is more powerful and will be introduced later.

How unittest works

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324644908&siteId=291194637