Skip to content

Commit

Permalink
Merge branch 'release-0.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
mixxorz committed Apr 2, 2015
2 parents 1d9552b + 2a5d0e2 commit 4219916
Show file tree
Hide file tree
Showing 35 changed files with 497 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ coverage.xml

# Django stuff:
*.log
*.sqlite3

# Sphinx documentation
docs/_build/
Expand Down
36 changes: 36 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Contributing

Wanna help out with behave-django? Yay! Here's a quick guide to do just that.

Fork, then clone the repo:

git clone [email protected]:your-username/behave-django.git

Install the dependencies

pip install -r requirements.txt

Make sure the tests pass:

python manage.py behave

Start your topic branch

git checkout -b your-topic-branch

Make your change. Add tests for your change. Make the tests pass:

python manage.py test

Your tests don't have to be behave tests. :smile:

Push to your fork and [submit a pull request][pr].

[pr]: https://github.com/mixxorz/behave-django/compare/

Other things to note:

* Write tests.
* We're using PEP8 as our code style guide

Thanks :)
68 changes: 67 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,68 @@
# behave-django
Behave test runner for Django
Behave BDD integration for Django

## Features
* Web Browser Automation ready
* Database transactions per scenario
* Use Django's testing client
* Use unittest + Django assert library

## Installation

Install using pip

pip install behave-django

Add `behave_django` to your `INSTALLED_APPS`

INSTALLED_APPS += ('behave_django',)

Create the features directory in your project's root directory.

features/
steps/
your_steps.py
environment.py
your-feature.feature

Setup your `environment.py` file

from behave_django import environment

def before_scenario(context, scenario):
environment.before_scenario(context, scenario)

def after_scenario(context, scenario):
environment.after_scenario(context, scenario)

Run `python manage.py behave`

Creating test database for alias 'default'...
Feature: Running tests # features/running-tests.feature:1
In order to prove that behave-django works
As the Maintainer
I want to test running behave against this features directory
Scenario: The Test # features/running-tests.feature:6
Given this step exists # features/steps/running_tests.py:4 0.000s
When I run "python manage.py behave" # features/steps/running_tests.py:9 0.000s
Then I should see the behave tests run # features/steps/running_tests.py:14 0.000s

1 features passed, 0 failed, 0 skipped
1 scenarios passed, 0 failed, 0 skipped
3 steps passed, 0 failed, 0 skipped, 0 undefined
Took.010s
Destroying test database for alias 'default'...

## TODO
* You should be able to set where you features directory will be.
* You should be able to pass regular behave command line args.
* You should see behave's unimplemented step hints.
* Support for Django 1.4.x, 1.6.x, 1.7.x, 1.8 on Python 2.6, 2.7, 3, 3.3, 3.4.

## Contributing
[Read the quick contributing guide](CONTRIBUTING.md)


## Changelog
### 0.1.0
* Initial release
1 change: 1 addition & 0 deletions behave_django/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Release 0.1.0
13 changes: 13 additions & 0 deletions behave_django/environment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from behave_django.testcase import BehaveDjangoTestCase

def before_scenario(context, scenario):
# This is probably a hacky method of setting up the test case
# outside of a test runner. Suggestions are welcome. :)
context.test = BehaveDjangoTestCase()
context.test.setUpClass()
context.test()
context.base_url = context.test.live_server_url

def after_scenario(context, scenario):
context.test.tearDownClass()
del context.test
Empty file.
Empty file.
34 changes: 34 additions & 0 deletions behave_django/management/commands/behave.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from __future__ import absolute_import
import os

from behave.configuration import Configuration
from behave.runner import Runner
from django.conf import settings
from django.core.management.base import BaseCommand, CommandError
from django.test.runner import DiscoverRunner

from behave_django.testcase import BehaveDjangoTestCase


class Command(BaseCommand):
args = '<app_name app_name ...>'
help = 'Runs behave tests'

def handle(self, *args, **options):
# Configure Behave
configuration = Configuration()
configuration.paths = [os.path.join(settings.BASE_DIR, 'features')]
configuration.format = ['pretty']

# Configure django environment
django_test_runner = DiscoverRunner()
django_test_runner.setup_test_environment()
old_config = django_test_runner.setup_databases()

# Run Behave tests
runner = Runner(configuration)
runner.run()

# Teardown django environment
django_test_runner.teardown_databases(old_config)
django_test_runner.teardown_test_environment()
6 changes: 6 additions & 0 deletions behave_django/templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<!doctype html>
<html>
<body>
Behave Django works
</body>
</html>
6 changes: 6 additions & 0 deletions behave_django/testcase.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.contrib.staticfiles.testing import StaticLiveServerTestCase

