samedi 4 avril 2020

AttributeError: object has no attribute 'monkeypatch' while testing pyqt5 application

I have PyQt5 application (which you can find here) and I writing tests for it using pytest.

I have a BaseTest class which every test inherits from. Here is fixture of that class:

@pytest.fixture(autouse=True)
    def monkeypatching(self, monkeypatch):
        self.monkeypatch = monkeypatch

I use it for monkeypatching, but when I run my test suite:

$ pytest

I get errors like this:

================================== test session starts ===================================
platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1
PyQt5 5.10.1 -- Qt runtime 5.9.5 -- Qt compiled 5.9.5
rootdir: /home/python/Documents/PyQtAccounts
plugins: cov-2.8.1, qt-3.3.0
collected 66 items                                                                       

tests/func/test_check_updates.py ....                                              [  6%]
tests/func/test_close.py ....                                                      [ 12%]
tests/func/test_create_account.py .s....                                           [ 21%]
tests/func/test_create_database.py ..........                                      [ 36%]
tests/func/test_edit_account.py ...                                                [ 40%]
tests/func/test_edit_database.py ....                                              [ 46%]
tests/func/test_import_export.py .....                                             [ 54%]
tests/func/test_open_database.py ...                                               [ 59%]
tests/func/test_pass_generation_dialog.py ..                                       [ 62%]
tests/func/test_save_database.py FF.FF...                                          [ 74%]
tests/func/test_settings.py .....                                                  [ 81%]
tests/unit/test_about.py ..                                                        [ 84%]
tests/unit/test_accs_help_tip.py ..                                                [ 87%]
tests/unit/test_changelog.py .                                                     [ 89%]
tests/unit/test_dbs_help_tip.py ..                                                 [ 92%]
tests/unit/test_run_sh_path.py ..                                                  [ 95%]
tests/unit/test_show_account.py .s.                                                [100%]

======================================== FAILURES ========================================
___________________________ ImportExportTest.test_export_fail ____________________________

self = <tests.func.test_import_export.ImportExportTest testMethod=test_export_fail>

    def test_export_fail(self):
        # Toon wants to export his database, so he chose one in the list
        self.list.selected(Index('database'))

        # And presses Ctrl+E
        # File dialog appears and he chose / path
        file_dialog = self.save_file_dialog('database', ('/database.tar',))
>       self.monkeypatch.setattr(QFileDialog, 'getSaveFileName', file_dialog)
E       AttributeError: 'ImportExportTest' object has no attribute 'monkeypatch'

tests/func/test_import_export.py:128: AttributeError
---------------------------------- Captured stdout call ----------------------------------
/usr/bin/git
/home/python/.local/bin/pip3
/usr/bin/xclip
__________________________ ImportExportTest.test_export_success __________________________

self = <tests.func.test_import_export.ImportExportTest testMethod=test_export_success>

    def test_export_success(self):
        # Lea wants to export her database, so she chose one in the list
        self.list.selected(Index('database'))

        # And presses Ctrl+E
        # File dialog appears and she chose path
        file_dialog = self.save_file_dialog('database', ('tests/func/src/database.tar',))
>       self.monkeypatch.setattr(QFileDialog, 'getSaveFileName', file_dialog)
E       AttributeError: 'ImportExportTest' object has no attribute 'monkeypatch'

tests/func/test_import_export.py:106: AttributeError
---------------------------------- Captured stdout call ----------------------------------
/usr/bin/git
/home/python/.local/bin/pip3
/usr/bin/xclip
___________________________ ImportExportTest.test_import_fail ____________________________

self = <tests.func.test_import_export.ImportExportTest testMethod=test_import_fail>

    def test_import_fail(self):
        # Tom wants to import database
        # File dialog appears and he chose his tar file
        file_dialog = self.file_dialog(('tests/func/src/corrupted_few_files.tar',))
>       self.monkeypatch.setattr(QFileDialog, 'getOpenFileName', file_dialog)
E       AttributeError: 'ImportExportTest' object has no attribute 'monkeypatch'

tests/func/test_import_export.py:77: AttributeError
---------------------------------- Captured stdout call ----------------------------------
/usr/bin/git
/home/python/.local/bin/pip3
/usr/bin/xclip
__________________________ ImportExportTest.test_import_success __________________________

self = <tests.func.test_import_export.ImportExportTest testMethod=test_import_success>

    def test_import_success(self):
        # Emily wants to import database so she goes to menu File -> Import database...
        # File dialog appears and she chose her tar file
        file_dialog = self.file_dialog(('tests/func/src/import_database.tar',))
>       self.monkeypatch.setattr(QFileDialog, 'getOpenFileName', file_dialog)
E       AttributeError: 'ImportExportTest' object has no attribute 'monkeypatch'

tests/func/test_import_export.py:60: AttributeError
---------------------------------- Captured stdout call ----------------------------------
/usr/bin/git
/home/python/.local/bin/pip3
/usr/bin/xclip
================================ short test summary info =================================
FAILED tests/func/test_save_database.py::ImportExportTest::test_export_fail - Attribute...
FAILED tests/func/test_save_database.py::ImportExportTest::test_export_success - Attrib...
FAILED tests/func/test_save_database.py::ImportExportTest::test_import_fail - Attribute...
FAILED tests/func/test_save_database.py::ImportExportTest::test_import_success - Attrib...
======================== 4 failed, 60 passed, 2 skipped in 19.15s ========================

And at the same time if I run just test file that fails:

$ pytest tests/func/test_save_database.py

it will pass:

================================== test session starts ===================================
platform linux -- Python 3.6.9, pytest-5.4.1, py-1.8.1, pluggy-0.13.1
PyQt5 5.10.1 -- Qt runtime 5.9.5 -- Qt compiled 5.9.5
rootdir: /home/python/Documents/PyQtAccounts
plugins: cov-2.8.1, qt-3.3.0
collected 8 items                                                                        

tests/func/test_save_database.py ........                                          [100%]

=================================== 8 passed in 3.36s ====================================

I'm getting errors like this time to time, sometimes they disappear by themselves but I want to understand why testing have such strange behaviors and how to solve them.

Thank you!

Aucun commentaire:

Enregistrer un commentaire