From 7c95e6dffacc99ba52c9cbdcb5e637df4749611d Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Sun, 6 Dec 2020 01:06:37 +0100 Subject: [PATCH] Updated existing Error test --- pytest.ini | 2 +- tests/dummy.py | 3 ++ tests/test_base_error.py | 87 ++++++++++++++++++++++++++++++++++++++++ tests/test_error.py | 44 -------------------- 4 files changed, 91 insertions(+), 45 deletions(-) create mode 100644 tests/test_base_error.py delete mode 100644 tests/test_error.py diff --git a/pytest.ini b/pytest.ini index b61d59ec..c2278317 100644 --- a/pytest.ini +++ b/pytest.ini @@ -1,6 +1,6 @@ [pytest] markers = - error: Test various error handling scenarios + base: Base functionality tests core: Core functionality tests gui: Qt5 GUI tests serial diff --git a/tests/dummy.py b/tests/dummy.py index 0f26e0ff..2807578a 100644 --- a/tests/dummy.py +++ b/tests/dummy.py @@ -71,3 +71,6 @@ class StatusBar(): def causeOSError(*args, **kwargs): raise OSError + +def causeException(*args, **kwargs): + raise Exception diff --git a/tests/test_base_error.py b/tests/test_base_error.py new file mode 100644 index 00000000..e3bd689d --- /dev/null +++ b/tests/test_base_error.py @@ -0,0 +1,87 @@ +# -*- coding: utf-8 -*- +"""novelWriter Error Tester +""" + +import nw +import pytest + +from PyQt5.QtWidgets import qApp + +from dummy import causeException + +from nw.error import NWErrorMessage, exceptionHandler + +@pytest.mark.base +def testBaseError_Dialog(qtbot, monkeypatch, fncDir, tmpDir): + qApp.closeAllWindows() + nwGUI = nw.main(["--testmode", "--config=%s" % fncDir, "--data=%s" % tmpDir]) + qtbot.addWidget(nwGUI) + nwGUI.show() + qtbot.waitForWindowShown(nwGUI) + + nwErr = NWErrorMessage(nwGUI) + qtbot.addWidget(nwErr) + nwErr.show() + + # Invalid Error Message + nwErr.setMessage(Exception, "Faulty Error", 123) + assert nwErr.msgBody.toPlainText() == "Failed to generate error report ..." + + # Valid Error Message + nwErr.setMessage(Exception, "First Error", None) + theMessage = nwErr.msgBody.toPlainText() + assert theMessage + assert "First Error" in theMessage + assert "Exception" in theMessage + nwErr._doClose() + nwErr.close() + + # Valid Error + monkeypatch.setattr("PyQt5.QtCore.QSysInfo.kernelVersion", lambda: "1.2.3") + theMessage = exceptionHandler(Exception, "Second Error", None, testMode=True) + assert theMessage + assert "Second Error" in theMessage + assert "Exception" in theMessage + assert "(1.2.3)" in theMessage + monkeypatch.undo() + + # No kernel version retrieved + monkeypatch.setattr("PyQt5.QtCore.QSysInfo.kernelVersion", causeException) + theMessage = exceptionHandler(Exception, "Third Error", None, testMode=True) + assert theMessage + assert "Third Error" in theMessage + assert "Exception" in theMessage + assert "(Unknown)" in theMessage + monkeypatch.undo() + + # Normal shutdown, but not testmode + monkeypatch.setattr("PyQt5.QtWidgets.qApp.exit", lambda x: None) + nwGUI.mainConf.showGUI = True + exceptionHandler(Exception, "Third Error", None, testMode=False) + nwGUI.mainConf.showGUI = False + monkeypatch.undo() + + # Disable blocking of GUI + monkeypatch.setattr("PyQt5.QtWidgets.QDialog.exec_", lambda: None) + exceptionHandler(Exception, "Third Error", None, testMode=True) + monkeypatch.undo() + + # Should handle qApp failing + monkeypatch.setattr("PyQt5.QtWidgets.qApp.topLevelWidgets", causeException) + exceptionHandler(Exception, "Third Error", None, testMode=True) + monkeypatch.undo() + + # Should handle failing to close main GUI + monkeypatch.setattr(nwGUI, "closeMain", causeException) + exceptionHandler(Exception, "Third Error", None, testMode=True) + monkeypatch.undo() + + # Should not crash when no GUI is found + nwGUI.setObjectName("Stuff") + assert exceptionHandler(Exception, "Third Error", None, testMode=True) is None + + nwGUI.closeMain() + + # qtbot.stopForInteraction() + +# END Test testBaseError_Dialog diff --git a/tests/test_error.py b/tests/test_error.py deleted file mode 100644 index 192fb6cb..00000000 --- a/tests/test_error.py +++ /dev/null @@ -1,44 +0,0 @@ -# -*- coding: utf-8 -*- -"""novelWriter Error Tester -""" - -import nw -import pytest - -from PyQt5.QtWidgets import qApp - -from nw.error import NWErrorMessage, exceptionHandler - -@pytest.mark.error -def testErrorDialog(qtbot, fncDir, tmpDir): - qApp.closeAllWindows() - nwGUI = nw.main(["--testmode", "--config=%s" % fncDir, "--data=%s" % tmpDir]) - qtbot.addWidget(nwGUI) - nwGUI.show() - qtbot.waitForWindowShown(nwGUI) - - nwErr = NWErrorMessage(nwGUI) - qtbot.addWidget(nwErr) - nwErr.show() - - # Invalid Error - nwErr.setMessage(Exception, "Faulty Error", 123) - assert nwErr.msgBody.toPlainText() == "Failed to generate error report ..." - - # Valid Error - nwErr.setMessage(Exception, "First Error", None) - theMessage = nwErr.msgBody.toPlainText() - assert theMessage - assert "First Error" in theMessage - assert "Exception" in theMessage - nwErr._doClose() - nwErr.close() - - theMessage = exceptionHandler(Exception, "Second Error", None, testMode=True) - assert theMessage - assert "Second Error" in theMessage - assert "Exception" in theMessage - - nwGUI.closeMain() - - # qtbot.stopForInteraction()