Esempio n. 1
0
def test_display_single_error(capsys, swagger_20, endpoint, execution_context,
                              show_errors_tracebacks):
    # Given exception is multiline
    exception = None
    try:
        exec("some invalid code")
    except SyntaxError as exc:
        exception = exc

    result = models.TestResult(endpoint)
    result.add_error(exception)
    # When the related test result is displayed
    execution_context.show_errors_tracebacks = show_errors_tracebacks
    default.display_single_error(execution_context,
                                 SerializedTestResult.from_test_result(result))
    lines = capsys.readouterr().out.strip().split("\n")
    # Then it should be correctly formatted and displayed in red color
    if sys.version_info <= (3, 8):
        expected = '  File "<string>", line 1\n    some invalid code\n               ^\nSyntaxError: invalid syntax\n'
    else:
        expected = '  File "<string>", line 1\n    some invalid code\n         ^\nSyntaxError: invalid syntax\n'
    if show_errors_tracebacks:
        lines = click.unstyle("\n".join(lines)).split("\n")
        assert lines[1] == "Traceback (most recent call last):"
        # There is a path on the next line, it is simpler to not check it since it doesn't give much value
        # But presence of traceback itself is checked
        expected = f'    exec("some invalid code")\n{expected}'
        assert "\n".join(lines[3:8]) == expected.strip("\n")
    else:
        assert "\n".join(lines[1:6]) == strip_style_win32(
            click.style(expected, fg="red")).rstrip("\n")
Esempio n. 2
0
def test_display_single_error(capsys, swagger_20, endpoint):
    # Given exception is multiline
    exception = None
    try:
        exec("some invalid code")
    except SyntaxError as exc:
        exception = exc

    result = models.TestResult(endpoint, swagger_20)
    result.add_error(exception)
    # When the related test result is displayed
    default.display_single_error(result)
    lines = capsys.readouterr().out.strip().split("\n")
    # Then it should be correctly formatted and displayed in red color
    if sys.version_info <= (3, 8):
        expected = '  File "<string>", line 1\n    some invalid code\n               ^\nSyntaxError: invalid syntax\n'
    else:
        expected = '  File "<string>", line 1\n    some invalid code\n         ^\nSyntaxError: invalid syntax\n'
    assert "\n".join(lines[1:6]) == click.style(expected, fg="red")