class BehaveDjangoTestCase(StaticLiveServerTestCase):

def runTest(*args, **kwargs):
pass
12 changes: 12 additions & 0 deletions features/database-transactions.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Feature: Database Transactions
In order to test if transactions are done per scenario
As the Maintainer
I want to save two database items

Scenario: Save the first item
When I save the object
Then I should only have one object

Scenario: Save the second item
When I save the object
Then I should only have one object
8 changes: 8 additions & 0 deletions features/django-test-client.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Feature: Django's test client
In order to ensure that the django's test client works in behave steps
As the Maintainer
I want to test if the client works

Scenario: Django's test client
When I use django's test client to visit "/"
Then it should return a successful response
12 changes: 12 additions & 0 deletions features/django-unittest-asserts.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Feature: Django + unittest asserts
In order to have cleaner assert calls
As the Maintainer
I want to use Django + unittest's built in assert library

Scenario: Testing the unittest assert library
When I use the unittest assert library
Then it should work properly

Scenario: Testing the django assert library
When I use the django assert library
Then it should work properly
8 changes: 8 additions & 0 deletions features/environment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from behave_django import environment


def before_scenario(context, scenario):
environment.before_scenario(context, scenario)

def after_scenario(context, scenario):
environment.after_scenario(context, scenario)
8 changes: 8 additions & 0 deletions features/live-test-server.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Feature: Live server
In order to prove that the live server works
As the Maintainer
I want to send an HTTP request

Scenario: HTTP GET
When I visit "/"
Then I should see "Behave Django works"
9 changes: 9 additions & 0 deletions features/running-tests.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Feature: Running tests
In order to prove that behave-django works
As the Maintainer
I want to test running behave against this features directory

Scenario: The Test
Given this step exists
When I run "python manage.py behave"
Then I should see the behave tests run
13 changes: 13 additions & 0 deletions features/steps/database_transactions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from behave import when, then

from test_app.models import BehaveTestModel


@when(u'I save the object')
def save_object(context):
BehaveTestModel.objects.create(name='Behave Works', number=123)


@then(u'I should only have one object')
def should_have_only_one_object(context):
assert 1 == BehaveTestModel.objects.count()
18 changes: 18 additions & 0 deletions features/steps/django-unittest-asserts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from behave import when, then


@when(u'I use the unittest assert library')
def use_assert_library(context):
# If one of them works, all of them work. ;)
context.test.assertEqual(1, 1)


@when(u'I use the django assert library')
def use_assert_library(context):
response = context.test.client.get('/')
context.test.assertTemplateUsed(response, 'index.html')


@then(u'it should work properly')
def asserts_should_work(context):
pass
10 changes: 10 additions & 0 deletions features/steps/django_test_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from behave import when, then

@when(u'I use django\'s test client to visit "{url}"')
def use_django_client(context, url):
context.response = context.test.client.get(url)


@then(u'it should return a successful response')
def it_should_be_successful(context):
assert context.response.status_code == 200
13 changes: 13 additions & 0 deletions features/steps/live_test_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import urllib2

from behave import when, then


@when(u'I visit "{url}"')
def visit(context, url):
context.response = urllib2.urlopen(context.base_url + url).read()


@then(u'I should see "{text}"')
def i_should_see(context, text):
assert text in context.response
16 changes: 16 additions & 0 deletions features/steps/running_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from behave import given, when, then


@given(u'this step exists')
def step_exists(context):
pass


@when(u'I run "python manage.py behave"')
def run_command(context):
pass


@then(u'I should see the behave tests run')
def is_running(context):
pass
10 changes: 10 additions & 0 deletions manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "test_project.settings")

from django.core.management import execute_from_command_line

execute_from_command_line(sys.argv)
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
behave
Django>=1.7
40 changes: 40 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import os
from setuptools import find_packages, setup

# allow setup.py to be run from any path
os.chdir(os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir)))

setup(
name='behave-django',
version='0.1.0',
packages=find_packages(),
include_package_data=True,
license='MIT License',
description='Behave BDD integration for Django',
url='https://github.com/mixxorz/behave-django',
author='Mitchel Cabuloy',
author_email='[email protected]',
install_requires=[
'behave',
'Django>=1.7'
],
classifiers=[
'Development Status :: 3 - Alpha',
'Environment :: Console',
'Environment :: Plugins',
'Environment :: Web Environment',
'Framework :: Django',
'Intended Audience :: Developers',
'License :: OSI Approved :: MIT License',
'Operating System :: OS Independent',
'Programming Language :: Python',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'Topic :: Internet :: WWW/HTTP',
'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
'Topic :: Software Development :: Testing',
],
)
Empty file added test_app/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions test_app/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
Loading

0 comments on commit 4219916

Please sign in to comment.