-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #163 from pytest-dev/fixture_name_from_factory
Determine the fixture name using the factory name, rather than the model
- Loading branch information
Showing
8 changed files
with
153 additions
and
42 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
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,55 @@ | ||
from __future__ import annotations | ||
|
||
import sys | ||
|
||
from _pytest.pytester import RunResult | ||
from packaging.version import Version | ||
|
||
if sys.version_info >= (3, 8): | ||
from importlib import metadata | ||
else: | ||
import importlib_metadata as metadata | ||
|
||
PYTEST_VERSION = Version(metadata.version("pytest")) | ||
|
||
if PYTEST_VERSION >= Version("6.0.0"): | ||
|
||
def assert_outcomes( | ||
result: RunResult, | ||
passed: int = 0, | ||
skipped: int = 0, | ||
failed: int = 0, | ||
errors: int = 0, | ||
xpassed: int = 0, | ||
xfailed: int = 0, | ||
) -> None: | ||
"""Compatibility function for result.assert_outcomes""" | ||
result.assert_outcomes( | ||
errors=errors, | ||
passed=passed, | ||
skipped=skipped, | ||
failed=failed, | ||
xpassed=xpassed, | ||
xfailed=xfailed, | ||
) | ||
|
||
else: | ||
|
||
def assert_outcomes( | ||
result: RunResult, | ||
passed: int = 0, | ||
skipped: int = 0, | ||
failed: int = 0, | ||
errors: int = 0, | ||
xpassed: int = 0, | ||
xfailed: int = 0, | ||
) -> None: | ||
"""Compatibility function for result.assert_outcomes""" | ||
result.assert_outcomes( | ||
error=errors, # Pytest < 6 uses the singular form | ||
passed=passed, | ||
skipped=skipped, | ||
failed=failed, | ||
xpassed=xpassed, | ||
xfailed=xfailed, | ||
) # type: ignore[call-arg] |
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 @@ | ||
pytest_plugins = "pytester" |
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,63 @@ | ||
from __future__ import annotations | ||
|
||
import factory | ||
|
||
from pytest_factoryboy import register | ||
from tests.compat import assert_outcomes | ||
|
||
|
||
@register | ||
class JSONPayloadFactory(factory.Factory): | ||
class Meta: | ||
model = dict | ||
|
||
name = "John Doe" | ||
|
||
|
||
def test_fixture_name_as_expected(json_payload): | ||
"""Test that the json_payload fixture is registered from the JSONPayloadFactory.""" | ||
assert json_payload["name"] == "John Doe" | ||
|
||
|
||
def test_fixture_name_cant_be_determined(testdir): | ||
"""Test that an error is raised if the fixture name can't be determined.""" | ||
testdir.makepyfile( | ||
""" | ||
import factory | ||
from pytest_factoryboy import register | ||
@register | ||
class JSONPayloadF(factory.Factory): | ||
class Meta: | ||
model = dict | ||
name = "John Doe" | ||
""" | ||
) | ||
res = testdir.runpytest() | ||
assert_outcomes(res, errors=1) | ||
res.stdout.fnmatch_lines("*JSONPayloadF *does not follow*naming convention*") | ||
|
||
|
||
def test_invalid_factory_name_override(testdir): | ||
"""Test that, although the factory name doesn't follow the naming convention, it can still be overridden.""" | ||
testdir.makepyfile( | ||
""" | ||
import factory | ||
from pytest_factoryboy import register | ||
@register(_name="payload") | ||
class JSONPayloadF(factory.Factory): | ||
class Meta: | ||
model = dict | ||
name = "John Doe" | ||
def test_payload(payload): | ||
assert payload["name"] == "John Doe" | ||
""" | ||
) | ||
res = testdir.runpytest() | ||
assert_outcomes(res, passed=1) |