PyCharm 2021.2 Help

Enable support for behave-django

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/Auth.py.

    a Django project structure required for behave-django ntegration
  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/Auth.py 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", "jane@example.org", "123") context.test.client.force_login(user)
  5. Modify the views.py file and the urls.py files to add code required for our testing sample:

    views.py

    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")

    urls.py

    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 settings.py.

    enabling behave-django integration in the settings.py 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. Position the cursor on a line with any of the Scenario statements, then right-click it, and choose Run <scenario name>. Preview the test run results.

    Test running for a behave configuration

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

Last modified: 27 August 2021