Merge pull request 'Fixed wrong examples' (#477) from Qubasa-main into main
This commit is contained in:
commit
f2062db56c
@ -92,40 +92,20 @@ def test_get_task(case: deal.TestCase) -> None:
|
|||||||
case() # <--- Call testing framework with function
|
case() # <--- Call testing framework with function
|
||||||
```
|
```
|
||||||
|
|
||||||
### Combining hypothesis with deal
|
### Adding example input for deeper testing
|
||||||
|
|
||||||
You can combine hypothesis annotations with deal annotations to add example inputs to the function so that the verifier can reach deeper parts of the function.
|
You can combine hypothesis annotations with deal annotations to add example inputs to the function so that the verifier can reach deeper parts of the function.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
import hypothesis
|
|
||||||
import deal
|
import deal
|
||||||
|
|
||||||
@hypothesis.example(["8c3041e0-4512-4b30-aa8e-7be4a75b8b45", "5c2061e0-4512-4b30-aa8e-7be4a75b8b45"])
|
@deal.example(lambda: get_task(UUID("5c2061e0-4512-4b30-aa8e-7be4a75b8b45"))) # type: ignore
|
||||||
|
@deal.example(lambda: get_task(UUID("7c2061e6-4512-4b30-aa8e-7be4a75b8b45"))) # type: ignore
|
||||||
@deal.raises(ClanError)
|
@deal.raises(ClanError)
|
||||||
def get_task(uuid: UUID) -> BaseTask:
|
def get_task(uuid: UUID) -> BaseTask:
|
||||||
global POOL
|
global POOL
|
||||||
if "206" in str(uuid):
|
|
||||||
raise ValueError("206 should not be in here")
|
|
||||||
return POOL[uuid]
|
return POOL[uuid]
|
||||||
```
|
```
|
||||||
|
|
||||||
You can also annotate what kind of information a value can have by adding a hypothesis "strategy". Which kind of data generation hypothesis supports you can see here:
|
|
||||||
[Hypothesis Value Properties](https://hypothesis.readthedocs.io/en/latest/data.html)
|
|
||||||
The example above doesn't really need a hypothesis annotation because deal can infer from the UUID type a strategy.
|
|
||||||
But as an example how it might look like:
|
|
||||||
|
|
||||||
```python
|
|
||||||
from hypothesis.strategies import uuids
|
|
||||||
|
|
||||||
@hypothesis.given(uuids)
|
|
||||||
@deal.raises(ClanError)
|
|
||||||
def get_task(uuid: UUID) -> BaseTask:
|
|
||||||
global POOL
|
|
||||||
if "206" in str(uuid):
|
|
||||||
raise ValueError("206 should not be in here")
|
|
||||||
return POOL[uuid]
|
|
||||||
```
|
|
||||||
|
|
||||||
For a complex example for an [HTTP API look here](https://hypothesis.readthedocs.io/en/latest/examples.html#fuzzing-an-http-api). Just note we are using schemathesis for this already.
|
|
||||||
|
|
||||||
You can also add `pre` and `post` conditions. A `pre` condition must be true before the function is executed. A `post` condition must be true after the function was executed. For more information read the [Writing Contracts Section](https://deal.readthedocs.io/basic/values.html).
|
You can also add `pre` and `post` conditions. A `pre` condition must be true before the function is executed. A `post` condition must be true after the function was executed. For more information read the [Writing Contracts Section](https://deal.readthedocs.io/basic/values.html).
|
||||||
|
Or read the [API doc of Deal](https://deal.readthedocs.io/details/api.html)
|
||||||
|
@ -26,6 +26,7 @@ norecursedirs = "tests/helpers"
|
|||||||
markers = [ "impure" ]
|
markers = [ "impure" ]
|
||||||
|
|
||||||
[tool.mypy]
|
[tool.mypy]
|
||||||
|
plugins = ["deal.mypy"]
|
||||||
python_version = "3.10"
|
python_version = "3.10"
|
||||||
warn_redundant_casts = true
|
warn_redundant_casts = true
|
||||||
disallow_untyped_calls = true
|
disallow_untyped_calls = true
|
||||||
|
@ -3,7 +3,6 @@ import deal
|
|||||||
from clan_cli.task_manager import get_task
|
from clan_cli.task_manager import get_task
|
||||||
|
|
||||||
|
|
||||||
# type annotations below are optional
|
|
||||||
@deal.cases(get_task)
|
@deal.cases(get_task)
|
||||||
def test_get_task(case: deal.TestCase) -> None:
|
def test_get_task(case: deal.TestCase) -> None:
|
||||||
case()
|
case()
|
||||||
|
Loading…
Reference in New Issue
Block a user