test suite
Using the Test Suite feature of unittest, you can group different tests into a logical group, and then set up a unified test suite to execute the tests together. The test suite is created by the TestSuite and TestLoader classes, and finally the TestRunner class is used to execute the test suite.
Before using it we add a new test for the example (Baidu) to check the homepage. The new test code is as follows:
import unittest from selenium import webdriver class BaiduHomePageTest(unittest.TestCase): @classmethod def setUpClass(cls): cls.driver = webdriver.Chrome() cls.driver.implicitly_wait(30) cls.driver.maximize_window() cls.driver.get('https://www.baidu.com') def test_baidu_title(self): tag = self.driver.title self.assertEqual( " Baidu, you will know " ,tag) def test_baidu_homepape(self): tag = self.driver.find_element_by_link_text( " About Baidu " ).text self.assertIn( " Baidu " ,tag) @classmethod def tearDownClass(cls): cls.driver.quit() if __name__ == '__main__': unittest.main(verbosity=2)
Then the new test and the previous test will be put into a test component with the following code:
import unittest from searchtests import BaiduSearchTest from homepagetests import BaiduHomePageTest #get all tests from BaiduSearchTest and BaiduHomePageTest class search_tests = unittest.TestLoader().loadTestsFromTestCase(BaiduSearchTest) home_page_tests = unittest.TestLoader().loadTestsFromTestCase(BaiduHomePageTest) #create a test suite combing search_tests and home_page_tests test_suite = unittest.TestSuite([home_page_tests, search_tests]) #run the suite unittest.TextTestRunner(verbosity=2).run(test_suite)
Using the TestLoader class, you will get all the test methods in the specified test file, create the test suite with the TestSuite class, and finally the TestRunner class will run all the tests in the file by calling the test suite.
The results are as follows:
To learn more about TestSuite refer to:
https://docs.python.org/3.6/library/unittest.html#unittest.TestSuite