Unittest Web用例编写

实践案例

百度搜索关键词:“Selenium自学网” 并打开课程页面。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from selenium import webdriver
import unittest
from time import sleep

class TestBaidu(unittest.TestCase):
def setUp(self):
self.driver=webdriver.Firefox()
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("Selenium自学网")
driver.find_element_by_id("su").click()
sleep(3)

title=driver.title
self.assertEqual(title,"Selenium自学网_百度搜索")

driver.find_element_by_partial_link_text("Selenium自动化").click()
sleep(5)

def tearDown(self):
self.driver.quit()

if __name__ == '__main__':
unittest.main()

执行测试用例

1
2
3
4
5
6
7
import unittest
test_dir='./test_case'

discover=unittest.defaultTestLoader.discover(test_dir,pattern="test*.py")
if __name__ == '__main__':
runer=unittest.TextTestRunner()
runer.run(discover)