Beispiel #1
0
def test_display_statistic(capsys, swagger_20, execution_context, operation,
                           response):
    # Given multiple successful & failed checks in a single test
    success = models.Check("not_a_server_error", models.Status.success,
                           response, 0, models.Case(operation))
    failure = models.Check("not_a_server_error", models.Status.failure,
                           response, 0, models.Case(operation))
    single_test_statistic = models.TestResult(
        operation.method,
        operation.full_path,
        DataGenerationMethod.default(),
        [
            success,
            success,
            success,
            failure,
            failure,
            models.Check("different_check", models.Status.success, response, 0,
                         models.Case(operation)),
        ],
    )
    results = models.TestResultSet([single_test_statistic])
    event = Finished.from_results(results, running_time=1.0)
    # When test results are displayed
    default.display_statistic(execution_context, event)

    lines = [line for line in capsys.readouterr().out.split("\n") if line]
    failed = strip_style_win32(click.style("FAILED", bold=True, fg="red"))
    passed = strip_style_win32(click.style("PASSED", bold=True, fg="green"))
    # Then all check results should be properly displayed with relevant colors
    assert lines[2:4] == [
        f"    not_a_server_error                    3 / 5 passed          {failed} ",
        f"    different_check                       1 / 1 passed          {passed} ",
    ]
Beispiel #2
0
def test_display_single_failure(capsys, swagger_20, endpoint, body):
    # Given a single test result with multiple successful & failed checks
    success = models.Check("not_a_server_error", models.Status.success)
    failure = models.Check(
        "not_a_server_error",
        models.Status.failure,
        models.Case("/success", "GET", base_url="http://example.com", body=body),
    )
    test_statistic = models.TestResult(
        endpoint,
        swagger_20,
        [success, success, success, failure, failure, models.Check("different_check", models.Status.success)],
    )
    # When this failure is displayed
    output.display_single_failure(test_statistic)
    out = capsys.readouterr().out
    lines = out.split("\n")
    # Then the endpoint name is displayed as a subsection
    assert " GET: /success " in lines[0]
    # And check name is displayed in red
    assert lines[1] == click.style("Check           : not_a_server_error", fg="red")
    # And body should be displayed if it is not None
    if body is None:
        assert "Body" not in out
    else:
        assert click.style(f"Body            : {body}", fg="red") in lines
    # And empty parameters are not present in the output
    assert "Path parameters" not in out
    # And not needed attributes are not displayed
    assert "Path" not in out
    assert "Method" not in out
    assert "Base url" not in out
Beispiel #3
0
def test_display_failures(swagger_20, capsys, execution_context, results_set,
                          verbosity, response):
    execution_context.verbosity = verbosity
    # Given two test results - success and failure
    operation = models.APIOperation("/api/failure",
                                    "GET", {},
                                    base_url="http://127.0.0.1:8080",
                                    schema=swagger_20)
    failure = models.TestResult(operation.method, operation.full_path,
                                DataGenerationMethod.default())
    failure.add_failure("test", models.Case(operation), response, 0, "Message")
    execution_context.results.append(
        SerializedTestResult.from_test_result(failure))
    results_set.append(failure)
    event = Finished.from_results(results_set, 1.0)
    # When the failures are displayed
    default.display_failures(execution_context, event)
    out = capsys.readouterr().out.strip()
    # Then section title is displayed
    assert " FAILURES " in out
    # And operation with a failure is displayed as a subsection
    assert " GET: /v1/api/failure " in out
    assert "Message" in out
    assert "Run this Python code to reproduce this failure: " in out
    assert f"requests.get('http://127.0.0.1:8080/api/failure', headers={{'User-Agent': '{USER_AGENT}'}})" in out
Beispiel #4
0
def test_display_single_failure(capsys, swagger_20, execution_context, operation, body, response):
    # Given a single test result with multiple successful & failed checks
    success = models.Check("not_a_server_error", models.Status.success, response, 0, models.Case(operation, body=body))
    failure = models.Check("not_a_server_error", models.Status.failure, response, 0, models.Case(operation, body=body))
    test_statistic = models.TestResult(
        operation.method,
        operation.full_path,
        DataGenerationMethod.default(),
        [
            success,
            success,
            success,
            failure,
            failure,
            models.Check("different_check", models.Status.success, response, 0, models.Case(operation, body=body)),
        ],
    )
    # When this failure is displayed
    default.display_failures_for_single_test(execution_context, SerializedTestResult.from_test_result(test_statistic))
    out = capsys.readouterr().out
    lines = out.split("\n")
    # Then the path is displayed as a subsection
    assert " GET /v1/success " in lines[0]
    # And body should be displayed if it is not NOT_SET
    if body is NOT_SET:
        assert "Body" not in out
    else:
        assert strip_style_win32(click.style(f"Body            : {body}", fg="red")) in lines
    # And empty parameters are not present in the output
    assert "Path parameters" not in out
    # And not needed attributes are not displayed
    assert "Path" not in out
    assert "Method" not in out
    assert "Base url" not in out
