Skip to content

Commit

Permalink
Rename parametrize parameters.
Browse files Browse the repository at this point in the history
``filter_endpoint`` to ``endpoint`` and ``filter_method`` to ``method``.
  • Loading branch information
dmitry.dygalo authored and Stranger6667 committed Sep 24, 2019
1 parent c69f845 commit 5a86787
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 38 deletions.
10 changes: 10 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,16 @@ To limit the number of examples you could use ``hypothesis.settings`` decorator
def test_something(client, case):
...
To narrow down the scope of the schemathesis tests it is possible to filter by method or endpoint:

.. code:: python
@schema.parametrize(method="GET", endpoint="/pet")
def test_no_server_errors(case):
...
The acceptable values are regexps or list of regexps (matched with ``re.search``).

Explicit examples
~~~~~~~~~~~~~~~~~

Expand Down
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Changed
~~~~~~~

- Rename module ``readers`` to ``loaders``.
- Rename ``parametrize`` parameters. ``filter_endpoint`` to ``endpoint`` and ``filter_method`` to ``method``.

Removed
~~~~~~~
Expand Down
16 changes: 8 additions & 8 deletions src/schemathesis/lazy.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
class LazySchema:
fixture_name: str = attr.ib()

def parametrize(self, filter_method: Optional[Filter] = None, filter_endpoint: Optional[Filter] = None) -> Callable:
def parametrize(self, method: Optional[Filter] = None, endpoint: Optional[Filter] = None) -> Callable:
def wrapper(func: Callable) -> Callable:
def test(request: FixtureRequest, subtests: SubTests) -> None:
schema = get_schema(request, self.fixture_name, filter_method, filter_endpoint)
schema = get_schema(request, self.fixture_name, method, endpoint)
fixtures = get_fixtures(func, request)
node_id = subtests.item._nodeid
for endpoint, sub_test in schema.get_all_tests(func):
subtests.item._nodeid = f"{node_id}[{endpoint.method}:{endpoint.path}]"
with subtests.test(method=endpoint.method, path=endpoint.path):
for _endpoint, sub_test in schema.get_all_tests(func):
subtests.item._nodeid = f"{node_id}[{_endpoint.method}:{_endpoint.path}]"
with subtests.test(method=_endpoint.method, path=_endpoint.path):
sub_test(**fixtures)
subtests.item._nodeid = node_id

Expand All @@ -31,14 +31,14 @@ def test(request: FixtureRequest, subtests: SubTests) -> None:


def get_schema(
request: FixtureRequest, name: str, filter_method: Optional[Filter] = None, filter_endpoint: Optional[Filter] = None
request: FixtureRequest, name: str, method: Optional[Filter] = None, endpoint: Optional[Filter] = None
) -> BaseSchema:
"""Loads a schema from the fixture."""
schema = request.getfixturevalue(name)
if not isinstance(schema, BaseSchema):
raise ValueError(f"The given schema must be an instance of BaseSchema, got: {type(schema)}")
schema.filter_method = filter_method
schema.filter_endpoint = filter_endpoint
schema.method = method
schema.endpoint = endpoint
return schema


Expand Down
14 changes: 6 additions & 8 deletions src/schemathesis/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
@attr.s(slots=True)
class BaseSchema:
raw_schema: Dict[str, Any] = attr.ib()
filter_method: Optional[Filter] = attr.ib(default=None)
filter_endpoint: Optional[Filter] = attr.ib(default=None)
method: Optional[Filter] = attr.ib(default=None)
endpoint: Optional[Filter] = attr.ib(default=None)

