【问题标题】:Retrieving the name of the current test case running检索当前正在运行的测试用例的名称
【发布时间】:2015-09-04 14:54:37
【问题描述】:

我已经了解了如何检索当前为 Python Selenium Unittest 运行的测试用例的名称。

unittest.TestCase.id()

如何使用 Webdriver Py.test 框架实现这一点?我没有使用 unittest 框架,所以我的测试看起来像这样:

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" 以将其打印到屏幕上或在文件名中使用。

【问题讨论】:

    标签: python testing selenium-webdriver pytest


    【解决方案1】:

    如果您使用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 包? splinter 是 python selenium 绑定的便捷包装器。包can take automatic screenshots on failures

    当您的功能测试失败时,了解原因很重要。 当在 continuos 上运行测试时,这变得很困难 无法调试的集成服务器(使用 --pdb)。简化 事情,浏览器固定装置的特殊行为是可用的,它 截取测试失败的屏幕截图并将其放入带有 a 的文件夹中 与 jenkins 插件兼容的命名约定。 html内容 浏览器页面的也被存储,这对于调试很有用 html 源代码。

    通常,pytest 术语中的完整测试方法名称称为nodeid。有多种方法可以检索它。其中之一是让一个基于TerminalReporter 的自定义报告器读取失败标题,请参阅pytest-wholenodeid 的示例。

    【讨论】:

    • @rwbyrd 您能否展示一个您拥有的示例测试并详细说明为什么需要测试名称?谢谢。
    • 我刚刚更新了描述。就像我说的,我只想检索当前正在运行的测试用例的名称。我计划在我的错误代码中使用它,这样当我拍摄屏幕截图时,屏幕截图的名称将与无法防止混淆的测试用例相关联。
    • @alecxe 我是个白痴……哈哈。如果我说我使用的是 Py.test,那肯定会对你有所帮助,呵呵 :-)。
    • @rwbyrd 是的,现在好多了。我已经更新了答案并添加了一个 pytest 特定选项。看看吧。
    • 感谢@alecxe 的 pytest-splinter 建议。我会看看!同时,在我实现 pytest-splinter 包之前,有没有办法检索测试用例名称以打印在屏幕上或在文件名中使用?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-09
    • 1970-01-01
    • 1970-01-01
    • 2012-01-12
    • 2014-05-08
    • 2010-09-07
    • 1970-01-01
    相关资源
    最近更新 更多