UnitTest and logging in Python

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.