@property
def resolver(self) -> jsonschema.RefResolver:
Expand All @@ -41,13 +41,11 @@ def get_all_tests(self, func: Callable) -> Generator[Tuple[Endpoint, Callable],
for endpoint in self.get_all_endpoints():
yield endpoint, create_test(endpoint, func)

def parametrize(self, filter_method: Optional[Filter] = None, filter_endpoint: Optional[Filter] = None) -> Callable:
def parametrize(self, method: Optional[Filter] = None, endpoint: Optional[Filter] = None) -> Callable:
"""Mark a test function as a parametrized one."""

def wrapper(func: Callable) -> Callable:
func._schemathesis_test = self.__class__( # type: ignore
self.raw_schema, filter_method=filter_method, filter_endpoint=filter_endpoint
)
func._schemathesis_test = self.__class__(self.raw_schema, method=method, endpoint=endpoint) # type: ignore
return func

return wrapper
Expand All @@ -70,11 +68,11 @@ def get_all_endpoints(self) -> Generator[Endpoint, None, None]:
paths = self.raw_schema["paths"] # pylint: disable=unsubscriptable-object
for path, methods in paths.items():
full_path = self.get_full_path(path)
if should_skip_endpoint(full_path, self.filter_endpoint):
if should_skip_endpoint(full_path, self.endpoint):
continue
common_parameters = get_common_parameters(methods)
for method, definition in methods.items():
if method == "parameters" or should_skip_method(method, self.filter_method):
if method == "parameters" or should_skip_method(method, self.method):
continue
parameters = itertools.chain(definition.get("parameters", ()), common_parameters)
yield self.make_endpoint(full_path, method, parameters, definition)
Expand Down
4 changes: 2 additions & 2 deletions test/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def test_endpoint_filter(testdir, endpoint):
parameters = {"parameters": [integer(name="id", required=True)]}
testdir.make_test(
"""
@schema.parametrize(filter_endpoint={})
@schema.parametrize(endpoint={})
@settings(max_examples=5)
def test_(request, case):
request.config.HYPOTHESIS_CASES += 1
Expand All @@ -32,7 +32,7 @@ def test_method_filter(testdir, method):
parameters = {"parameters": [integer(name="id", required=True)]}
testdir.make_test(
"""
@schema.parametrize(filter_method={})
@schema.parametrize(method={})
@settings(max_examples=1)
def test_(request, case):
request.config.HYPOTHESIS_CASES += 1
Expand Down
2 changes: 1 addition & 1 deletion test/test_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_path(testdir):
# When parameter is specified for "path"
testdir.make_test(
"""
@schema.parametrize(filter_endpoint="/users/{user_id}")
@schema.parametrize(endpoint="/users/{user_id}")
@settings(max_examples=3)
def test_(case):
assert_int(case.path_parameters["user_id"])
Expand Down
2 changes: 1 addition & 1 deletion test/test_parametrization.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def test_deselecting(testdir):
def test_a(request, case):
request.config.HYPOTHESIS_CASES += 1
@schema.parametrize(filter_endpoint="pets")
@schema.parametrize(endpoint="pets")
@settings(max_examples=1)
def test_b(request, case):
request.config.HYPOTHESIS_CASES += 1
Expand Down
36 changes: 18 additions & 18 deletions test/test_petstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def assert_petstore(passed=1, tests_num=5):
def test_pet(testdir):
testdir.make_petstore_test(
"""
@schema.parametrize(filter_endpoint="/pet$")
@schema.parametrize(endpoint="/pet$")
@settings(max_examples=5)
def test_(request, case):
request.config.HYPOTHESIS_CASES += 1
Expand All @@ -36,7 +36,7 @@ def test_(request, case):
def test_find_by_status(testdir):
testdir.make_petstore_test(
"""
@schema.parametrize(filter_endpoint="/pet/findByStatus$")
@schema.parametrize(endpoint="/pet/findByStatus$")
@settings(max_examples=5)
def test_(request, case):
request.config.HYPOTHESIS_CASES += 1
Expand All @@ -51,7 +51,7 @@ def test_(request, case):
def test_find_by_tag(testdir):
testdir.make_petstore_test(
"""
@schema.parametrize(filter_endpoint="/pet/findByTags$")
@schema.parametrize(endpoint="/pet/findByTags$")
@settings(max_examples=5)
def test_(request, case):
request.config.HYPOTHESIS_CASES += 1
Expand All @@ -64,7 +64,7 @@ def test_(request, case):
def test_get_pet(testdir):
testdir.make_petstore_test(
"""
@schema.parametrize(filter_method="GET", filter_endpoint="/pet/{petId}$")
@schema.parametrize(method="GET", endpoint="/pet/{petId}$")
@settings(max_examples=5)
def test_(request, case):
request.config.HYPOTHESIS_CASES += 1
Expand All @@ -78,7 +78,7 @@ def test_(request, case):
def test_update_pet(testdir):
testdir.make_petstore_test(
"""
@schema.parametrize(filter_method="POST", filter_endpoint="/pet/{petId}$")
@schema.parametrize(method="POST", endpoint="/pet/{petId}$")
@settings(max_examples=5)
def test_(request, case):
request.config.HYPOTHESIS_CASES += 1
Expand All @@ -91,7 +91,7 @@ def test_(request, case):
def test_delete_pet(testdir):
testdir.make_petstore_test(
"""
@schema.parametrize(filter_method="DELETE", filter_endpoint="/pet/{petId}$")
@schema.parametrize(method="DELETE", endpoint="/pet/{petId}$")
@settings(max_examples=5)
def test_(request, case):
request.config.HYPOTHESIS_CASES += 1
Expand All @@ -106,7 +106,7 @@ def test_(request, case):
def test_upload_image(testdir):
testdir.make_petstore_test(
"""
@schema.parametrize(filter_endpoint="/pet/{petId}/uploadImage$")
@schema.parametrize(endpoint="/pet/{petId}/uploadImage$")
@settings(max_examples=5)
def test_(request, case):
request.config.HYPOTHESIS_CASES += 1
Expand All @@ -119,7 +119,7 @@ def test_(request, case):
def test_get_inventory(testdir):
testdir.make_petstore_test(
"""
@schema.parametrize(filter_endpoint="/store/inventory$")
@schema.parametrize(endpoint="/store/inventory$")
@settings(max_examples=5)
def test_(request, case):
request.config.HYPOTHESIS_CASES += 1
Expand All @@ -134,7 +134,7 @@ def test_(request, case):
def test_create_order(testdir):
testdir.make_petstore_test(
"""
@schema.parametrize(filter_endpoint="/store/order$")
@schema.parametrize(endpoint="/store/order$")
@settings(max_examples=5)
def test_(request, case):
request.config.HYPOTHESIS_CASES += 1
Expand All @@ -146,7 +146,7 @@ def test_(request, case):
def test_get_order(testdir):
testdir.make_petstore_test(
"""
@schema.parametrize(filter_method="GET", filter_endpoint="/store/order/{orderId}$")
@schema.parametrize(method="GET", endpoint="/store/order/{orderId}$")
@settings(max_examples=5)
def test_(request, case):
request.config.HYPOTHESIS_CASES += 1
Expand All @@ -160,7 +160,7 @@ def test_(request, case):
def test_delete_order(testdir):
testdir.make_petstore_test(
"""
@schema.parametrize(filter_method="DELETE", filter_endpoint="/store/order/{orderId}$")
@schema.parametrize(method="DELETE", endpoint="/store/order/{orderId}$")
@settings(max_examples=5)
def test_(request, case):
request.config.HYPOTHESIS_CASES += 1
Expand All @@ -174,7 +174,7 @@ def test_(request, case):
def test_create_user(testdir):
testdir.make_petstore_test(
"""
@schema.parametrize(filter_endpoint="/user$")
@schema.parametrize(endpoint="/user$")
@settings(max_examples=5)
def test_(request, case):
request.config.HYPOTHESIS_CASES += 1
Expand All @@ -187,7 +187,7 @@ def test_(request, case):
def test_create_multiple_users(testdir):
testdir.make_petstore_test(
"""
@schema.parametrize(filter_endpoint="/user/createWith")
@schema.parametrize(endpoint="/user/createWith")
@settings(max_examples=5)
def test_(request, case):
request.config.HYPOTHESIS_CASES += 1
Expand All @@ -200,7 +200,7 @@ def test_(request, case):
def test_login(testdir):
testdir.make_petstore_test(
"""
@schema.parametrize(filter_endpoint="/user/login")
@schema.parametrize(endpoint="/user/login")
@settings(max_examples=5)
def test_(request, case):
request.config.HYPOTHESIS_CASES += 1
Expand All @@ -214,7 +214,7 @@ def test_(request, case):
def test_logout(testdir):
testdir.make_petstore_test(
"""
@schema.parametrize(filter_endpoint="/user/logout")
@schema.parametrize(endpoint="/user/logout")
@settings(max_examples=5)
def test_(request, case):
request.config.HYPOTHESIS_CASES += 1
Expand All @@ -229,7 +229,7 @@ def test_(request, case):
def test_get_user(testdir):
testdir.make_petstore_test(
"""
@schema.parametrize(filter_method="GET", filter_endpoint="/user/{username}$")
@schema.parametrize(method="GET", endpoint="/user/{username}$")
@settings(max_examples=5)
def test_(request, case):
request.config.HYPOTHESIS_CASES += 1
Expand All @@ -242,7 +242,7 @@ def test_(request, case):
def test_update_user(testdir):
testdir.make_petstore_test(
"""
@schema.parametrize(filter_method="PUT", filter_endpoint="/user/{username}$")
@schema.parametrize(method="PUT", endpoint="/user/{username}$")
@settings(max_examples=5)
def test_(request, case):
request.config.HYPOTHESIS_CASES += 1
Expand All @@ -256,7 +256,7 @@ def test_(request, case):
def test_delete_user(testdir):
testdir.make_petstore_test(
"""
@schema.parametrize(filter_method="DELETE", filter_endpoint="/user/{username}$")
@schema.parametrize(method="DELETE", endpoint="/user/{username}$")
@settings(max_examples=5)
def test_(request, case):
request.config.HYPOTHESIS_CASES += 1
Expand Down

0 comments on commit 5a86787

Please sign in to comment.