Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ce20655
commit cce1e95
Showing
12 changed files
with
167 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ Welcome to schemathesis's documentation! | |
:caption: Contents: | ||
|
||
usage | ||
targeted | ||
faq | ||
changelog | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
.. _targeted: | ||
|
||
Targeted property-based testing | ||
=============================== | ||
|
||
Schemathesis supports targeted property-based testing via utilizing ``hypothesis.target`` inside its runner and provides | ||
an API to guide data generation towards certain pre-defined goals: | ||
|
||
- ``response_time``. Hypothesis will try to generate input that will more likely to have higher response time; | ||
|
||
To illustrate this feature, consider the following AioHTTP endpoint, that contains a hidden performance problem - | ||
the more zeroes are in the input number the slower it works and if there are more than 10 zeroes it will cause an internal | ||
server error: | ||
|
||
.. code:: python | ||
async def performance(request: web.Request) -> web.Response: | ||
decoded = await request.json() | ||
number = str(decoded).count("0") | ||
if number > 0: | ||
# emulate hard work | ||
await asyncio.sleep(0.01 * number) | ||
if number > 10: | ||
raise web.HTTPInternalServerError | ||
return web.json_response({"slow": True}) | ||
Let's take a look if Schemathesis can discover this issue and how much time it will take: | ||
|
||
.. code:: bash | ||
$ schemathesis run --hypothesis-max-examples=100000 http://127.0.0.1:8081/swagger.yaml | ||
... | ||
1. Received a response with 5xx status code: 500 | ||
Check : not_a_server_error | ||
Body : 58150920460703009030426716484679203200 | ||
Run this Python code to reproduce this failure: | ||
requests.post('http://127.0.0.1:8081/api/performance', json=58150920460703009030426716484679203200) | ||
Or add this option to your command line parameters: --hypothesis-seed=240368931405400688094965957483327791742 | ||
================================================== SUMMARY ================================================== | ||
Performed checks: | ||
not_a_server_error 67993 / 68041 passed FAILED | ||
============================================ 1 failed in 662.16s =========================================== | ||
And with targeted testing (``.hypothesis`` directory was removed between these test runs to avoid reusing results): | ||
|
||
.. code:: bash | ||
$ schemathesis run --target=response_time --hypothesis-max-examples=100000 http://127.0.0.1:8081/swagger.yaml | ||
... | ||
1. Received a response with 5xx status code: 500 | ||
Check : not_a_server_error | ||
Body : 2600050604444474172950385505254500000 | ||
Run this Python code to reproduce this failure: | ||
requests.post('http://127.0.0.1:8081/api/performance', json=2600050604444474172950385505254500000) | ||
Or add this option to your command line parameters: --hypothesis-seed=340229547842147149729957578683815058325 | ||
================================================== SUMMARY ================================================== | ||
Performed checks: | ||
not_a_server_error 22039 / 22254 passed FAILED | ||
============================================ 1 failed in 305.50s =========================================== | ||
This behavior is reproducible in general, but not guaranteed due to the randomness of data generation. However, it shows | ||
a significant testing time reduction especially on a big number of examples. | ||
|
||
Hypothesis `documentation <https://hypothesis.readthedocs.io/en/latest/details.html#targeted-example-generation>`_ provides a detailed explanation of what targeted property-based testing is. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from enum import Enum, unique | ||
|
||
DEFAULT_TARGETS = () | ||
DEFAULT_TARGETS_NAMES = () | ||
|
||
|
||
@unique | ||
class Target(Enum): | ||
response_time = 1 |
Oops, something went wrong.