0

current code:

conftest.py

@pytest.fixture(scope="class")
def setup_portal(request):    ### NOTE: I want this to be: def setup_portal(request,login=False)

    "@pytest.mark.usefixtures" : applied to the class ==  a fixture to every test methods in the class.
    Since the fixture "setup_portal" has a scope == "class" ->  webdriver will be initialized only once per each class."""

    # setup chrome driver ...      
    ## login into portal
    
    # TODO: 
    ## if login: # login 


    request.cls.driver = chrome_driver
    yield chrome_driver
    chrome_driver.close()

test_file.py

@pytest.mark.usefixtures("setup_portal") ### initialize driver
class TestMusimotionPage:
     ... do stuff

The goal: request setup_portal fixture with a parameter: login == True and then login

One solution for a similar problem is to define "setup_portal" as a method in the fixture as shown here: Can I pass arguments to pytest fixtures?

The issue: the fixture is being called using:

@pytest.mark.usefixtures("setup_portal")

so I cannot pass an argument as such:

conftest.py

@pytest.fixture(scope="class")
def setup_portal():
    def _setup_portal(request, login=False):    

        "@pytest.mark.usefixtures" : applied to the class ==  a fixture to every test methods in the class.
        Since the fixture "setup_portal" has a scope == "class" ->  webdriver will be initialized only once per each class."""

        # setup chrome driver ...      
        ## login into portal

        if login:
              ## login...
    
        request.cls.driver = chrome_driver
        yield chrome_driver
        chrome_driver.close()

   return _setup_portal()

test_file.py

@pytest.mark.usefixtures("setup_portal") ### not sure how to pass : login == True
class TestMusimotionPage:
     setup_portal(login==True). ## doesn't work
     ... do stuff

what do?

tigertiger
  • 73
  • 1
  • 9
  • 1
    your return statement should be `return _setup_portal`, you need to omit the parens – gold_cy May 21 '21 at 18:20
  • oh oops, I'll try that later. thanks – tigertiger May 22 '21 at 17:48
  • @gold_cy I can't get it working b/c TestMusimotionPage can't do : setup_portal(login=True), so i dk how to pass that parameter. I tried to paramterize the fixture call and access it via request.params buut that doesn't work b/c it's a class definition. needs to be accessed via mark.usefixtures not mark.paramterize – tigertiger May 26 '21 at 16:34

0 Answers0