0
import pytest

# Fixture for Firefox
from selenium import webdriver


@pytest.fixture(scope="class")
def driver_init(request):
    ff_driver = webdriver.Firefox()
    request.cls.driver = ff_driver
    yield
    ff_driver.close()


# Fixture for Chrome
@pytest.fixture(scope="class")
def chrome_driver_init(request):
    chrome_driver = webdriver.Chrome()
    request.cls.driver = chrome_driver
    yield
    chrome_driver.close()


@pytest.mark.usefixtures("driver_init")
class BaseTest:
    pass

It's not working, everything looks correct to me but still getting the error driver_init not found. I am using pythin version 3.9 and using mac os.

4

1 回答 1

0

I'm not sure, if you forgot to add a few lines to your minimal example. As I see it, it does not work as expected, because you have a class BaseTest that has not even an constructor or an unit test and the other problem is, that pytest classes should start with Test... in order to be found.

See also convention for pytest discovery : https://docs.pytest.org/en/latest/explanation/goodpractices.html#conventions-for-python-test-discovery

If I use your code and change the BaseTest class to the following, it is working fine for me.

@pytest.mark.usefixtures("driver_init")
class TestBase:

    def test_test(self):
    """ This is a unit test """
       pass

Now, if you execute the unit test test_test, then the class TestBase will call to driver_init.

于 2021-11-02T12:46:58.227 回答