Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if override:
case = Case(**kwargs)
response = case.call(base_url)
else:
case = Case(**kwargs)
endpoint.base_url = base_url
response = case.call()
assert response.status_code == 200
assert response.json() == {"success": True}
with pytest.warns(None) as records:
del response
assert not records
schema = schemathesis.from_path(SIMPLE_PATH)
ENDPOINT = Endpoint("/api/success", "GET", {}, base_url="http://example.com", schema=schema)
@pytest.mark.parametrize(
"case, expected",
(
(Case(ENDPOINT, body={"test": 1}), "requests.get('http://example.com/api/success', json={'test': 1})"),
(Case(ENDPOINT), "requests.get('http://example.com/api/success')"),
(Case(ENDPOINT, query={"a": 1}), "requests.get('http://example.com/api/success', params={'a': 1})"),
),
)
def test_get_code_to_reproduce(case, expected):
assert case.get_code_to_reproduce() == expected
def test_validate_response(testdir):
testdir.make_test(
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
def test_response_conformance_invalid(args):
app, kwargs = args
# When endpoint returns a response that doesn't conform to the schema
# And "response_schema_conformance" is specified
results = execute(**kwargs, checks=(response_schema_conformance,), hypothesis_options={"max_examples": 1})
# Then there should be a failure
assert results.has_failures
lines = results.results[0].checks[-1].message.split("\n")
assert lines[0] == "The received response does not conform to the defined schema!"
assert lines[2] == "Details: "
assert lines[4] == "'success' is a required property"
def test_is_schemathesis_test():
# When a test is wrapped into with `parametrize`
schema = schemathesis.from_dict(MINIMAL_SCHEMA)
@schema.parametrize()
def test_a():
pass
# Then is should be recognized as a schemathesis test
assert is_schemathesis_test(test_a)
# For sake of example to check allOf logic
{"type": "string", "example": datetime.date(2020, 1, 1)},
{"type": "string", "example": datetime.date(2020, 1, 1)},
]
},
}
],
}
}
},
}
schema_path = testdir.makefile(".yaml", schema=yaml.dump(schema))
# Then yaml loader should ignore it
# And data generation should work without errors
schema = schemathesis.from_path(str(schema_path))
@given(case=schema["/teapot"]["GET"].as_strategy())
def test(case):
assert isinstance(case.query["key"], str)
test()
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:
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
assert "Path parameters" not in out
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_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} ",