{"id":7977,"library":"behave-django","title":"Behave-Django","description":"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.","status":"active","version":"1.9.0","language":"en","source_language":"en","source_url":"https://github.com/behave/behave-django","tags":["django","behave","bdd","testing","gherkin","automation"],"install":[{"cmd":"pip install behave-django","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Core BDD framework integration; requires behave 1.3.3+ as of behave-django 1.8.0.","package":"behave"},{"reason":"The web framework being integrated with Behave; requires Django >=3.2.","package":"Django"}],"imports":[{"note":"The 'test_case' module uses a snake_case name, not 'testcase'.","wrong":"from behave_django.testcase import DjangoBehaveTestCase","symbol":"DjangoBehaveTestCase","correct":"from behave_django.test_case import DjangoBehaveTestCase"},{"note":"Behave-Django hooks are typically defined directly in your project's 'environment.py' and imported via `behave_django.environment` if needed for custom setup.","wrong":"from behave_django.hooks import before_all","symbol":"before_all","correct":"from behave_django.environment import before_all"}],"quickstart":{"code":"from django.test import TestCase\n\nclass MyFeatureTests(TestCase):\n    def test_example_scenario(self):\n        # Your Django-specific test logic here\n        self.assertEqual(1 + 1, 2)\n\n# features/example.feature\n# Feature: Example Feature\n#   Scenario: Basic addition\n#     Given I have the number 1\n#     And I add the number 1\n#     Then the result should be 2\n\n# features/steps/example_steps.py\n# from behave import given, when, then\n# from django.test import TestCase\n# from behave_django.decorators import fixtures\n\n# @fixtures(['my_fixture.json'])\n# @given('I have the number {number:d}')\n# def step_impl(context, number):\n#     context.a = number\n\n# @when('I add the number {number:d}')\n# def step_impl(context, number):\n#     context.result = context.a + number\n\n# @then('the result should be {expected_result:d}')\n# def step_impl(context, expected_result):\n#     assert context.result == expected_result\n","lang":"python","description":"To get started, define your `features/` directory with `.feature` files and corresponding step definitions in `features/steps/`. Behave-Django automatically detects these. An `environment.py` file is typically used for setup/teardown hooks (e.g., `before_all`, `after_scenario`). Ensure your Django settings are configured correctly for testing. You can run `behave` from your Django project root."},"warnings":[{"fix":"Update command-line invocations. For example, change `--runner-class my_runner` to `--runner my_module:MyRunnerClass`.","message":"The `--runner-class` option was renamed to `--runner` in behave-django 1.5.0, and it now requires `parent.module:class` syntax.","severity":"breaking","affected_versions":">=1.5.0"},{"fix":"Upgrade your Python environment to 3.9+ and Django to a supported version (e.g., Django 4.2+, 5.x). Check the release notes for specific version requirements.","message":"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.","severity":"breaking","affected_versions":">=1.5.0"},{"fix":"For versions prior to 1.9.0, or to be safe before 2.0.0's automatic reset, manually manage fixture cleanup in your `after_scenario` hooks if you encounter issues with fixture state persistence.","message":"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.","severity":"gotcha","affected_versions":"<2.0.0 (especially Django 5.2+)"},{"fix":"Upgrade to behave-django 1.8.0 or newer to ensure proper database transaction handling and rollback. This version depends on behave 1.3.3+.","message":"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.","severity":"gotcha","affected_versions":"<1.8.0"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Ensure 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+.","cause":"Fixtures are not being properly reset between scenarios, leading to duplicate key errors when re-inserting data.","error":"django.db.utils.IntegrityError: (1062, \"Duplicate entry '...' for key '...'')"},{"fix":"Change `from behave_django.testcase import ...` to `from behave_django.test_case import ...`.","cause":"Incorrect import path for the Behave-Django test case classes.","error":"ModuleNotFoundError: No module named 'behave_django.testcase'"},{"fix":"Make 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`.","cause":"Behave-Django couldn't find your Django project settings.","error":"behave-django.exceptions.ImproperlyConfigured: Django settings are not configured. Call settings.configure() or set DJANGO_SETTINGS_MODULE in your environment."}]}