Unit testing framework (PyUnit)

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, unit tests are the smallest human-defined functional modules to be tested.

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 one button for each test pair. The result of these tests is either a message box or some kind of display result directly on the form itself. Since every 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 do not need to write their own test control tools; the unit testing framework provides a test runner (runner) that executes all tests at the click of a button. Leveraging 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, testtools, subunit...

unit testing framework

  • Provide use case organization and execution
  • Provides rich assertion methods
  • Provides rich logs and test results

Python unit testing framework - UnitTest

unittetest official documentation: https://docs.python.org/2.7/library/unittest.html

The unittest unit testing framework is not only suitable for unit testing, but also can be used to automate the development and execution of web test cases. The test framework can organize and execute test cases, and provides a wealth of assertion methods to determine whether the test cases pass, and finally generate test results.


unittest core elements

1. TestCase

An instance of TestCase is a test case. A test case is a complete test process, including the preparation of the environment before the test (setUp), the execution of the test code (run), and the restoration of the environment after the test (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.

2. TestSuite

A collection of multiple test cases is TestSuite, and TestSuite can also nest TestSuite. TestLoader is used to load test cases into TestSuite.

3. TextTestRunner

TextTestRunner is used to execute test cases, where run() executes 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.

4. Fixture

The construction and destruction of a test case environment is Fixture.


case

First define a Math method for testing:

Then write specific unit tests:


Affirmation

The content of the assertion is an important part of the automation script. Only after the assertion is set correctly can it help us judge the execution result of the test case.

unittest provides a number of assertion methods:

  • assertEqual(a,b) Judging a==b, tending to numerical judgment
  • assertNotEqual(a,b) Judging a!=b, tending to numerical judgment
  • assertTrue(x)          bool(x) is True
  • assertFalse(x)          bool(x) is False
  • assertIs(a,b) judge that a is b
  • assertIsNot(a,b) judge a is not b
  • assertIsNone(x)      x是None
  • assertIsNotNone(x) x is not None
  • assertIn(a,b)    判断a in b
  • assertNotIn(a,b)   判断a not in b
  • assertIsInstance(a,b)          isinstance(a,b)
  • assertNotIsInstance(a,b)         not isinstance(a,b)



Guess you like

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