1、按顺序执行
from selenium import webdriver import unittest import time class BaiduTest(unittest.TestCase): @classmethod def setUpClass(cls): cls.driver = webdriver.Chrome() cls.driver.maximize_window() url = 'http://www.baidu.com' cls.driver.get(url) cls.driver.implicitly_wait(30) @classmethod def tearDownClass(cls): cls.driver.quit() def test_baidu_news(self): self.driver.find_element_by_link_text("新闻").click() url = self.driver.current_url self.assertEqual(url,'http://news.baidu.com/') def test_baidu_map(self): self.driver.find_element_by_link_text("地图").click() self.driver.get("http://www.baidu.com") if __name__ =='__main__': suite = unittest.TestSuite() # TestSuite类实例化 suite.addTest(BaiduTest('test_baidu_map')) # 调用TestSuite类中addTest方法,将测试用例添加到测试套件中 suite.addTest(BaiduTest('test_baidu_news')) unittest.TextTestRunner().run(suite)
2、按测试类执行
from selenium import webdriver import unittest import time class BaiduTest(unittest.TestCase): @classmethod def setUpClass(cls): cls.driver = webdriver.Chrome() cls.driver.maximize_window() url = 'http://www.baidu.com' cls.driver.get(url) cls.driver.implicitly_wait(30) @classmethod def tearDownClass(cls): cls.driver.quit() def test_baidu_news(self): self.driver.find_element_by_link_text("新闻").click() url = self.driver.current_url self.assertEqual(url,'http://news.baidu.com/') def test_baidu_map(self): self.driver.find_element_by_link_text("地图").click() self.driver.get("http://www.baidu.com") def test_baidu_feiyan(self): self.driver.find_element_by_link_text("抗击肺炎").click() self.driver.get("http://www.baidu.com") if __name__ =='__main__': # suite = unittest.TestSuite() # suite.addTest(BaiduTest('test_baidu_map')) # suite.addTest(BaiduTest('test_baidu_news')) # unittest.TextTestRunner().run(suite) suite = unittest.TestSuite(unittest.makeSuite(BaiduTest)) # 调用了makeSuite unittest.TextTestRunner().run(suite)
3、加载测试类
from selenium import webdriver import unittest import time class BaiduTest(unittest.TestCase): @classmethod def setUpClass(cls): cls.driver = webdriver.Chrome() cls.driver.maximize_window() url = 'http://www.baidu.com' cls.driver.get(url) cls.driver.implicitly_wait(30) @classmethod def tearDownClass(cls): cls.driver.quit() def test_baidu_news(self): self.driver.find_element_by_link_text("新闻").click() url = self.driver.current_url self.assertEqual(url,'http://news.baidu.com/') def test_baidu_map(self): self.driver.find_element_by_link_text("地图").click() self.driver.get("http://www.baidu.com") def test_baidu_feiyan(self): self.driver.find_element_by_link_text("抗击肺炎").click() self.driver.get("http://www.baidu.com") if __name__ =='__main__': # suite = unittest.TestSuite() # suite.addTest(BaiduTest('test_baidu_map')) # suite.addTest(BaiduTest('test_baidu_news')) # unittest.TextTestRunner().run(suite) # suite = unittest.TestSuite(unittest.makeSuite(BaiduTest)) suite = unittest.TestLoader().loadTestsFromTestCase(BaiduTest) # 使用TestLoader类来加载测试类 unittest.TextTestRunner().run(suite)
4、按测试模块执行
from selenium import webdriver import unittest import time class BaiduTest(unittest.TestCase): @classmethod def setUpClass(cls): cls.driver = webdriver.Chrome() cls.driver.maximize_window() url = 'http://www.baidu.com' cls.driver.get(url) cls.driver.implicitly_wait(30) @classmethod def tearDownClass(cls): cls.driver.quit() def test_baidu_news(self): self.driver.find_element_by_link_text("新闻").click() url = self.driver.current_url self.assertEqual(url,'http://news.baidu.com/') def test_baidu_feiyan(self): self.driver.find_element_by_link_text("抗击肺炎").click() self.driver.get("http://www.baidu.com") class BaiduMap(unittest.TestCase): @classmethod def setUpClass(cls): cls.driver = webdriver.Chrome() cls.driver.maximize_window() url = 'http://www.baidu.com' cls.driver.get(url) cls.driver.implicitly_wait(30) @classmethod def tearDownClass(cls): cls.driver.quit() def test_baidu_map(self): self.driver.find_element_by_link_text("地图").click() self.driver.get("http://www.baidu.com") if __name__ =='__main__': # suite = unittest.TestSuite() # suite.addTest(BaiduTest('test_baidu_map')) # suite.addTest(BaiduTest('test_baidu_news')) # unittest.TextTestRunner().run(suite) # suite = unittest.TestSuite(unittest.makeSuite(BaiduTest)) # suite = unittest.TestLoader().loadTestsFromTestCase(BaiduTest) # 使用TestLoader类来加载测试类 suite = unittest.TestLoader().loadTestsFromModule('dd.py') # 两个测试类在同一个模块下 unittest.TextTestRunner().run(suite)
5、优化测试套件(将测试套件写成一个方法来调用)
from selenium import webdriver import unittest class BaiduTest(unittest.TestCase): @classmethod def setUpClass(cls): cls.driver = webdriver.Chrome() cls.driver.maximize_window() url = 'http://www.baidu.com' cls.driver.get(url) cls.driver.implicitly_wait(30) @classmethod def tearDownClass(cls): cls.driver.quit() def test_baidu_news(self): self.driver.find_element_by_link_text("新闻").click() url = self.driver.current_url self.assertEqual(url,'http://news.baidu.com/') def test_baidu_feiyan(self): self.driver.find_element_by_link_text("抗击肺炎").click() self.driver.get("http://www.baidu.com") @staticmethod def suite(testCaseClass): suite = unittest.TestLoader().loadTestsFromTestCase(testCaseClass) return suite if __name__ == '__main__': unittest.TextTestRunner().run(BaiduTest.suite(BaiduTest))