案例:百度搜索关键词:“unittest”
test_baidu.py:
from selenium import webdriver from time import sleep import unittest class TestBaidu(unittest.TestCase): def setUp(self): self.driver = webdriver.Firefox() self.driver.maximize_window() self.driver.implicitly_wait(10) self.driver.get("http://www.baidu.com") def test_baidu(self): driver = self.driver driver.find_element_by_id("kw").clear() driver.find_element_by_id("kw").send_keys("unittest") driver.find_element_by_id("su").click() sleep(2) title = driver.title self.assertEqual(title,"unittest_百度搜索") def tearDown(self): self.driver.quit() if __name__ == '__main__': unittest.main()
执行测试用例
runtest.py:
import unittest test_dir = './test_case' discover = unittest.defaultTestLoader.discover(test_dir, pattern='test*.py') if __name__ == '__main__': runner = unittest.TextTestRunner() runner.run(discover)
在本案例中,将unittest和Python的webdriver结合在一起,从而实现最简单的Web测试用例的执行