Skip to content
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

Easing the use of QSettings #85

Closed
PatrickMassot opened this issue Aug 31, 2015 · 3 comments
Closed

Easing the use of QSettings #85

PatrickMassot opened this issue Aug 31, 2015 · 3 comments

Comments

@PatrickMassot
Copy link

Is it possible to easily tell the great qapp fixture of pytest-qt where it should look for settings?

Say my main code has app = QApplication([]) followed by app.setOrganizationName('MyOrg') and app.setApplicationName('MyApp'). Maybe I'm doing this wrong but, since I don't want my tests to rely on (or alter) real world settings I would like my test to use app.setOrganizationName('MyOrg') and app.setApplicationName('MyAppTest').

Assuming the above is not completely wrong, I'd like the qtbot fixture of pytest-qt to use (through the qapp fixture) those settings. One solution that I see and doesn't seem to cost anything is to put in my conftest.py

def pytest_namespace():
    return {'organizationName': 'MyOrg',
            'applicationName': 'MyAppTest'
            }

and then modify the plugin in the qapp fixture to replace

    if app is None:
        global _qapp_instance
        _qapp_instance = QApplication([])
        yield _qapp_instance

by

    if app is None:
        global _qapp_instance
        _qapp_instance = QApplication([])
        _qapp_instance.setOrganizationName(
                getattr(pytest, 'organizationName', '')
                )
        _qapp_instance.setApplicationName(
                getattr(pytest, 'applicationName', '')
                )
        yield _qapp_instance

Does it make sense? Should I do something completely different?

@nicoddemus
Copy link
Member

Hi!

A simple way to accomplish this is by using a autouse fixture in your root conftest.py file:

@pytest.fixture(scope='session', autouse=True)
def config_app_settings(qapp):
    qapp.setOrganizationName('MyOrg')
    qapp.setApplicationName('MyApp')

This this code will execute exactly once per test session and will ensure your app fixture is properly configured.

Hope this helps! 😄

@PatrickMassot
Copy link
Author

Thank you very much! I was looking for something too complicated. I'm new to testing PyQt applications and have only done a little bit of django applications testing (without py.test).

I suggest you mention this in the documentation. I think I believed I needed a modified plugin because the qapp fixture is not mentioned on readthedocs so I deduced (incorrectly) this was an internal detail of the plugin that I couldn't access.

More generally I'd love to read much more documentation on testing Qt applications in python. It's much much harder to find than the corresponding documentation for django. Maybe the documentation of this plugin could include a larger tutorial testing a larger application having signal/slots, modal dialogs, model/views...

Note that I may try to contribute such a tutorial in the future if I manage to understand enough...

@nicoddemus
Copy link
Member

Glad that helps! 😄

I created a new issue for the docs at #86, thanks for the suggestion.

If you don't have any more questions, could you then close the issue?

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

2 participants