运行下面的两段代码,看看有什么不同?
第一段:
import unittest from selenium import webdriver class F2(unittest.TestCase): @classmethod def setUpClass(cls): cls.driver = webdriver.Chrome() cls.driver.maximize_window() cls.driver.implicitly_wait(30) cls.driver.get("http://www.baidu.com/") @classmethod def tearDownClass(cls): cls.driver.quit() def test_baidu_news(self): self.driver.find_element_by_link_text("新闻").click() self.driver.back() def test_baidu_map(self): self.driver.find_element_by_link_text("地图").click() self.driver.back() if __name__ == "__main__": unittest.main()
第二段:
import unittest from selenium import webdriver class F2(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.maximize_window() self.driver.implicitly_wait(30) self.driver.get("http://www.baidu.com/") def tearDown(self): self.driver.quit() def test_baidu_news(self): self.driver.find_element_by_link_text("新闻").click() def test_baidu_map(self): self.driver.find_element_by_link_text("地图").click() if __name__ == "__main__": unittest.main()
总结:第一段代码只打开一次浏览器,第二段代码有几个测试用例就打开几次浏览器。