unittest中更高效的執行測試用例一個類只須要打開一次瀏覽器

示例代碼web

baidu.py瀏覽器

# _*_ coding:utf-8 _*_
import csv,unittest #導入csv模塊
from time import sleep
from selenium import webdriver
from module import baidumodule
class baidu(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.maximize_window()#最大化窗口
        self.driver.implicitly_wait(10)#隱式等待
        self.search = baidumodule(self.driver) #將driver傳給aidumodule這個類
        with open("file.csv","r") as name:
            self.lines = name.readlines()#以行讀取整個文件
    def tearDown(self):
        self.driver.quit()

    def test_search(self):
        search = self.search
        lines = self.lines
        driver = self.driver
        search.login(lines[0])
        sleep(1)
        title = driver.title
        self.assertEqual(title,'selenium_百度搜索2')
        sleep(2)
    def test_search1(self):
        search = self.search
        lines = self.lines
        driver = self.driver
        search.login(lines[1])
        sleep(1)
        title = driver.title
        self.assertEqual(title,'selenium2_百度搜索')
        sleep(2)
    def test_search2(self):
        search = self.search
        lines = self.lines
        driver = self.driver
        search.login(lines[2])
        sleep(1)
        title = driver.title
        self.assertEqual(title,'selenium3_百度搜索')
        sleep(2)
    def test_search3(self):
        search = self.search
        lines = self.lines
        driver = self.driver
        search.login(lines[3])
        sleep(1)
        title = driver.title
        self.assertEqual(title,'webdriver_百度搜索')
        sleep(2)

if __name__ == "__main__": #若是直接執行將執行如下代碼,調用不執行如下代碼
    unittest.main()

module.pysvg

class baidumodule():
    def __init__(self,driver,):
        self.dr = driver #不能在類中再次導入webdriver 兩邊的driver等於兩個窗口,直接讓調用方傳入driver便可
    def login(self,values):
        login_dr = self.dr
        login_dr.get("https://www.baidu.com/")
        login_dr.find_element_by_xpath("//*[@id='kw']").send_keys(values)
        login_dr.find_element_by_xpath("//*[@id='su']").click()

baidu.py執行結果測試

....
----------------------------------------------------------------------
Ran 4 tests in 49.660s #運行4條耗時49.660s

OK

以上代碼都可成功執行可是執行過程當中每一個測試用例都須要再次打開瀏覽器,這會大大拖延執行時間,咱們可使用@classmethod裝飾器將@classmethod所裝飾的方法改成類方法即setUpClasstearDownClass讓每類執行只須要開啓一次瀏覽器便可(在unittest中默認如此).ui

@classmethod
    def setUpClass(cls):
        "Hook method for setting up class fixture before running tests in the class."

@classmethod
    def tearDownClass(cls):
        "Hook method for deconstructing the class fixture after running all tests in the class."

更改後的baidu.pycode

# _*_ coding:utf-8 _*_
import csv,unittest #導入csv模塊
from time import sleep
from selenium import webdriver
from module import baidumodule
class baidu(unittest.TestCase):
    @classmethod
    def setUpClass(cls):#類中最早執行
        cls.driver = webdriver.Chrome()
        cls.driver.maximize_window()  # 最大化窗口
        cls.driver.implicitly_wait(10)  # 隱式等待
        cls.search = baidumodule(cls.driver)  # 將driver傳給aidumodule這個類
        with open("file.csv","r") as name:
            cls.lines = name.readlines()#以行讀取整個文件
    @classmethod
    def tearDownClass(cls): #類中最後執行
        cls.driver.quit()

    def test_search(self):
        search = self.search
        lines = self.lines
        driver = self.driver
        search.login(lines[0])
        sleep(1)
        title = driver.title
        self.assertEqual(title,'selenium_百度搜索')
        sleep(2)
    def test_search1(self):
        search = self.search
        lines = self.lines
        driver = self.driver
        search.login(lines[1])
        sleep(1)
        title = driver.title
        self.assertEqual(title,'selenium2_百度搜索')
        sleep(2)
    def test_search2(self):
        search = self.search
        lines = self.lines
        driver = self.driver
        search.login(lines[2])
        sleep(1)
        title = driver.title
        self.assertEqual(title,'selenium3_百度搜索')
        sleep(2)
    def test_search3(self):
        search = self.search
        lines = self.lines
        driver = self.driver
        search.login(lines[3])
        sleep(1)
        title = driver.title
        self.assertEqual(title,'webdriver_百度搜索')
        sleep(2)

if __name__ == "__main__": #若是直接執行將執行如下代碼,調用不執行如下代碼
    unittest.main()

執行結果:xml

....
----------------------------------------------------------------------
Ran 4 tests in 23.124s#執行4條耗時23.124s

OK

能夠看到每次打開瀏覽器再執行至少多一倍的時間。blog

  • setUpClass/tearDownClass: 在測試類的開始與結束時被執行。
  • setUp/tearDown : 在測試用例的開始與結束時被執行。須要注意的是,setUpClass/tearDownClass 的寫法稍微有些不一樣。首先,須要經過@classmethod 進行裝飾,其次方法的參數爲 cls。其實,cls 與 self 並無什麼特別之處,都只表示類方法的第一個參數,只是你們約定俗成,習慣於這樣來命名,固然也能夠用 abc 來代替。

特別注意

使用@classmethod裝飾器時不要把要測試的網頁放置到setUpClass中那樣執行完第一個用例時不會再次打開瀏覽器,致使參數化等第二條用例只會在原地執行,因此要把連接放置到測試用例中。
以下是錯誤示例:utf-8

@classmethod
   def setUpClass(cls):
        cls.dr = webdriver.Chrome()
        cls.dr.maximize_window()
        cls.dr.implicitly_wait(10)
        cls.dr.get("https://www.baidu.com/")

正確寫法:ci

def test_search(self,search_str,aa):  # 接收上面的兩個參數
        '''百度測試用例'''
        self.dr.get("https://www.baidu.com/")#這樣執行下個用例纔會再次打開連接
        self.dr.find_element_by_id("kw").send_keys(search_str)
        self.dr.find_element_by_id("su").click()
        sleep(2)
相關文章
相關標籤/搜索