Behave-Django
Behave-Django integrates the Behave BDD (Behavior-Driven Development) framework with Django projects, allowing developers to write human-readable feature files and step definitions to test web applications. As of its current version 1.9.0, it supports modern Python and Django versions, with a regular release cadence addressing compatibility and feature enhancements.
Common errors
-
django.db.utils.IntegrityError: (1062, "Duplicate entry '...' for key '...'')
cause Fixtures are not being properly reset between scenarios, leading to duplicate key errors when re-inserting data.fixEnsure your `environment.py` includes proper `after_scenario` or `after_step` hooks for database cleanup, or upgrade to a version (1.9.0+) that fixes known fixture reset issues, especially with Django 5.2+. -
ModuleNotFoundError: No module named 'behave_django.testcase'
cause Incorrect import path for the Behave-Django test case classes.fixChange `from behave_django.testcase import ...` to `from behave_django.test_case import ...`. -
behave-django.exceptions.ImproperlyConfigured: Django settings are not configured. Call settings.configure() or set DJANGO_SETTINGS_MODULE in your environment.
cause Behave-Django couldn't find your Django project settings.fixMake sure the `DJANGO_SETTINGS_MODULE` environment variable is set to your project's settings file (e.g., `export DJANGO_SETTINGS_MODULE=myproject.settings`) before running `behave`.
Warnings
- breaking The `--runner-class` option was renamed to `--runner` in behave-django 1.5.0, and it now requires `parent.module:class` syntax.
- breaking Python 3.6, 3.7, Django 3.3, and 4.1 support was dropped in behave-django 1.5.0. Python 3.8 support was dropped in 1.6.0. Python 3.9 is now the minimum required. Django 5.2+ requires specific fixes in 1.6.0 and 1.9.0.
- gotcha Automatic fixture resetting is planned for v2.0.0. In current versions, if `fixtures` are set to an empty list, they might not reset correctly, especially on Django 5.2+. This was fixed in 1.9.0 for the empty list case.
- gotcha Older versions of behave-django (before 1.8.0) experienced issues with database transaction rollback due to incompatibilities with Behave 1.2.7.dev8 and later.
Install
-
pip install behave-django
Imports
- DjangoBehaveTestCase
from behave_django.testcase import DjangoBehaveTestCase
from behave_django.test_case import DjangoBehaveTestCase
- before_all
from behave_django.hooks import before_all
from behave_django.environment import before_all
Quickstart
from django.test import TestCase
class MyFeatureTests(TestCase):
def test_example_scenario(self):
# Your Django-specific test logic here
self.assertEqual(1 + 1, 2)
# features/example.feature
# Feature: Example Feature
# Scenario: Basic addition
# Given I have the number 1
# And I add the number 1
# Then the result should be 2
# features/steps/example_steps.py
# from behave import given, when, then
# from django.test import TestCase
# from behave_django.decorators import fixtures
# @fixtures(['my_fixture.json'])
# @given('I have the number {number:d}')
# def step_impl(context, number):
# context.a = number
# @when('I add the number {number:d}')
# def step_impl(context, number):
# context.result = context.a + number
# @then('the result should be {expected_result:d}')
# def step_impl(context, expected_result):
# assert context.result == expected_result