danladis
danladis

Reputation: 49

Running the same pytest with different setup

So I want to run the same test multiple times (preferably in parallel, but thats another story), just with different variables for the setup. I need to test the same thing on different devices, so I have to change the device name on every setup. Here is how far I've got, but its not working:

import pytest
from appium import webdriver

device_list = ["x", "y", "z"]

class TestLogin():

    @pytest.mark.parametrize("device_setup", device_list)
    def setup(self, device_setup):
        self.driver = webdriver.Remote(command_executor='http://172.18.0.2:4444/wd/hub',
                desired_capabilities= {
                    'deviceName': device_setup,
                    })

    def teardown(self):
        self.driver.quit()

    def test_a(self):
        # Do something

    def test_b(self):
        # Do another thing

Upvotes: 0

Views: 889

Answers (1)

MrBean Bremen
MrBean Bremen

Reputation: 16805

You cannot parametrize the setup method, but you can use an auto-used fixture instead:

device_list = ["x", "y", "z"]

class TestLogin:

    @pytest.fixture(params=device_list, autouse=True)
    def device_setup(self, request):
        self.driver = webdriver.Remote(
            command_executor='http://172.18.0.2:4444/wd/hub',
            desired_capabilities={
                'deviceName': request.param,
            })
        yield
        self.driver.quit()

    def test_a(self):
        # Do something

This will execute each test with each parameter of the device list, as if you put the mark.parametrize decorator before each function.

UPDATE: If you want to run the setup only once for each parameter, you can use a session-scoped fixture instead:

class TestLogin:

    @pytest.fixture(params=device_list, autouse=True, scope="session")
    def device_setup(self, request):
        self.driver = webdriver.Remote(
            command_executor='http://172.18.0.2:4444/wd/hub',
            desired_capabilities={
                'deviceName': request.param,
            })
        yield
        self.driver.quit()

    def test_a(self):
        # Do something

    def test_b(self):
        # Do something

This will run the setup 3 times (once for each driver parameter) instead of 6 times.

Upvotes: 2

Related Questions