Beispiel #5
0
def test_display_single_failure(capsys, swagger_20, execution_context,
                                endpoint, body):
    # Given a single test result with multiple successful & failed checks
    success = models.Check("not_a_server_error", models.Status.success)
    failure = models.Check("not_a_server_error", models.Status.failure,
                           models.Case(endpoint, body=body))
    test_statistic = models.TestResult(endpoint, [
        success, success, success, failure, failure,
        models.Check("different_check", models.Status.success)
    ])
    # When this failure is displayed
    default.display_failures_for_single_test(
        execution_context,
        SerializedTestResult.from_test_result(test_statistic))
    out = capsys.readouterr().out
    lines = out.split("\n")
    # Then the endpoint name is displayed as a subsection
    assert " GET: /success " in lines[0]
    # And check name is displayed in red
    assert lines[1] == strip_style_win32(
        click.style("Check           : not_a_server_error", fg="red"))
    # And body should be displayed if it is not None
    if body is None:
        assert "Body" not in out
    else:
        assert strip_style_win32(
            click.style(f"Body            : {body}", fg="red")) in lines
    # And empty parameters are not present in the output
    assert "Path parameters" not in out
    # And not needed attributes are not displayed
    assert "Path" not in out
    assert "Method" not in out
    assert "Base url" not in out
Beispiel #6
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")
Beispiel #7
0
def test_display_errors(swagger_20, capsys, results_set, execution_context,
                        show_errors_tracebacks, verbosity):
    execution_context.verbosity = verbosity
    # Given two test results - success and error
    endpoint = models.Endpoint("/api/error", "GET", {}, swagger_20)
    error = models.TestResult(endpoint, seed=123)
    error.add_error(ConnectionError("Connection refused!"),
                    models.Case(endpoint, query={"a": 1}))
    results_set.append(error)
    execution_context.results.append(
        SerializedTestResult.from_test_result(error))
    event = Finished.from_results(results_set, 1.0)
    # When the errors are displayed
    execution_context.show_errors_tracebacks = show_errors_tracebacks
    default.display_errors(execution_context, event)
    out = capsys.readouterr().out.strip()
    # Then section title is displayed
    assert " ERRORS " in out
    help_message_exists = (
        "Add this option to your command line parameters to see full tracebacks: --show-errors-tracebacks"
        in out)
    # And help message is displayed only if tracebacks are not shown
    assert help_message_exists is not show_errors_tracebacks
    # And endpoint with an error is displayed as a subsection
    assert " GET: /api/error " in out
    # And the error itself is displayed
    assert "ConnectionError: Connection refused!" in out
    # And the example is displayed
    assert "Query           : {'a': 1}" in out
    assert "Or add this option to your command line parameters: --hypothesis-seed=123" in out
Beispiel #8
0
def test_display_failures(swagger_20, capsys, execution_context, results_set,
                          verbosity, response):
    execution_context.verbosity = verbosity
    # Given two test results - success and failure
    operation = models.APIOperation("/api/failure",
                                    "GET", {},
                                    base_url="http://127.0.0.1:8080",
                                    schema=swagger_20)
    failure = models.TestResult(
        operation.method,
        operation.full_path,
        verbose_name=f"{operation.method} {operation.full_path}",
        data_generation_method=DataGenerationMethod.default(),
    )
    failure.add_failure("test", models.Case(operation), response, 0, "Message",
                        None)
    execution_context.results.append(
        SerializedTestResult.from_test_result(failure))
    results_set.append(failure)
    event = Finished.from_results(results_set, 1.0)
    # When the failures are displayed
    default.display_failures(execution_context, event)
    out = capsys.readouterr().out.strip()
    # Then section title is displayed
    assert " FAILURES " in out
    # And operation with a failure is displayed as a subsection
    assert " GET /v1/api/failure " in out
    assert "Message" in out
    assert "Run this cURL command to reproduce this failure:" in out
    headers = f"-H 'Content-Length: 0' -H 'Content-Type: application/json' -H 'User-Agent: {USER_AGENT}'"
    assert f"curl -X GET {headers} http://127.0.0.1:8080/api/failure" in out
Beispiel #9
0
def results_set(operation):
    statistic = models.TestResult(
        operation.method,
        operation.full_path,
        data_generation_method=DataGenerationMethod.default(),
        verbose_name=f"{operation.method} {operation.full_path}",
    )
    return models.TestResultSet([statistic])
Beispiel #10
0
def test_display_failures(swagger_20, capsys, results_set):
    # Given two test results - success and failure
    failure = models.TestResult(models.Endpoint("/api/failure", "GET", {}), swagger_20)
    failure.add_failure("test", models.Case("/api/failure", "GET", base_url="http://127.0.0.1:8080"))
    results_set.append(failure)
    # When the failures are displayed
    output.display_failures(results_set)
    out = capsys.readouterr().out.strip()
    # Then section title is displayed
    assert " FAILURES " in out
    # And endpoint with a failure is displayed as a subsection
    assert " GET: /api/failure " in out
    # And check name is displayed
    assert "Check           : test" in out
    assert "Run this Python code to reproduce this failure: " in out
    assert "requests.get('http://127.0.0.1:8080/api/failure')" in out
