Skip to content

Commit

Permalink
refactor(runner): Convert Hypothesis configuration options earlier
Browse files Browse the repository at this point in the history
  • Loading branch information
Stranger6667 committed Apr 3, 2021
1 parent 4652ec5 commit 5b9d757
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 37 deletions.
38 changes: 22 additions & 16 deletions src/schemathesis/runner/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@

from .. import fixups as _fixups
from ..checks import DEFAULT_CHECKS
from ..constants import DEFAULT_DATA_GENERATION_METHODS, DEFAULT_STATEFUL_RECURSION_LIMIT, DataGenerationMethod
from ..constants import (
DEFAULT_DATA_GENERATION_METHODS,
DEFAULT_DEADLINE,
DEFAULT_STATEFUL_RECURSION_LIMIT,
DataGenerationMethod,
)
from ..models import CheckFunction
from ..schemas import BaseSchema
from ..specs.graphql import loaders as gql_loaders
Expand Down Expand Up @@ -77,7 +82,7 @@ def prepare(
if auth is None:
# Auth type doesn't matter if auth is not passed
auth_type = None # type: ignore
hypothesis_options = prepare_hypothesis_options(
hypothesis_settings = prepare_hypothesis_settings(
deadline=hypothesis_deadline,
derandomize=hypothesis_derandomize,
max_examples=hypothesis_max_examples,
Expand All @@ -102,7 +107,7 @@ def prepare(
data_generation_methods=data_generation_methods,
max_response_time=max_response_time,
targets=targets,
hypothesis_options=hypothesis_options,
hypothesis_settings=hypothesis_settings,
seed=seed,
workers_num=workers_num,
exit_first=exit_first,
Expand Down Expand Up @@ -161,7 +166,7 @@ def execute_from_schema(
max_response_time: Optional[int] = None,
targets: Iterable[Target],
workers_num: int = 1,
hypothesis_options: Dict[str, Any],
hypothesis_settings: hypothesis.settings,
auth: Optional[RawAuth] = None,
auth_type: Optional[str] = None,
headers: Optional[Dict[str, Any]] = None,
Expand Down Expand Up @@ -217,7 +222,7 @@ def execute_from_schema(
checks=checks,
max_response_time=max_response_time,
targets=targets,
hypothesis_settings=hypothesis_options,
hypothesis_settings=hypothesis_settings,
auth=auth,
auth_type=auth_type,
headers=headers,
Expand All @@ -238,7 +243,7 @@ def execute_from_schema(
checks=checks,
max_response_time=max_response_time,
targets=targets,
hypothesis_settings=hypothesis_options,
hypothesis_settings=hypothesis_settings,
auth=auth,
auth_type=auth_type,
headers=headers,
Expand All @@ -256,7 +261,7 @@ def execute_from_schema(
checks=checks,
max_response_time=max_response_time,
targets=targets,
hypothesis_settings=hypothesis_options,
hypothesis_settings=hypothesis_settings,
auth=auth,
auth_type=auth_type,
headers=headers,
Expand All @@ -276,7 +281,7 @@ def execute_from_schema(
checks=checks,
max_response_time=max_response_time,
targets=targets,
hypothesis_settings=hypothesis_options,
hypothesis_settings=hypothesis_settings,
auth=auth,
auth_type=auth_type,
headers=headers,
Expand All @@ -296,7 +301,7 @@ def execute_from_schema(
checks=checks,
max_response_time=max_response_time,
targets=targets,
hypothesis_settings=hypothesis_options,
hypothesis_settings=hypothesis_settings,
auth=auth,
auth_type=auth_type,
headers=headers,
Expand All @@ -314,7 +319,7 @@ def execute_from_schema(
checks=checks,
max_response_time=max_response_time,
targets=targets,
hypothesis_settings=hypothesis_options,
hypothesis_settings=hypothesis_settings,
auth=auth,
auth_type=auth_type,
headers=headers,
Expand Down Expand Up @@ -389,16 +394,16 @@ def load_schema(
)


def prepare_hypothesis_options(
def prepare_hypothesis_settings(
deadline: Optional[Union[int, NotSet]] = None,
derandomize: Optional[bool] = None,
max_examples: Optional[int] = None,
phases: Optional[List[hypothesis.Phase]] = None,
report_multiple_bugs: Optional[bool] = None,
suppress_health_check: Optional[List[hypothesis.HealthCheck]] = None,
verbosity: Optional[hypothesis.Verbosity] = None,
) -> Dict[str, Any]:
options = dict_not_none_values(
) -> hypothesis.settings:
kwargs = dict_not_none_values(
derandomize=derandomize,
max_examples=max_examples,
phases=phases,
Expand All @@ -409,7 +414,8 @@ def prepare_hypothesis_options(
# `deadline` is special, since Hypothesis allows passing `None`
if deadline is not None:
if isinstance(deadline, NotSet):
options["deadline"] = None
kwargs["deadline"] = None
else:
options["deadline"] = deadline
return options
kwargs["deadline"] = deadline
kwargs.setdefault("deadline", DEFAULT_DEADLINE)
return hypothesis.settings(**kwargs)
9 changes: 1 addition & 8 deletions src/schemathesis/runner/impl/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from requests.auth import HTTPDigestAuth, _basic_auth_str

from ...constants import (
DEFAULT_DEADLINE,
DEFAULT_STATEFUL_RECURSION_LIMIT,
RECURSIVE_REFERENCE_ERROR_MESSAGE,
USER_AGENT,
Expand All @@ -34,19 +33,13 @@
from ..serialization import SerializedTestResult


def get_hypothesis_settings(hypothesis_options: Dict[str, Any]) -> hypothesis.settings:
# Default settings, used as a parent settings object below
hypothesis_options.setdefault("deadline", DEFAULT_DEADLINE)
return hypothesis.settings(**hypothesis_options)


@attr.s # pragma: no mutate
class BaseRunner:
schema: BaseSchema = attr.ib() # pragma: no mutate
checks: Iterable[CheckFunction] = attr.ib() # pragma: no mutate
max_response_time: Optional[int] = attr.ib() # pragma: no mutate
targets: Iterable[Target] = attr.ib() # pragma: no mutate
hypothesis_settings: hypothesis.settings = attr.ib(converter=get_hypothesis_settings) # pragma: no mutate
hypothesis_settings: hypothesis.settings = attr.ib() # pragma: no mutate
auth: Optional[RawAuth] = attr.ib(default=None) # pragma: no mutate
auth_type: Optional[str] = attr.ib(default=None) # pragma: no mutate
headers: Optional[Dict[str, Any]] = attr.ib(default=None) # pragma: no mutate
Expand Down
30 changes: 17 additions & 13 deletions test/cli/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,18 +305,18 @@ def test_commands_run_help(cli):
"--hypothesis-verbosity=normal",
],
{
"hypothesis_options": {
"deadline": 1000,
"derandomize": True,
"max_examples": 1000,
"phases": [Phase.explicit, Phase.generate],
"report_multiple_bugs": False,
"suppress_health_check": [HealthCheck.too_slow, HealthCheck.filter_too_much],
"verbosity": Verbosity.normal,
}
"hypothesis_settings": hypothesis.settings(
deadline=1000,
derandomize=True,
max_examples=1000,
phases=[Phase.explicit, Phase.generate],
report_multiple_bugs=False,
suppress_health_check=[HealthCheck.too_slow, HealthCheck.filter_too_much],
verbosity=Verbosity.normal,
)
},
),
(["--hypothesis-deadline=None"], {"hypothesis_options": {"deadline": None}}),
(["--hypothesis-deadline=None"], {"hypothesis_settings": hypothesis.settings(deadline=None)}),
(["--max-response-time=10"], {"max_response_time": 10}),
),
)
Expand Down Expand Up @@ -344,7 +344,6 @@ def test_execute_arguments(cli, mocker, simple_schema, args, expected):
"skip_deprecated_operations": False,
"force_schema_version": None,
"loader": from_uri,
"hypothesis_options": {},
"workers_num": 1,
"exit_first": False,
"dry_run": False,
Expand All @@ -362,9 +361,14 @@ def test_execute_arguments(cli, mocker, simple_schema, args, expected):
"count_operations": True,
**expected,
}

assert result.exit_code == ExitCode.OK, result.stdout
assert execute.call_args[1] == expected
hypothesis_settings = expected.pop("hypothesis_settings", None)
call_kwargs = execute.call_args[1]
executed_hypothesis_settings = call_kwargs.pop("hypothesis_settings", None)
if hypothesis_settings is not None:
# Compare non-default Hypothesis settings as `hypothesis.settings` can't be compared
assert executed_hypothesis_settings.show_changed() == hypothesis_settings.show_changed()
assert call_kwargs == expected


@pytest.mark.parametrize(
Expand Down

0 comments on commit 5b9d757

Please sign in to comment.