Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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} ",
]
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(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_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
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)
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 "", line 1\n some invalid code\n ^\nSyntaxError: invalid syntax\n'
else:
expected = ' File "", line 1\n some invalid code\n ^\nSyntaxError: invalid syntax\n'
assert "\n".join(lines[1:6]) == click.style(expected, fg="red")
def test_display_failures(swagger_20, capsys, 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")
results_set.append(failure)
# When the failures are displayed
default.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
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
def results_set(endpoint):
statistic = models.TestResult(endpoint)
return models.TestResultSet([statistic])
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