There must be more than one interface test case in our daily projects, and how do we manage these batch cases as the case grows? How to ensure that cases are not duplicated? How do you ensure the efficiency of case execution when there are a lot of cases (hundreds, even more)? How to do (batch) test data management? How do you separate data from scripts?
These are some of the issues that we need to focus on in our automated testing, and a single use case is not really difficult.
Take a look at how to manage batch cases in the UnitTest framework:
First, manually load the batch use case:
#-*-Coding:utf-8-*-# Batch use case execution-manually loading import unittestclass testone (unittest. TestCase): def setUp (self): print ' \ncases before ' pass def test_add (self): ' Test Add method ' ' print ' Add ... ' a = 3 + 4 B = 7 self.assertequal (A, b) def test_sub (self): ' Test sub method ' ' print ' sub ... ' a = 10-5 b = 5 self.assertequal (A, b) def tearDown (self): print ' Case AF Ter ' passif __name__ = = ' __main__ ': # 1, construct use case set suite = UnitTest. TestSuite () # 2, the order of execution is the load order: Execute test_sub First, then execute Test_add suite.addtest (Testone ("Test_sub")) Suite.addtest (Testone ("Test_add")) # 3, Instantiate runner class runner = UnitTest. Texttestrunner ()
# 4, execute Test Runner.run (Suite)
Second, automatic loading of batch use cases:
#-*-Coding:utf-8-*-# Batch use case execution-auto load import unittestimport osclass testone (unittest. TestCase): def setUp (self): print ' \ncases before ' pass def test_add (self): ' Test Add method ' ' print ' Add ... ' a = 3 + 4 B = 7 self.assertequal (A, b) def test_sub (self): ' Test sub method ' ' print ' sub ... ' a = 10-5 b = 5 self.assertequal (A, b) def tearDown (self): print ' Case AF Ter ' passif __name__ = = ' __main__ ': # 1, set the directory for the case to be executed Test_dir = Os.path.join (OS.GETCWD ()) # 2, Automatically searches CAs in the specified directory, constructs the test set, executes the order of names: Executes the Test_add first, then executes test_sub discover = Unittest.defaultTestLoader.discover (test_ Dir, pattern= ' test_*.py ') # instantiates Texttestrunner class runner = UnitTest. Texttestrunner () # Run the test suite with the Run () method (that is, run all the use cases in the test suite) Runner.run (Discover)
The above only solves the problem of how to manage the batch case, other questions how to do (batch) test data management? How do you separate data from scripts? Follow-up in the introduction.
Python interface Automation test (vi) Using UNITTEST batch use case management