PyCharm 2024.1 Help

Enable support for behave-django

Enable the Django plugin

This functionality relies on the Django plugin, which is bundled and enabled in PyCharm by default. If the relevant features aren't available, make sure that you didn't disable the plugin.

  1. Press Ctrl+Alt+S to open settings and then select Plugins.

  2. Open the Installed tab, find the Django plugin, and select the checkbox next to the plugin name.

With PyCharm you can benefit from the behavior-driven development (BDD) with Django by enabling behave-django integration in your Django applications.

To enable behave-django integration:

  1. In your Django project, install the following Python packages:

    • behave

    • behave-django

  2. Modify the structure of your project to meet the behave structural requirements. In your Django application directory, create the features directory for the test scenario files and the steps subdirectory for the Python implementations of the scenarios. Create a new Gherkin feature file, features/auth.feature, and a new Python step file, features/steps/

    a Django project structure required for behave-django integration
  3. Now, add your test scenarios to the auth.feature by using Gherkin feature testing language:

    Feature: auth Scenario: Unauthenticated user can't access the page Given I am not authenticated When I access the page Then Status code is 302 Scenario: Authenticated user can access the page Given I am authenticated When I access the page Then Status code is 200
  4. Implement the steps used in the auth.feature scenarios in the features/steps/ Python file:

    from behave import * from django.contrib.auth.models import User use_step_matcher("re") @given("I am not authenticated") def step_impl(context): pass @when("I access the page") def step_impl(context): context.response = context.test.client.get("/") @then("Status code is (?P<status>\d+)") def step_impl(context, status): code = context.response.status_code assert code == int(status), "{0} != {1}".format(code, status) @given("I am authenticated") def step_impl(context): user = User.objects.create_superuser("jane", "", "123") context.test.client.force_login(user)
  5. Modify the file and the files to add code required for our testing sample:

    from django.contrib.auth.decorators import login_required from django.http import HttpResponse # Create your views here. @login_required def show_user(request): return HttpResponse("OK")

    from django.urls import path from Django_Behave import views urlpatterns = [ path('', views.show_user), ]
  6. Now, implement the key tweak: add 'behave_django' to the INSTALLED_APPS section of the

    enabling behave-django integration in the file

    With this done, your application is ready for a test session. All you need is to run test configurations for your scenarios.

  7. Open the auth.feature file in the Editor window. Click Gutter icon to run a behave scenario in the gutter next to any of the Scenario statements and choose Run <scenario name>. The Run tool window opens with test run results.

    Test running for a behave configuration

    From the test report, you can learn that the test was run by You can also see how many steps of the scenario have been passed, failed, or skipped.

    By default, successful steps are not shown. Click Show passed tests on the toolbar to display them.

Last modified: 28 March 2024