1.导入测试用例需要的模块,unittest是python的内置模块,它提供了组织测试用例的框架

import unittest   # 导入测试用例的模块

  

2.测试用例继承于unittest

class baiduSearch(unittest.TestCase):

  

3.SetUp方法是初始化的一部分,在每个测试功能之前被调用

    def setUp(self):
self.driver = webdriver.Ie()

  

4.创建测试用例,测试用例的方法名称尽量以test字符串开头

5.测试用例执行完毕后会调用tearDown方法,主要工作是执行清理工作

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

  

6.函数入口是执行测试套件的固定写法

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

完整代码如下:

# -*- coding: utf-8 -*-
# @Time : 2018/5/28 11:16
# @Author : Nancy
# @Email : NancyWangDL@163.com
# @File : TestCaseBaidu.py
# @Software: PyCharm from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys
import unittest import unittest # 导入测试用例的模块
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time class baiduSearch(unittest.TestCase): def setUp(self):
self.driver = webdriver.Ie() def test_baiduSearch(self):
driver = self.driver
driver.get("https://www.baidu.com/") time.sleep(3) driver.find_element_by_id("kw").send_keys("python")
driver.find_element_by_id("kw").send_keys(Keys.ENTER) def tearDown(self):
self.driver.quit() # 函数入口,固定写法
if __name__ == "__main__":
unittest.main()

  

05-15 01:55
查看更多