Oct 28, 2011

Testing applications and extensions

Python Testing Tools Taxonomy
Django Packages: Testing Tools

Open Source Testing
Link Checker

django-testtools - A helper for writting Django's tests.
  • assertRecipients
  • assertQuerySetEqual
  • assertErrorsInForm
django-test-extensions - A set of custom assertions and examples for use testing django applications.
  • login_as_admin
  • ...
  • assert_file_exists
  • assert_key_exists
  • assert_has_attr
  • ...
  • assert_mail
  • assert_latest
  • assert_model_changes
django-autofixture - Can create auto-generated test data
from autofixture import AutoFixture
fixture = AutoFixture(Entry)
entries = fixture.create(10)
django-test-utils
  • Django Testmaker 
  • Django Crawler 
  • Django Test Runner 
  • Twill Runner 
  • Persistent Database Test Runner
+
./manage.py makefixture proposals.Proposal[:10] --indent=4 > proposal_with_related_items.json
factory_boy - A test fixtures replacement for Python based on thoughtbot's factory_girl for Ruby
import factory
from models import User

class UserFactory(factory.Factory):
    FACTORY_FOR = User

    first_name = 'John'
    last_name = 'Doe'
    admin = False

# Returns a User instance that's not saved
user = UserFactory.build()

# Returns a saved User instance
user = UserFactory.create()

# Returns a dict of attributes that can be used to build a User instance
attributes = UserFactory.attributes()

# Returns an object with all defined attributes stubbed out:
stub = UserFactory.stub()
rebar - Rebar makes your Forms stronger
from rebar.testing import flatten_to_dict

form_data = flatten_to_dict(ContactForm())
form_data.update({
        'name': 'X' * 300,
    })
form = ContactForm(data=form_data)
assert(not form.is_valid())
The same for formsets
from rebar.testing import flatten_to_dict, empty_form_data

formset = ContactFormSet()
form_data = flatten_to_dict(formset)
form_data.update(
    empty_form_data(formset, len(formset))
)

No comments: