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

How i can test code with QMessageBox? #262

Closed
theperelomov opened this issue May 30, 2019 · 2 comments
Closed

How i can test code with QMessageBox? #262

theperelomov opened this issue May 30, 2019 · 2 comments

Comments

@theperelomov
Copy link

theperelomov commented May 30, 2019

Sorry if something is wrong, I just started to learn automatic testing, and I'm sorry, I'm not English speaking.
Here is my code:

def button_start_clicked(self):
    try:
        vertices_count = int(self.line_edit_count_vertices.text())
        if vertices_count < 5:
            raise ValueError
    except ValueError:
        QMessageBox.warning(self, "header", "text", QMessageBox.Close, QMessageBox.Close)

The problem is that the QMessageBox does not close.
Please explain how to test this code correctly.

Here is my simple test

@pytest.mark.parametrize("value", [4, 5, "text"])
def test_button_start_clicked(value):
    window = Window()
    window.line_edit_count_vertices.setText(str(value))
    window.button_start_clicked()

UPD after 30 minutes of thinking:
here is my simple solution, but I think that it is not perfect

application = QApplication()
    
@pytest.mark.parametrize("value", [4, 5, "text"])
def test_button_start_clicked(self, value):
    def close_window():
        sleep(0.5)
        if type(application.activeWindow()) is QMessageBox:
            application.activeWindow().accept()
    window = Window()
    window.line_edit_count_vertices.setText(str(value))
    ThreadPoolExecutor().submit(close_window)
    window.button_start_clicked()
@theperelomov theperelomov changed the title How i can test this code with QMessageBox? How i can test code with QMessageBox? May 30, 2019
@The-Compiler
Copy link
Member

Sorry if something is wrong, I just started to learn automatic testing, and I'm sorry, I'm not English speaking.

Don't worry! Your English is fine 😄

There are some ideas about this in #256 and in the docs: https://pytest-qt.readthedocs.io/en/latest/note_dialogs.html

@theperelomov
Copy link
Author

Don't worry! Your English is fine 😄

Don't worry Google Translate 😄

Thanks for the links provided.

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