python 单元测试

unittest

https://docs.python.org/2/library/unittest.html

A test­case is cre­at­ed by sub­class­ing unittest.TestCase. The three indi­vid­u­al tests are defined with meth­ods whose names start with the let­ters test. This nam­ing con­ven­tion informs the test run­ner about which meth­ods rep­re­sent tests.

The crux of each test is a call to assertE­qual() to check for an expect­ed result; assert­True() or assert­False() to ver­i­fy a con­di­tion; or asser­tRais­es() to ver­i­fy that a speci­fic excep­tion gets raised. The­se meth­ods are used instead of the assert state­ment so the test run­ner can accu­mu­late all test results and pro­duce a report.

The setUp() and tear­Down() meth­ods allow you to define instruc­tions that will be exe­cut­ed before and after each test method. They are cov­ered in more detail in the sec­tion Orga­niz­ing test code.

The final block shows a sim­ple way to run the tests. unittest.main() pro­vides a com­mand-line inter­face to the test script. When run from the com­mand line, the above script pro­duces an out­put that looks like this:

Leave a Reply

Your email address will not be published. Required fields are marked *