rwbyrd
rwbyrd

Reputation: 416

Retrieving the name of the current test case running

I've seen how to retrieve the name of the current test case running for Python Selenium Unittest.

unittest.TestCase.id()

How to achieve this using Webdriver Py.test framework? I'm not using unittest framework so my test looks something like this:

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):

So basically, I want to retrieve the name "test_PointsBlockingTableNavigationPageLinksBlockingPointsOnly02" to either print it to the screen or use in a filename.

Upvotes: 1

Views: 1697

Answers (1)

alecxe
alecxe

Reputation: 474241

This is actually the same if you are using the unittest framework.

Selenium is a browser automation instrument and not a test framework. For instance, here we have a unittest test case where selenium is used:

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")

In case of pytest, have you considered using 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.

In general, the full test method name in pytest terms is called nodeid. There are multiple ways to retrieve it. One of which is to have a custom reporter based on TerminalReporter reading the failure headline, see example at pytest-wholenodeid.

Upvotes: 3

Related Questions