Skip to content

Fixture that calls getattr() on request.function throws an AttributeError: 'function' object has no attribute #1805

@arosszay

Description

@arosszay

Hello good people of pytest!

I have a major issue when trying to introspect the requesting test function from within a fixture. I am able to do the introspection successfully when doing it on the requesting test module, but NOT on the test function. I'd like for my fixture functions to be able to get data from my test functions.

Currently, I receive the following error:
> x = getattr(request.function, "url")
E AttributeError: 'function' object has no attribute 'url'

I've tried a couple things, like making the url variable in the test function to be nonlocal or global, using the concept of a closure(), ...all to no avail. I can't seem to understand how to make the fixture understand that yes, the 'function' object does indeed have an attribute 'url'.

I am using python 3.5.1 installed via the Anaconda 3 package on a Windows 10 machine. I run my code within Pycharm 2016.1.2 using pytest 2.9.1. I do not use a python virtual env.

Would a kind soul over here be able to help me with my situation? I seem to be stuck :/

Thanks!

Here's the fixture I wrote in a conftest.py file:
@pytest.fixture(scope="function")
def navigate_to(request):
"""Fixture used to determine the correct url to use when test execution starts"""
x = getattr(request.function, "url")
return x

Here's my test class including the test function I'm trying to make work (NOTE: my fixture 'login_and_nav' takes in the 'navigate_to' fixture above as a parameter):
@pytest.mark.usefixtures('login_and_nav')
class TestModuleNav(object):
"""Module Navigation Panel Selenium Tests."""

@pytest.mark.smoke
def test_diagnostics_module_loads(self):
    """Verifies the Diagnostics Module loads successfully."""

    url = URL.base_url
    login_credentials = Users.auto8, Client.demo_site, Provider=None

    # Verify the Home Page loaded successfully after login
    assert HomePage()
    # Verify the Module Navigation Pane is correct
    module_nav = ModuleNav()
    # Navigate to the Diagnostics Module
    diagnostics_module = module_nav.go_to_diagnostics()
    # Verify the Diagnostics Module is correct
    assert diagnostics_module.check_elements_visible(diagnostics_module.page_visible_element_list)`

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: questiongeneral question, might be closed after 2 weeks of inactivity

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions