Today, I wrote some unit tests using Python's UnitTest module, and now we have the following notes:
The basic format for using UnitTest is as follows:
ImportUnitTestclassTest (unittest. TestCase):defsetUp (self):Pass deftest_a (self):Pass defTest_b (self):Pass defTearDown (self):Passif __name__=='__main__': Unittest.main ()
Basically the online has already said very detailed, in the use process found the following points are more important:
1.unittest execution sequence, for each test, the first to execute the setup, and then execute teardown, not the setup after the completion of the incident, for the above format, Setup and teardown should be executed two times, rather than a
2. Each test is executed in alphabetical order of the function name, in other words, for the above format, even if the test_a,test_b is switched to a location, the test_a is executed first, then Test_b
The 3.assert method is extremely rich, besides the most basic assertequal,assertfalse, there are assertis,assertisnotnone and so on.
4.1 In UnitTest to see more detailed information, you can print, but less convenient, can be configured to use logging:
Import logginglogging.basicconfig (FileName='/users/wenli.xu/desktop/log1.log', Level=logging.info)
where filename indicates the log path, level indicates that only the severity is greater than or equal to info to be logged (the logging default is warning).
The severity is lighter to heavy: debug,info,warning,error,critical
The following methods can then be used to output critical information to the appropriate file:
Logging.info ('important info:%s', info)
If you use Logging.debug (' XXX '), the message will not be recorded in the log.
4.2 Use Logging.getlogger (' Log_name ') to get an instance of root logger (logging), with various personalized settings in logger
UnitTest and logging in Python