-
-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement parametrizing tests from pytest fixtures
- Loading branch information
1 parent
5913e3d
commit 441ed34
Showing
9 changed files
with
154 additions
and
8 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
from .loaders import Parametrizer, from_dict, from_file, from_path, from_uri | ||
from .loaders import Parametrizer, from_dict, from_file, from_path, from_pytest_fixture, from_uri | ||
from .models import Case |
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,48 @@ | ||
from inspect import signature | ||
from typing import Any, Callable, Dict, Optional | ||
|
||
import attr | ||
from _pytest.fixtures import FixtureRequest | ||
from pytest_subtests import SubTests | ||
|
||
from .schemas import BaseSchema | ||
from .types import Filter | ||
|
||
|
||
@attr.s(slots=True) | ||
class LazySchema: | ||
fixture_name: str = attr.ib() | ||
|
||
def parametrize(self, filter_method: Optional[Filter] = None, filter_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) | ||
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): | ||
sub_test(**fixtures) | ||
subtests.item._nodeid = node_id | ||
|
||
return test | ||
|
||
return wrapper | ||
|
||
|
||
def get_schema( | ||
request: FixtureRequest, name: str, filter_method: Optional[Filter] = None, filter_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 | ||
return schema | ||
|
||
|
||
def get_fixtures(func: Callable, request: FixtureRequest) -> Dict[str, Any]: | ||
"""Load fixtures, needed for the test function.""" | ||
sig = signature(func) | ||
return {name: request.getfixturevalue(name) for name in sig.parameters if name != "case"} |
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,49 @@ | ||
def test_default(testdir): | ||
# When LazySchema is used | ||
testdir.make_test( | ||
""" | ||
@pytest.fixture | ||
def simple_schema(): | ||
return schema | ||
lazy_schema = schemathesis.from_pytest_fixture("simple_schema") | ||
@lazy_schema.parametrize() | ||
def test_(case): | ||
assert case.path == "/v1/users" | ||
assert case.method == "GET" | ||
""" | ||
) | ||
result = testdir.runpytest("-v") | ||
# Then the generated test should use this fixture | ||
result.assert_outcomes(passed=1) | ||
result.stdout.re_match_lines([r"test_default.py::test_ PASSED", r".*1 passed"]) | ||
|
||
|
||
def test_with_fixtures(testdir): | ||
# When the test uses custom arguments for pytest fixtures | ||
testdir.make_test( | ||
""" | ||
@pytest.fixture | ||
def simple_schema(): | ||
return schema | ||
lazy_schema = schemathesis.from_pytest_fixture("simple_schema") | ||
@pytest.fixture | ||
def another(): | ||
return 1 | ||
@lazy_schema.parametrize() | ||
def test_(request, case, another): | ||
request.config.HYPOTHESIS_CASES += 1 | ||
assert case.path == "/v1/users" | ||
assert case.method == "GET" | ||
assert another == 1 | ||
""" | ||
) | ||
result = testdir.runpytest("-v") | ||
# Then the generated test should use these fixtures | ||
result.assert_outcomes(passed=1) | ||
result.stdout.re_match_lines([r"test_with_fixtures.py::test_ PASSED", r".*1 passed"]) | ||
result.stdout.re_match_lines([r"Hypothesis calls: 1"]) |