Skip to content

Commit

Permalink
Allow to pass arguments to pytest via runner-options argument
Browse files Browse the repository at this point in the history
  • Loading branch information
yakky committed May 16, 2020
1 parent c7ac8bb commit 082edb8
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 38 deletions.
9 changes: 6 additions & 3 deletions app_helper/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,13 @@ def _test_run_worker(test_labels, test_runner, failfast=False, runner_options=No
settings.TEST_RUNNER = test_runner
TestRunner = get_runner(settings) # NOQA

kwargs = {"verbosity": verbose, "interactive": False, "failfast": failfast}
if runner_options:
sys.argv.extend(runner_options.split(","))
test_runner = TestRunner(verbosity=verbose, interactive=False, failfast=failfast)
if "PytestTestRunner" in test_runner:
kwargs["pytest_args"] = runner_options
else:
sys.argv.extend(runner_options.split(","))
test_runner = TestRunner(**kwargs)
failures = test_runner.run_tests(test_labels)
return failures

Expand Down Expand Up @@ -343,7 +347,6 @@ def core(args, application):
)
]
_make_settings(args, application, settings, STATIC_ROOT, MEDIA_ROOT)
print("OPTIONS", options)
execute_from_command_line(options)

else:
Expand Down
3 changes: 3 additions & 0 deletions app_helper/pytest_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def __init__(self, verbosity=1, failfast=False, keepdb=False, **kwargs):
self.verbosity = verbosity
self.failfast = failfast
self.keepdb = keepdb
self.extra_args = kwargs.pop("pytest_args", "")

def run_tests(self, test_labels):
"""Run pytest and return the exitcode.
Expand All @@ -18,6 +19,8 @@ def run_tests(self, test_labels):
import pytest

argv = shlex.split(os.environ.get("PYTEST_ARGS", ""))
if self.extra_args:
argv.extend(shlex.split(self.extra_args))
if self.verbosity == 0: # pragma: no cover
argv.append("--quiet")
if self.verbosity == 2: # pragma: no cover
Expand Down
20 changes: 1 addition & 19 deletions app_helper/test_utils/runners.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,9 @@
import sys
import unittest # noqa

from django.test.runner import DiscoverRunner

try:
from django.utils import unittest
except ImportError:
import unittest


class CapturedOutputRunner(DiscoverRunner):
def run_suite(self, suite, **kwargs):
return unittest.TextTestRunner(verbosity=self.verbosity, failfast=self.failfast, stream=sys.stderr).run(suite)


try:
from django.test.simple import DjangoTestSuiteRunner

class CapturedOutputSimpleRunner(DjangoTestSuiteRunner):
def run_suite(self, suite, **kwargs):
return unittest.TextTestRunner(verbosity=self.verbosity, failfast=self.failfast, stream=sys.stderr).run(
suite,
)


except ImportError:
CapturedOutputSimpleRunner = CapturedOutputRunner
27 changes: 19 additions & 8 deletions app_helper/tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,7 @@ def test_testrun_nocms(self):
args["test"] = True
args["--cms"] = False
args["--runner"] = "runners.CapturedOutputRunner"
args["--runner-options"] = "--capture"
core(args, self.application)
self.assertTrue("Ran 14 tests in" in err.getvalue())
self.assertEqual(exit_state.exception.code, 0)
Expand Down Expand Up @@ -810,11 +811,15 @@ def test_testrun_native(self):
except (ImportError, AttributeError):
pass
args = copy(DEFAULT_ARGS)
args["<command>"] = "test"
args["<command>"] = None
args["test"] = True
args["--cms"] = False
args["--native"] = True
args["--extra-settings"] = "cms_helper_extra_runner.py"
core(args, self.application)
try:
core(args, self.application)
except SystemExit:
pass
self.assertTrue("Ran 14 tests in" in err.getvalue())

def test_testrun_pytest(self):
Expand All @@ -831,15 +836,20 @@ def test_testrun_pytest(self):
except (ImportError, AttributeError):
pass
args = copy(DEFAULT_ARGS)
args["<command>"] = "test"
args["<command>"] = None
args["test"] = True
args["--cms"] = False
args["--runner"] = "app_helper.pytest_runner.PytestTestRunner"
args["--extra-settings"] = "helper_no_cms.py"
args["--runner-options"] = "'-k test_create_django_image_object'"
args["options"] = ["helper", "test", "--failfast", "--verbosity=2"]
core(args, self.application)
self.assertTrue("collected 15 items" in out.getvalue())
try:
core(args, self.application)
except SystemExit:
pass
self.assertTrue("collected 15 items / 14 deselected / 1 selected" in out.getvalue())
# warnings will depend on django version and adds too much noise
self.assertTrue("10 passed, 5 skipped" in out.getvalue())
self.assertTrue("1 passed, 14 deselected" in out.getvalue())

def test_runner_pytest(self):
with work_in(self.basedir):
Expand All @@ -850,11 +860,12 @@ def test_runner_pytest(self):
args.append("example1")
args.append("test")
args.append("--extra-settings=helper_no_cms.py")
args.append("--runner-options='-k test_create_django_image_object'")
args.append("--runner=app_helper.pytest_runner.PytestTestRunner")
runner.run("example1", args)
self.assertTrue("collected 15 items" in out.getvalue())
self.assertTrue("collected 15 items / 14 deselected / 1 selected" in out.getvalue())
# warnings will depend on django version and adds too much noise
self.assertTrue("10 passed, 5 skipped" in out.getvalue())
self.assertTrue("1 passed, 14 deselected" in out.getvalue())
self.assertEqual(exit_state.exception.code, 0)

def test_authors(self):
Expand Down
1 change: 1 addition & 0 deletions changes/159.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
git a
26 changes: 18 additions & 8 deletions docs/pytest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ To enable pytest compatible runner:

* Add to project ``helper.py`` file:

.. code-block:: python
.. code-block:: python
HELPER_SETTINGS = {
...
"TEST_RUNNER": "app_helper.pytest_runner.PytestTestRunner",
...
}
HELPER_SETTINGS = {
...
"TEST_RUNNER": "app_helper.pytest_runner.PytestTestRunner",
...
}
* Run tests as usual::

Expand All @@ -36,8 +36,18 @@ The runner support translates the following Django test runner options to pytest
* ``failfast``: ``--exitfirst``
* ``keepdb``: ``--reuse-db``

All the other pytest and pytest plugins are supported via ``PYTEST_ARGS`` enviroment variable::
All the other pytest and pytest plugins are supported either via ``PYTEST_ARGS`` enviroment variable or
``--runner-options`` cmdline argument.

PYTEST_ARGS='-s -k my_test' python helper.py
Environment variable example::

PYTEST_ARGS='-s -k my_test' python helper.py test

argument variable example::

python helper.py test --runner-options="-k my_test"

In case arguments are passed via both channels they are merged together, with runner-options arguments having priority
over environment variables in case of overlapping options.

.. _pytest-django: https://pytest-django.readthedocs.io/en/latest/faq.html#how-can-i-use-manage-py-test-with-pytest-django

0 comments on commit 082edb8

Please sign in to comment.