5 ways Python uses the UnitTest test framework to organize test cases

Source: Internet
Author: User

Using the UnitTest test framework, you can write test cases in two main ways: using the Main method and leveraging Testsuite, where you can use a test suite to organize your test cases in 4 ways.

Before you do this, you should understand a few concepts

    • TestCase: The basic class of all test cases, given the name of a test method, returns a test case instance;
    • TestSuite: an instance of a test case is organized to support the addition and deletion of test cases and will eventually be passed to Testrunner for test execution;
    • Texttestrunner: an instance of the execution of a test case where text means to display the test results in textual form. The results of the test are saved to the Texttestresult instance, including the number of test cases that were run, the number of successes, and the number of failed messages;
    • testloader: Used to load testcase into Testsuite, there are several loadtestsfrom__ () methods, which are to find testcase from various places, create instances of them, Then add to Testsuite, and then return a testsuite instance;
    • defaulttestloader.discover: The use cases filtered by the Discover method are added to the test suite, and the printed use case information is incremented

Method One: How to execute the test case through Unittest.main ()

ImportUnitTestclassTeststringmethods (unittest. TestCase):defsetUp (self): UnitTest. Testcase.setup (self)defTearDown (self): UnitTest. Testcase.teardown (self)defTest_upper (self): self.assertequal ('Foo'. Upper (),'FOO')            defTest_isupper (self): Self.asserttrue ('FOO'. Isupper ()) Self.assertfalse ('Foo'. Isupper ())defTest_split (self): s='Hello World'self.assertequal (S.split (), ['Hello',' World']) with Self.assertraises (TypeError): S.split (2)if __name__=="__main__":    #Import sys;sys.argv = [' ', ' test.testname ']Unittest.main ()

Unittest.main () will find all the methods that begin with test and use them as a test case

Method Two: Use the test kit

ImportUnitTestclassTeststringmethods (unittest. TestCase):defsetUp (self): UnitTest. Testcase.setup (self)defTearDown (self): UnitTest. Testcase.teardown (self)defTest_upper (self): self.assertequal ('Foo'. Upper (),'FOO')            defTest_isupper (self): Self.asserttrue ('FOO'. Isupper ()) Self.assertfalse ('Foo'. Isupper ())defTest_split (self): s='Hello World'self.assertequal (S.split (), ['Hello',' World']) with Self.assertraises (TypeError): S.split (2)    defSuite (): Suite=UnitTest. TestSuite () suite.addtest (Teststringmethods ('Test_upper')) Suite.addtest (Teststringmethods ('Test_isupper') ) UnitTest. Texttestrunner (verbosity=2). Run (Suite)if __name__=="__main__": Suite ()#Import sys;sys.argv = [' ', ' test.testname ']#Unittest.main ()

Method Three: Use Testloader

ImportUnitTestclassTestCase1 (unittest. TestCase):#def setUp (self):    #def tearDown (self):    deftestCase1 (self):Print 'AAA'          defTestCase2 (self):Print 'BBB'  classTestCase2 (unittest. TestCase):#def setUp (self):    #def tearDown (self):    deftestCase1 (self):Print 'Aaa1'      defTestCase2 (self):Print 'BBB1'        if __name__=="__main__":    #This usage can test multiple classes at the same timeSuite1 =UnitTest. Testloader (). Loadtestsfromtestcase (TestCase1) Suite2=UnitTest. Testloader (). Loadtestsfromtestcase (TESTCASE2) Suite=UnitTest. TestSuite ([Suite1, Suite2]) unittest. Texttestrunner (verbosity=2). Run (Suite)

Method Four: A degeneration of the test suite, similar to the method two

ImportUnitTestclassTeststringmethods (unittest. TestCase):defsetUp (self): UnitTest. Testcase.setup (self)defTearDown (self): UnitTest. Testcase.teardown (self)defTest_upper (self): self.assertequal ('Foo'. Upper (),'FOO')            defTest_isupper (self): Self.asserttrue ('FOO'. Isupper ()) Self.assertfalse ('Foo'. Isupper ())defTest_split (self): s='Hello World'self.assertequal (S.split (), ['Hello',' World']) with Self.assertraises (TypeError): S.split (2)    defSuite (): Tests= ['Test_upper','Test_isupper'] Suite=UnitTest. TestSuite (Map (teststringmethods,tests)) unittest. Texttestrunner (verbosity=2). Run (Suite)if __name__=="__main__": Suite ()#Import sys;sys.argv = [' ', ' test.testname ']#Unittest.main ()

Method Five: Use Discover

ImportUnitTestclassTeststringmethods (unittest. TestCase):defsetUp (self): UnitTest. Testcase.setup (self)defTearDown (self): UnitTest. Testcase.teardown (self)defTest_upper (self): self.assertequal ('Foo'. Upper (),'FOO')            defTest_isupper (self): Self.asserttrue ('FOO'. Isupper ()) Self.assertfalse ('Foo'. Isupper ())defTest_split (self): s='Hello World'self.assertequal (S.split (), ['Hello',' World']) with Self.assertraises (TypeError): S.split (2)    defSuite (): Suite=UnitTest. TestSuite () Discover=unittest.defaulttestloader.discover (Casepath, pattern='test_*.py', top_level_dir=None)#The use cases filtered by the Discover method are added to the test suite, and the printed case information is incremented      forTest_caseinchdiscover:suite.addTests (test_case)Print(Suite)returnSuiteif __name__=="__main__": All_test_cases=Suite () unittest. Texttestrunner (). Run (all_test_cases)#Import sys;sys.argv = [' ', ' test.testname ']#Unittest.main ()

5 ways Python uses the UnitTest test framework to organize test cases

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.