Beispiel #11
0
def test_display_errors(swagger_20, capsys, results_set):
    # Given two test results - success and error
    endpoint = models.Endpoint("/api/error", "GET", {}, swagger_20)
    error = models.TestResult(endpoint, seed=123)
    error.add_error(ConnectionError("Connection refused!"), models.Case(endpoint, query={"a": 1}))
    results_set.append(error)
    # When the errors are displayed
    default.display_errors(results_set)
    out = capsys.readouterr().out.strip()
    # Then section title is displayed
    assert " ERRORS " in out
    # And endpoint with an error is displayed as a subsection
    assert " GET: /api/error " in out
    # And the error itself is displayed
    assert "ConnectionError: Connection refused!" in out
    # And the example is displayed
    assert "Query           : {'a': 1}" in out
    assert "Or add this option to your command line parameters: --hypothesis-seed=123" in out
Beispiel #12
0
def test_display_errors(swagger_20, capsys, results_set):
    # Given two test results - success and error
    error = models.TestResult(models.Endpoint("/api/error", "GET", {}), swagger_20)
    error.add_error(
        ConnectionError("Connection refused!"),
        models.Case("/api/error", "GET", base_url="http://127.0.0.1:8080", query={"a": 1}),
    )
    results_set.append(error)
    # When the errors are displayed
    output.display_errors(results_set)
    out = capsys.readouterr().out.strip()
    # Then section title is displayed
    assert " ERRORS " in out
    # And endpoint with an error is displayed as a subsection
    assert " GET: /api/error " in out
    # And the error itself is displayed
    assert "ConnectionError: Connection refused!" in out
    # And the example is displayed
    assert "Query           : {'a': 1}" in out
Beispiel #13
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
    output.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")
Beispiel #14
0
def test_display_failures(swagger_20, capsys, execution_context, results_set):
    # Given two test results - success and failure
    endpoint = models.Endpoint("/api/failure", "GET", {}, base_url="http://127.0.0.1:8080", schema=swagger_20)
    failure = models.TestResult(endpoint)
    failure.add_failure("test", models.Case(endpoint), "Message")
    execution_context.results.append(SerializedTestResult.from_test_result(failure))
    results_set.append(failure)
    event = Finished.from_results(results_set, 1.0)
    # When the failures are displayed
    default.display_failures(execution_context, event)
    out = capsys.readouterr().out.strip()
    # Then section title is displayed
    assert " FAILURES " in out
    # And endpoint with a failure is displayed as a subsection
    assert " GET: /api/failure " in out
    assert "Message" in out
    # And check name is displayed
    assert "Check           : test" in out
    assert "Run this Python code to reproduce this failure: " in out
    assert "requests.get('http://127.0.0.1:8080/api/failure')" in out
Beispiel #15
0
def test_display_statistic(capsys, swagger_20, endpoint):
    # Given multiple successful & failed checks in a single test
    success = models.Check("not_a_server_error", models.Status.success)
    failure = models.Check("not_a_server_error", models.Status.failure)
    single_test_statistic = models.TestResult(
        endpoint, [success, success, success, failure, failure, models.Check("different_check", models.Status.success)]
    )
    results = models.TestResultSet([single_test_statistic])
    # When test results are displayed
    default.display_statistic(results)

    lines = [line for line in capsys.readouterr().out.split("\n") if line]
    failed = click.style("FAILED", bold=True, fg="red")
    not_a_server_error = click.style("not_a_server_error", bold=True)
    different_check = click.style("different_check", bold=True)
    passed = click.style("PASSED", bold=True, fg="green")
    # Then all check results should be properly displayed with relevant colors
    assert lines[1:3] == [
        f"{not_a_server_error}            3 / 5 passed          {failed} ",
        f"{different_check}               1 / 1 passed          {passed} ",
    ]
Beispiel #16
0
def results_set(endpoint):
    statistic = models.TestResult(endpoint)
    return models.TestResultSet([statistic])
def make_test_result(schema: BaseSchema,
                     definition: Dict[str, Any]) -> models.TestResult:
    endpoint = models.Endpoint("/path", "GET", definition=definition)
    return models.TestResult(endpoint, schema)
Beispiel #18
0
def results_set(endpoint):
    statistic = models.TestResult(endpoint, data_generation_method=DataGenerationMethod.default())
    return models.TestResultSet([statistic])
Beispiel #19
0
def results_set(endpoint, swagger_20):
    statistic = models.TestResult(endpoint, swagger_20)
    return models.TestResultSet([statistic])
Beispiel #20
0
def results_set(operation):
    statistic = models.TestResult(
        operation.method,
        operation.full_path,
        data_generation_method=DataGenerationMethod.default())
    return models.TestResultSet([statistic])
Beispiel #21
0
def results(request, swagger_20) -> models.TestResult:
    endpoint = models.Endpoint("/path",
                               "GET",
                               definition={"produces": request.param})
    return models.TestResult(endpoint, swagger_20)