Skip to content
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

Adds ability to override the django runner class #130

Merged
merged 15 commits into from
Jan 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Release History
**Features and Improvements**

- Cover Python 3.9 and 3.10 and Django 3.2, drop Python 3.5 and Django 3.0 support
- Bump Behave requirement to 1.2.7 (allows option to change the Behave TestRunner)
- New option to change the Django TestRunner

1.4.0 (2020-06-15)
++++++++++++++++++
Expand Down
32 changes: 26 additions & 6 deletions behave_django/management/commands/behave.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from behave.__main__ import main as behave_main
from behave.configuration import options as behave_options
from behave.configuration import valid_python_module
from django.core.management.base import BaseCommand

from behave_django.environment import monkey_patch_behave
Expand Down Expand Up @@ -54,6 +55,14 @@ def add_command_arguments(parser):
help="Use simple test runner that supports Django's"
" testing client only (no web browser automation)"
)
parser.add_argument(
'--runner-class',
action='store',
type=valid_python_module,
default='behave_django.runner.BehaviorDrivenTestRunner',
help=('Full Python dotted path to a package, module, Django '
'TestRunner. Defaults to "%(default)s)".')
)


def add_behave_arguments(parser): # noqa
Expand All @@ -70,6 +79,7 @@ def add_behave_arguments(parser): # noqa
'-v',
'-S',
'--simple',
'--runner-class',
]

parser.add_argument(
Expand Down Expand Up @@ -120,13 +130,23 @@ def add_arguments(self, parser):

def handle(self, *args, **options):

django_runner_class = options['runner_class']
is_default_runner = django_runner_class is BehaviorDrivenTestRunner

# Check the flags
if options['use_existing_database'] and options['simple']:
self.stderr.write(self.style.WARNING(
'--simple flag has no effect'
' together with --use-existing-database'
))

active_flags = options['use_existing_database'] or options['simple']
if not is_default_runner and active_flags:
self.stderr.write(self.style.WARNING(
'--use-existing-database or --simple has no effect'
' together with --runner-class'
))

# Configure django environment
passthru_args = ('failfast',
'interactive',
Expand All @@ -136,13 +156,13 @@ def handle(self, *args, **options):
k, v in
options.items() if k in passthru_args and v is not None}

if options['dry_run'] or options['use_existing_database']:
django_test_runner = ExistingDatabaseTestRunner(**runner_args)
elif options['simple']:
django_test_runner = SimpleTestRunner(**runner_args)
else:
django_test_runner = BehaviorDrivenTestRunner(**runner_args)
if is_default_runner:
if options['dry_run'] or options['use_existing_database']:
django_runner_class = ExistingDatabaseTestRunner
elif options['simple']:
django_runner_class = SimpleTestRunner

django_test_runner = django_runner_class(**runner_args)
django_test_runner.setup_test_environment()

old_config = django_test_runner.setup_databases()
Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
# All configuration values have a default; values that are commented out
# serve to show the default.

import sys
import os
import shlex
import sys
bittner marked this conversation as resolved.
Show resolved Hide resolved

sys.path.insert(0, os.path.abspath('..'))

Expand Down
25 changes: 17 additions & 8 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,6 @@ management command.

Additional command line options provided by *behave-django*:

``--use-existing-database``
***************************

Don't create a test database, and use the database of your default runserver
instead. USE AT YOUR OWN RISK! Only use this option for testing against a
*copy* of your production database or other valuable data. Your tests may
destroy your data irrecoverably.

``--keepdb``
************

Expand All @@ -30,6 +22,15 @@ recreating it each time you run the test. This flag enables
``manage.py behave --keepdb`` to take advantage of that feature.
|keepdb docs|_.

``--runner-class``
******************

Full Python dotted path to a package, module, Django TestRunner.

Not to be confused with `--behave-runner-class` that handles the internal
`TestRunner` inside `behave`. You can read more about it in the
`behave docs <https://behave.readthedocs.io/en/latest/behave.html#cmdoption-runner-class>`__.

``--simple``
************

Expand All @@ -38,6 +39,14 @@ after each scenario instead of flushing the entire database. Tests run much
quicker, however HTTP server is not started and therefore web browser
automation is not available.

``--use-existing-database``
***************************

Don't create a test database, and use the database of your default runserver
instead. USE AT YOUR OWN RISK! Only use this option for testing against a
*copy* of your production database or other valuable data. Your tests may
destroy your data irrecoverably.

Behave configuration file
-------------------------

Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
beautifulsoup4
behave
behave@git+http://github.com/behave/behave.git@v1.2.7.dev2#egg=behave # behave>=1.2.7
django>=2.2
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""
from os import chdir
from os.path import abspath, dirname, normpath

kingbuzzman marked this conversation as resolved.
Show resolved Hide resolved
from setuptools import find_packages, setup

# allow setup.py to be run from any path
Expand Down
83 changes: 77 additions & 6 deletions tests/unit/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import os
from imp import reload

import pytest

from .util import DjangoSetupMixin, run_silently, show_run_error


Expand All @@ -15,21 +17,28 @@ def test_additional_management_command_options(self):
os.linesep) in output
assert (os.linesep + ' -k, --keepdb') in output
assert (os.linesep + ' -S, --simple') in output
assert (os.linesep + ' --runner-class') in output
assert (os.linesep + ' --noinput, --no-input') in output
assert (os.linesep + ' --failfast') in output
assert (os.linesep + ' -r, --reverse') in output

