Skip to content

Configuring Qt API in pytest.ini #129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
The-Compiler opened this issue May 10, 2016 · 3 comments
Closed

Configuring Qt API in pytest.ini #129

The-Compiler opened this issue May 10, 2016 · 3 comments
Milestone

Comments

@The-Compiler
Copy link
Member

In #pylib I discussed with @estan if there's some good way to configure PYTEST_QT_API in a way which doesn't require passing it on the commandline, for cases where you always need it to be e.g. pyqt5.

Unfortunately using pytest-env doesn't work as it uses pytest_load_initial_conftestst which will be run after pytest-qt is already imported.

One possibility would be to change pytest-qt so it reads the API version from pytest.ini. This should be doable but is probably quite painful, as it reqires delaying any imports from qt_compat until after pytest_load_initial_conftests (i.e. moving a lot of imports inside functions...).

Another possibility would be to add an env option to pytest's core, but pytest would need to read it (and set the environment variables) before any plugins are loaded...

Alternatively pytest-qt could search a pytestqt.ini or so in the current directory, but that'd also break in various circumstances and isn't really nice either.

What do you think?

@estan
Copy link
Contributor

estan commented May 12, 2016

Just dropping by to say hi. Although it's not a big problem to have to set PYTEST_QT_API in the environment before running py.test, it's one more thing I need to put in the instructions for developers, so would love for it to be configurable through pytest.ini.

@nicoddemus
Copy link
Member

One possibility would be to change pytest-qt so it reads the API version from pytest.ini.

I think this is the way to go.

This should be doable but is probably quite painful, as it reqires delaying any imports from qt_compat until after pytest_load_initial_conftests (i.e. moving a lot of imports inside functions...).

I agree that it would be painful and easy to miss some imports due to this, but we could instead implement lazy importing like py.std does with stdlib modules.

from qt_compat import qt_api

@pytest.yield_fixture(scope='session')
def qapp():
    """
    fixture that instantiates the QApplication instance that will be used by
    the tests.
    """
    app = qt_api.QApplication.instance()
    if app is None:
        global _qapp_instance
        _qapp_instance = qt_api.QApplication([])
        yield _qapp_instance
    else:
        yield app  # pragma: no cover

def pytest_configure(config):
    qt_api.set_qt_api(config.getoption('qt_api')) # at this point import the correct module and inject the classes as attributes of qt_api instance

@nicoddemus
Copy link
Member

Wrote a quick proof of concept in #130

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants