Skip to content

Commit

Permalink
chore: Return registered functions from register_target and `regist…
Browse files Browse the repository at this point in the history
…er_check` decorators
  • Loading branch information
Stranger6667 committed Sep 17, 2020
1 parent 7b367b7 commit a539512
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
2 changes: 2 additions & 0 deletions docs/changelog.rst
Expand Up @@ -10,6 +10,7 @@ Changed
~~~~~~~

- Hide ``Case.endpoint`` from representation. Its representation decreases the usability of pytest's output. `#719`_
- Return registered functions from ``register_target`` and ``register_check`` decorators. `#721`_

`2.4.0`_ - 2020-09-15
---------------------
Expand Down Expand Up @@ -1338,6 +1339,7 @@ Fixed
.. _0.3.0: https://github.com/schemathesis/schemathesis/compare/v0.2.0...v0.3.0
.. _0.2.0: https://github.com/schemathesis/schemathesis/compare/v0.1.0...v0.2.0

.. _#721: https://github.com/schemathesis/schemathesis/issues/721
.. _#719: https://github.com/schemathesis/schemathesis/issues/719
.. _#708: https://github.com/schemathesis/schemathesis/issues/708
.. _#705: https://github.com/schemathesis/schemathesis/issues/705
Expand Down
8 changes: 6 additions & 2 deletions src/schemathesis/cli/__init__.py
Expand Up @@ -49,16 +49,20 @@ def _get_callable_names(items: Tuple[Callable, ...]) -> Tuple[str, ...]:
MAX_WORKERS = 64


def register_target(function: Target) -> None:
def register_target(function: Target) -> Target:
"""Register a new testing target for schemathesis CLI."""
targets_module.ALL_TARGETS += (function,)
TARGETS_TYPE.choices += (function.__name__,) # type: ignore
return function


def register_check(function: Callable[[Union[requests.Response, WSGIResponse], models.Case], None]) -> None:
def register_check(
function: Callable[[Union[requests.Response, WSGIResponse], models.Case], None]
) -> Callable[[Union[requests.Response, WSGIResponse], models.Case], None]:
"""Register a new check for schemathesis CLI."""
checks_module.ALL_CHECKS += (function,)
CHECKS_TYPE.choices += (function.__name__,) # type: ignore
return function


def reset_checks() -> None:
Expand Down
22 changes: 22 additions & 0 deletions test/cli/test_checks.py
@@ -0,0 +1,22 @@
import pytest

import schemathesis
from schemathesis.cli import reset_checks


@pytest.fixture
def new_check():
@schemathesis.register_check
def check_function(response, case):
pass

yield check_function

reset_checks()


def test_register_returns_a_value(new_check):
# When a function is registered via the `register_check` decorator
# Then this function should be available for further usage
# See #721
assert new_check is not None
19 changes: 19 additions & 0 deletions test/cli/test_targeted.py
@@ -1,6 +1,7 @@
import pytest
from _pytest.main import ExitCode

import schemathesis
from schemathesis.cli import reset_targets


Expand Down Expand Up @@ -35,3 +36,21 @@ def test_custom_target(testdir, cli, new_target, openapi3_schema_url):
assert result.exit_code == ExitCode.OK, result.stdout
# And the specified target is called
assert "NEW TARGET IS CALLED" in result.stdout


@pytest.fixture
def target_function():
@schemathesis.register_target
def new_target(context):
return 0.5

yield target_function

reset_targets()


def test_register_returns_a_value(target_function):
# When a function is registered via the `register_target` decorator
# Then this function should be available for further usage
# See #721
assert target_function is not None

0 comments on commit a539512

Please sign in to comment.