2015-09-04 103 views
1

我已經看到如何檢索運行Python Selenium Unittest的當前測試用例的名稱。檢索當前正在運行的測試用例的名稱

unittest.TestCase.id() 

如何使用Webdriver Py.test框架來實現這一目標?

from selenium import webdriver 
from selenium.webdriver.common.by import By 
from selenium.webdriver.common.keys import Keys 
from selenium.webdriver.support.ui import Select 
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions 
from selenium.common.exceptions import * 
import pytest, re, time, unicodedata 

from pageobjects import locators 
from os import sys, path 
sys.path.append(path.dirname(path.dirname(path.abspath(__file__)))) 



def test_PointsBlockingTableNavigationPageLinksBlockingPointsOnly02(driver, url): 

所以基本上,我想要檢索的名稱"test_PointsBlockingTableNavigationPageLinksBlockingPointsOnly02"要麼它打印到屏幕上或在文件名中使用:所以我的測試看起來是這樣的,我沒有使用單元測試框架。

回答

3

如果您使用unittest框架,這實際上是相同的。

Selenium是瀏覽器自動化工具,而不是測試框架。例如,我們在這裏有在使用硒unittest測試用例:

class MyTestCase(unittest.TestCase): 
    def setUp(self): 
     self.driver = webdriver.Firefox() 

    def test_title(self): 
     self.driver.get("https://google.com") 
     self.assertEqual(self.driver.title, "Google") 

pytest情況下,你有沒有考慮過使用pytest-splinter包? splinter是一個圍繞python硒綁定的方便包裝。該包裝can take automatic screenshots on failures

當您的功能測試失敗時,瞭解原因很重要。 當您在集成服務器上運行測試時,這變得很難,在集成服務器上無法調試(使用--pdb)。爲了簡化 的事情,瀏覽器夾具的一個特殊行爲是可用的,其中 截圖測試失敗,並將其放入一個文件夾中,命名約定與jenkins插件兼容。瀏覽器頁面的html內容 也被存儲,這對調試html源代碼很有用。

通常,pytest中的完整測試方法名稱爲nodeid。有多種方式可以檢索它。其中之一是有一個基於TerminalReporter閱讀失敗標題的自定義記者,請參閱pytest-wholenodeid的示例。

+0

@rwbyrd您能否給我一個示例測試,並詳細說明您爲什麼需要測試名稱?謝謝。 – alecxe

+0

我剛更新了說明。就像我所說的,我只想檢索當前正在運行的測試用例的名稱。我打算在我的錯誤代碼中使用它,這樣當我進行屏幕截圖時,屏幕截圖將會有與測試用例相關聯的名稱,但無法防止混淆。 – rwbyrd

+0

@rwbyrd感謝您的更新!你用鼻子或pytest來進行測試嗎.. – alecxe

相關問題