-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
89480d3
commit 0ffb929
Showing
2 changed files
with
122 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,15 +97,132 @@ For a more comprehensive tutorial, kindly look at [behave tutorial](https://beha | |
|
||
- There are two projects that integrate django and behave: | ||
- [django-behave](https://github.com/django-behave/django-behave/blob/master/README.md#how-to-use) | ||
- [behave-django](https://behave-django.readthedocs.io/en/latest/installation.html) | ||
- [behave_django](https://behave-django.readthedocs.io/en/latest/installation.html) | ||
|
||
### Behave-django | ||
### `behave_django` | ||
|
||
#### Setting up Behave-django | ||
#### Setting up `behave_django` | ||
|
||
For the tutorial clone: | ||
To install: `pip install behave_django` | ||
S | ||
Add `behave_django` as an installed application in `core.settings.dev` module. | ||
|
||
Under `django_project` directory: | ||
|
||
- Create `tests` directory and `behave.ini` file | ||
```bash | ||
mkdir tests | ||
|
||
touch behave.ini | ||
``` | ||
- In the `behave.ini` file, add the following: | ||
```ini | ||
[behave] | ||
paths = tests/acceptance | ||
junit_directory = tests/reports | ||
junit = yes | ||
``` | ||
|
||
- Under `tests`, create `acceptance` directory: | ||
```bash | ||
cd tests | ||
|
||
mkdir acceptance | ||
``` | ||
|
||
- Under `acceptance` create two directories: `features` and `steps` | ||
```bash | ||
mkdir features steps | ||
``` | ||
- In the `acceptance` directory create an `environment.py` file | ||
```python | ||
""" | ||
behave environment module | ||
""" | ||
# Customise according to project | ||
|
||
def before_feature(context, feature): | ||
if feature.name == 'Fixture loading': | ||
context.fixtures = ['behave-fixtures.json'] | ||
|
||
elif feature.name == 'Fixture loading with decorator': | ||
# Including empty fixture to test that #92 is fixed | ||
context.fixtures = ['empty-fixture.json'] | ||
|
||
|
||
def before_scenario(context, scenario): | ||
if scenario.name == 'Load fixtures for this scenario and feature': | ||
context.fixtures.append('behave-second-fixture.json') | ||
|
||
if scenario.name == 'Load fixtures then reset sequences': | ||
context.fixtures.append('behave-second-fixture.json') | ||
context.reset_sequences = True | ||
|
||
if scenario.name == 'Load fixtures with databases option': | ||
context.databases = '__all__' | ||
|
||
|
||
def django_ready(context): | ||
context.django = True | ||
``` | ||
#### Creating tests | ||
|
||
#### Creating bdd tests | ||
|
||
Add `*.feature ` file in features directory. | ||
Feature files use the Gherkin syntax. | ||
|
||
```Gherkin | ||
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 | ||
``` | ||
|
||
Add `*.py` file in steps directory. | ||
|
||
```python | ||
from behave import given, when, then, use_step_matcher | ||
from django.contrib.auth.models import User | ||
|
||
use_step_matcher("re") | ||
|
||
|
||
@given("I am not authenticated") | ||
def not_auth(context): | ||
pass | ||
|
||
|
||
@when("I access the page") | ||
def access_page(context): | ||
context.response = context.test.client.get("/map/") | ||
|
||
|
||
@then("Status code is (?P<status>\d+)") | ||
def status_code(context, status): | ||
code = context.response.status_code | ||
assert code == int(status), "{0} != {1}".format(code, status) | ||
|
||
|
||
@given("I am authenticated") | ||
def auth_success(context): | ||
user = User.objects.create_superuser("admin", "[email protected]", "admin") | ||
context.test.client.force_login(user) | ||
``` | ||
|
||
#### Running tests | ||
|
||
First collect static: `python manage.py collectstatic` | ||
|
||
To run: `python manage.py behave` | ||
|
||
Results: | ||
|
||
![bdd-results](./img/testing-bdd-behave-3.png) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.