检索当前测试用例的名称 运行

Retrieving the name of the current test case running

我已经了解了如何为 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" 以将其打印到屏幕或在文件名中使用。

如果您使用 unittest 框架,这实际上是相同的。

Selenium 是浏览器自动化工具,而不是测试框架。例如,这里我们有一个使用 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 package? splinter is a convenient wrapper around python selenium bindings. The package can take automatic screenshots on failures:

When your functional test fails, it's important to know the reason. This becomes hard when tests are being run on the continuos integration server, where you cannot debug (using --pdb). To simplify things, a special behaviour of the browser fixture is available, which takes a screenshot on test failure and puts it in a folder with the a naming convention compatible to the jenkins plugin. The html content of the browser page is also stored, this can be useful for debugging the html source.

一般来说,pytest术语中的完整测试方法名称称为nodeid。有多种方法可以检索它。其中之一是根据 TerminalReporter 阅读失败标题的自定义记者,请参阅 pytest-wholenodeid 中的示例。