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 6 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 TestCase)
- New option to change the Django TestCase
bittner marked this conversation as resolved.
Show resolved Hide resolved

1.4.0 (2020-06-15)
++++++++++++++++++
Expand Down
21 changes: 20 additions & 1 deletion 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,
help=('Full Python dotted path to a package, module, Django '
'TestRunner. Defaults to '
'"behave_django.runner.BehaviorDrivenTestRunner".')
bittner marked this conversation as resolved.
Show resolved Hide resolved
)


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 @@ -127,6 +137,13 @@ def handle(self, *args, **options):
' together with --use-existing-database'
))

both_active = options['use_existing_database'] or options['simple']
if options['runner_class'] and both_active:
kingbuzzman marked this conversation as resolved.
Show resolved Hide resolved
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,7 +153,9 @@ 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']:
if options['runner_class']:
django_test_runner = options['runner_class'](**runner_args)
kingbuzzman marked this conversation as resolved.
Show resolved Hide resolved
elif options['dry_run'] or options['use_existing_database']:
django_test_runner = ExistingDatabaseTestRunner(**runner_args)
elif options['simple']:
django_test_runner = SimpleTestRunner(**runner_args)
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
8 changes: 8 additions & 0 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,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.

``--runner-class``
kingbuzzman marked this conversation as resolved.
Show resolved Hide resolved
******************

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`._
kingbuzzman marked this conversation as resolved.
Show resolved Hide resolved

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
68 changes: 62 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,50 @@ 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.BehaviorDrivenTestRunner '
'--simple'
),
True
),
(
(
'--runner-class behave_django.runner.BehaviorDrivenTestRunner '
'--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