instantiate class that uses pytest fixtures in another class

Viewed 195

I have these pytest fixture that I require for my project:

import pytest


def pytest_addoption(parser):
    parser.addoption("--environment", action="store", help="add environment")


@pytest.fixture(scope="class")
def params(request):
    request.cls.params = {'environment': request.config.getoption('--environment')}

I also have this code where I want to use this fixture:

import pytest


@pytest.mark.usefixtures("params")
class Page:
    def get_environment_url(self):
        if self.params['environment'] == 'local':
            url = "http://local.test.com"
            return url
        elif self.params['environment'] == 'qa':
            url = "https://qa.test.com"
            return url
        elif self.params['environment'] == 'stage':
            url = "https://cloud.test.com"
            return url
        elif self.params['environment'] is None:
            url = "http://local.test.com"
            return url


@pytest.mark.usefixtures("params")
class URL(Page):
    def url_formatting(self):
        url = '{}/#/login'.format(self.get_environment_url())
        return url


class TestURL:
    url = URL()

    def test_url(self):
        print(self.url.url_formatting())

I'm getting this error:

def get_environment_url(self):
    if self.params['environment'] == 'local':
E       AttributeError: 'URL' object has no attribute 'params'

not sure what I need to change to use the methods from class URL in TestURL class, any help would be appreciated.

1 Answers

Inherit URL into TestURL:

class TestURL(URL):

    def test_url(self):
        print(self.url_formatting())
        # can directly access self.params here as well
Related