Make sure that tests fail in case there is an unhandled Python error (#1456)

Before this PR, the following test passed:

    def test_pyscript_hello(self):
        self.pyscript_run(
            """
            <script type="py">
                raise Exception("hello")
            </script>
            """)

What happens is that we intercept the Python exception and display a nice banner on the DOM, but the test itself passes. This is error prone: if we have Python exceptions on the page, the test should fail by default, and we should have a way to silence it in case those exceptions are expected.

This PR treats Python errors as we treat JS errors: unhandled exceptions cause the test to fail, but you can silence them by calling self.check_py_errors(), exactly as you can call self.check_js_errors().
This commit is contained in:
Antonio Cuni
2023-05-09 15:39:19 +02:00
committed by GitHub
parent 73e0271c23
commit 82e5b64bad
7 changed files with 107 additions and 71 deletions

View File

@@ -2,7 +2,7 @@ import re
import pytest
from .support import JsErrors, PyScriptTest, skip_worker
from .support import PageErrors, PyScriptTest, skip_worker
class TestBasic(PyScriptTest):
@@ -76,6 +76,8 @@ class TestBasic(PyScriptTest):
"""
)
assert "hello pyscript" in self.console.log.lines
self.check_py_errors("Exception: this is an error")
#
# check that we sent the traceback to the console
tb_lines = self.console.error.lines[-1].splitlines()
assert tb_lines[0] == "[pyexec] Python exception:"
@@ -107,6 +109,8 @@ class TestBasic(PyScriptTest):
"Exception: this is an error inside handler", match_substring=True
)
self.check_py_errors("Exception: this is an error inside handler")
## error in console
tb_lines = self.console.error.lines[-1].splitlines()
assert tb_lines[0] == "[pyexec] Python exception:"
@@ -178,20 +182,21 @@ class TestBasic(PyScriptTest):
self.pyscript_run(
"""
<py-config>
packages = ["nonexistendright"]
packages = ["i-dont-exist"]
</py-config>
""",
wait_for_pyscript=False,
)
expected_alert_banner_msg = (
"(PY1001): Unable to install package(s) 'nonexistendright'. "
"(PY1001): Unable to install package(s) 'i-dont-exist'. "
"Unable to find package in PyPI. Please make sure you have "
"entered a correct package name."
)
alert_banner = self.page.wait_for_selector(".alert-banner")
assert expected_alert_banner_msg in alert_banner.inner_text()
self.check_py_errors("Can't fetch metadata for 'i-dont-exist'")
@skip_worker("FIXME: the banner doesn't appear")
def test_no_python_wheel(self):
@@ -211,6 +216,7 @@ class TestBasic(PyScriptTest):
alert_banner = self.page.wait_for_selector(".alert-banner")
assert expected_alert_banner_msg in alert_banner.inner_text()
self.check_py_errors("Can't find a pure Python 3 wheel for 'opsdroid'")
def test_dynamically_add_py_script_tag(self):
self.pyscript_run(
@@ -240,7 +246,7 @@ class TestBasic(PyScriptTest):
assert self.console.log.lines[-1] == "hello from foo"
def test_py_script_src_not_found(self):
with pytest.raises(JsErrors) as exc:
with pytest.raises(PageErrors) as exc:
self.pyscript_run(
"""
<py-script src="foo.py"></py-script>
@@ -249,9 +255,7 @@ class TestBasic(PyScriptTest):
assert "Failed to load resource" in self.console.error.lines[0]
error_msgs = str(exc.value)
expected_msg = "(PY0404): Fetching from URL foo.py failed with error 404"
assert expected_msg in error_msgs
assert self.assert_banner_message(expected_msg)