AttributeError: объект не имеет атрибута «monkeypatch» во время тестирования приложения pyqt5 - PullRequest
0 голосов
/ 04 апреля 2020

У меня есть приложение PyQt5 (которое вы можете найти здесь ), и я пишу тесты для него, используя pytest.

У меня есть класс BaseTest, от которого наследуется каждый тест. Вот пример этого класса:

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

Я использую его для создания обезьян, но когда я запускаю свой набор тестов:

$ pytest

Я получаю такие ошибки :

================================== 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 ========================

И в то же время, если я запускаю только тестовый файл, который не проходит:

$ pytest tests/func/test_save_database.py

он пройдет:

================================== 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 ====================================

Я получаю ошибки, как это время от времени, иногда они исчезают сами по себе, но я хочу понять, почему тестирование имеет такое странное поведение и как их устранить.

Спасибо!

...