-
Notifications
You must be signed in to change notification settings - Fork 346
Integrate pytest into django's manage.py test #1182
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 17 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
00fcd4f
Create runner.py
kingbuzzman 9e962bb
Update faq.rst
kingbuzzman 88ebe7e
Update runner.py
kingbuzzman 2321ed6
Update faq.rst
kingbuzzman 32888cf
Create test_runner.py
kingbuzzman 251d207
Update test_environment.py
kingbuzzman 1fddf48
Update test_environment.py
kingbuzzman e59f369
Update test_environment.py
kingbuzzman add3866
.
kingbuzzman b41a4ed
.
kingbuzzman 938601d
.
kingbuzzman 4771035
.
kingbuzzman abda6a5
.
kingbuzzman 54a15e8
.
kingbuzzman e1e2116
Fix linter
kingbuzzman 3cb7dbc
Fix linter
kingbuzzman 9812df2
..
kingbuzzman e78f4aa
Update runner.py
kingbuzzman 5f8c01f
Update test_runner.py
kingbuzzman a0a6b84
Update test_runner.py
kingbuzzman db0fe7b
Apply suggestions from code review
kingbuzzman abda018
Update faq.rst
kingbuzzman 6af1225
Update test_runner.py
kingbuzzman 39d4927
Update test_environment.py
kingbuzzman a484e2d
Merge branch 'main' into patch-3
kingbuzzman 5089e39
Update runner.py
kingbuzzman c963ccb
Update test_runner.py
kingbuzzman 60ca811
testing the linter v2
kingbuzzman b7c4f1f
testing linter v3
kingbuzzman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,41 @@ | ||
from argparse import ArgumentParser | ||
from typing import Any, Iterable | ||
|
||
|
||
class PytestTestRunner: | ||
"""Runs pytest to discover and run tests.""" | ||
kingbuzzman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def __init__( | ||
self, verbosity: int = 1, failfast: bool = False, keepdb: bool = False, **kwargs: Any | ||
kingbuzzman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) -> None: | ||
self.verbosity = verbosity | ||
self.failfast = failfast | ||
self.keepdb = keepdb | ||
|
||
@classmethod | ||
def add_arguments(cls, parser: ArgumentParser) -> None: | ||
kingbuzzman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
parser.add_argument( | ||
"--keepdb", action="store_true", help="Preserves the test DB between runs." | ||
) | ||
|
||
def run_tests(self, test_labels: Iterable[str], **kwargs: Any) -> int: | ||
"""Run pytest and return the exitcode. | ||
|
||
It translates some of Django"s test command option to pytest"s. | ||
kingbuzzman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
import pytest | ||
|
||
argv = [] | ||
if self.verbosity == 0: | ||
argv.append("--quiet") | ||
elif self.verbosity == 2: | ||
argv.append("--verbose") | ||
elif self.verbosity == 3: | ||
argv.append("-vv") | ||
if self.failfast: | ||
argv.append("--exitfirst") | ||
if self.keepdb: | ||
argv.append("--reuse-db") | ||
|
||
argv.extend(test_labels) | ||
return pytest.main(argv) |
This file contains hidden or 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 hidden or 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 hidden or 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,28 @@ | ||
from unittest.mock import Mock, call | ||
|
||
import pytest | ||
|
||
from pytest_django.runner import PytestTestRunner | ||
|
||
|
||
@pytest.mark.parametrize( | ||
*( | ||
kingbuzzman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"kwargs, expected", | ||
[ | ||
({}, call(["tests"])), | ||
({"verbosity": 0}, call(["--quiet", "tests"])), | ||
({"verbosity": 1}, call(["tests"])), | ||
({"verbosity": 2}, call(["--verbose", "tests"])), | ||
({"verbosity": 3}, call(["-vv", "tests"])), | ||
({"verbosity": 4}, call(["tests"])), | ||
({"failfast": True}, call(["--exitfirst", "tests"])), | ||
({"keepdb": True}, call(["--reuse-db", "tests"])), | ||
], | ||
) | ||
) | ||
def test_runner_run_tests(monkeypatch, kwargs, expected): | ||
pytest_mock = Mock() | ||
monkeypatch.setattr("pytest.main", pytest_mock) | ||
runner = PytestTestRunner(**kwargs) | ||
runner.run_tests(["tests"]) | ||
assert pytest_mock.call_args == expected |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.