def test_should_accept_behave_arguments(self):
from behave_django.management.commands.behave import Command
command = Command()
args = command.get_behave_args(
argv=['manage.py', 'behave',
'--format', 'progress',
'--settings', 'test_project.settings',
'-i', 'some-pattern',
'features/running-tests.feature'])
argv = [
'manage.py', 'behave',
'--format', 'progress',
'--behave-runner-class', 'behave.runner.Runner',
'--runner-class', 'behave_django.runner.BehaviorDrivenTestRunner',
'--settings', 'test_project.settings',
'-i', 'some-pattern',
'features/running-tests.feature'
]
args = command.get_behave_args(argv=argv)

assert '--format' in args
assert '--runner-class' in args
assert args[args.index('--runner-class') + 1] == 'behave.runner.Runner'
assert 'progress' in args
assert '-i' in args
assert 'some-pattern' in args
Expand Down Expand Up @@ -107,3 +116,65 @@ def test_simple_and_use_existing_database_flags_raise_a_warning(self):
'--simple flag has no effect together with '
'--use-existing-database' +
os.linesep) in output

@pytest.mark.parametrize('arguments, expect_error', [
(
'--runner-class behave_django.runner.BehaviorDrivenTestRunner',
False
),
(
(
'--runner-class behave_django.runner.SimpleTestRunner '
'--simple'
),
True
),
(
(
'--runner-class behave_django.runner.BehaviorDrivenTestRunner '
'--simple'
),
False
),
(
(
'--behave-runner-class behave.runner.Runner '
'--runner-class behave_django.runner.SimpleTestRunner '
'--simple'
),
True
),
(
(
'--runner-class behave_django.runner.SimpleTestRunner '
'--use-existing-database'
),
True
),
('--behave-runner-class behave.runner.Runner --simple', False),
(
(
'--behave-runner-class behave.runner.Runner '
'--runner-class behave_django.runner.BehaviorDrivenTestRunner'
),
False
),
])
def test_runner_class_and_others_flags_raise_a_warning(self, arguments,
expect_error):
exit_status, output = run_silently(
'python tests/manage.py behave'
' %s --tags=@skip-all' % arguments
)
assert exit_status == 0, \
show_run_error(exit_status, output)

warning_message = (
os.linesep +
'--use-existing-database or --simple has no effect'
' together with --runner-class' +
os.linesep)
if expect_error:
assert warning_message in output
else:
assert warning_message not in output
2 changes: 1 addition & 1 deletion tests/unit/test_passthru_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@


@mock.patch('behave_django.management.commands.behave.behave_main', return_value=0) # noqa
@mock.patch('behave_django.management.commands.behave.BehaviorDrivenTestRunner') # noqa
@mock.patch('behave_django.runner.BehaviorDrivenTestRunner')
class TestPassThruArgs(DjangoSetupMixin):

def test_keepdb_flag(self,
Expand Down