Python unittest Understanding

Source: Internet
Author: User

First, the operation process

Second, how to use:

1, define an inheritance unittest. TestCase class in which the Test_ beginning method is the test case at the time of execution

############################name:test_unittest.py#!/usr/bin/env python#_*_ coding:utf-8 _*_defAdd (A, b):returnA +bdefMinus (A, b):returnA-bdefmulti (A, B):returnA *bdefDivide (A, b):returnA/b
###########################name:test_case.py#!/usr/bin/env python#_*_ coding:utf-8 _*_Importtest_unittestImportUnitTestclassMyTest (unittest. TestCase):defTest_add (self): self.assertequal (5,test_unittest.add (2,3))    defTest_minus (self): self.assertequal (2,test_unittest.minus (5,3))    defTest_multil (self): self.assertequal (6,test_unittest.multi (2,3))    deftest_divide (self): self.assertequal (2,test_unittest.divide (6,2))if __name__=='__main__': Unittest.main (verbosity=2)

2, the above direct execution, you can add multiple test methods to a testsuile (test suite), execute a test suite

#############################################name:test_unittest.py#!/usr/bin/env python#_*_ coding:utf-8 _*_defAdd (A, b):returnA +bdefMinus (A, b):returnA-bdefmulti (A, B):returnA *bdefDivide (A, b):returnA/b############################################name:test_case.py#!/usr/bin/env python#_*_ coding:utf-8 _*_Importtest_unittestImportUnitTestclassMyTest (unittest. TestCase):defTest_add (self): self.assertequal (5,test_unittest.add (2,3))    defTest_minus (self): self.assertequal (2,test_unittest.minus (5,3))classMyTest2 (unittest. TestCase):defTest_multil (self): self.assertequal (6,test_unittest.multi (2,3))    deftest_divide (self): self.assertequal (2,test_unittest.divide (6,2))if __name__=='__main__': Unittest.main (verbosity=2)#############################################
#name: test_suite.py#!/usr/bin/env python#_*_ coding:utf-8 _*_Importtest_unittest fromTest_caseImport*ImportUnitTestif __name__=='__main__': Suite=UnitTest. TestSuite () suite.addtests (unittest. Testloader (). Loadtestsfromnames (['Test_case. MyTest','Test_case. MyTest2'])) Runner= UnitTest. Texttestrunner (verbosity=2) Runner.run (suite)

3. Output the result to a file

4. SetUp () and teardown ()

5. Skip a Use case

6. Using Htmltestrunner

Python unittest Understanding

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.