Waelsteng
[wel stæŋ] / noun
literal: “death pole” or “slaugher pole”;
Anglo Saxon tool for impailing enemies or those who had done wrong;
A collection of django testing tools
Testing Django Views
- class awl.waelsteng.FakeRequest(user=None, method='GET', cookies={}, data={})
Simulates a request object
- __init__(user=None, method='GET', cookies={}, data={})
Constructor
- Parameters:
user – Django User object to include in the request. Defaults to None. If none is given then the parameter is not set at all
method – Request method. Defaults to ‘GET’
cookies – Dict containing cookies for the request. Defaults to empty
data – Dict for get or post fields. Defaults to empty
Testing Admin Classes
- class awl.waelsteng.AdminToolsMixin
This mixin is used to help test django admin objects using the django client interface. A superuser is created during setup which can then be used throughout.
Note
AdminToolsMixin.initiate
must be called in the inheritor’sTestCase.setUp
method to properly initialize.Once
AdminToolsMixin.intiate
is called, the following will be available:- Parameters:
site – An instance of an
AdminSite
to test againstadmin_user – A User object with staff and superuser privileges
- EMAIL = 'admin@admin.com'
Admin account’s email during the tests
- PASSWORD = 'admin'
Admin account’s password during the tests
- USERNAME = 'admin'
Admin account’s username during the tests
- authed_get(url, response_code=200, headers={}, follow=False)
Does a django test client
get
against the given url after logging in the admin first.- Parameters:
url – URL to fetch
response_code – Expected response code from the URL fetch. This value is asserted. Defaults to 200
headers – Optional dictionary of headers to send in the request
follow – When True, the get call will follow any redirect requests. Defaults to False.
- Returns:
Django testing
Response
object
- authed_post(url, data, response_code=200, follow=False, headers={})
Does a django test client
post
against the given url after logging in the admin first.- Parameters:
url – URL to fetch
data – Dictionary to form contents to post
response_code – Expected response code from the URL fetch. This value is asserted. Defaults to 200
headers – Optional dictionary of headers to send in with the request
- Returns:
Django testing
Response
object
- authorize()
Authenticates the superuser account via the web login.
- field_names(admin_model)
Returns the names of the fields/columns used by the given admin model.
- Parameters:
admin_model – Instance of a
admin.ModelAdmin
object that is responsible for displaying the change list- Returns:
List of field names
- field_value(admin_model, instance, field_name)
Returns the value displayed in the column on the web interface for a given instance.
- Parameters:
admin_model – Instance of a
admin.ModelAdmin
object that is responsible for displaying the change listinstance – Object instance that is the row in the admin change list
- Field_name:
Name of the field/column to fetch
- initiate()
Sets up the
AdminSite
and creates a user with the appropriate privileges. This should be called from the inheritor’sTestCase.setUp
method.
- visit_admin_link(admin_model, instance, field_name, response_code=200, headers={})
This method is used for testing links that are in the change list view of the django admin. For the given instance and field name, the HTML link tags in the column are parsed for a URL and then invoked with
AdminToolsMixin.authed_get
.- Parameters:
admin_model – Instance of a
admin.ModelAdmin
object that is responsible for displaying the change listinstance – Object instance that is the row in the admin change list
field_name – Name of the field/column to containing the HTML link to get a URL from to visit
response_code – Expected HTTP status code resulting from the call. The value of this is asserted. Defaults to 200.
headers – Optional dictionary of headers to send in the request
- Returns:
Django test
Response
object- Raises:
AttributeError – If the column does not contain a URL that can be parsed
- awl.waelsteng.create_admin(username='admin', email='admin@admin.com', password='admin')
Create and save an admin user.
- Parameters:
username – Admin account’s username. Defaults to ‘admin’
email – Admin account’s email address. Defaults to ‘admin@admin.com’
password – Admin account’s password. Defaults to ‘admin’
- Returns:
Django user with staff and superuser privileges
WRunner
This is an alternate to DiscoverRunner
. Main differences are:
Creates and destroys a media directory for uploads during testing
Calls a dynamic method for loading test data
Allows for short-form versions of test labels
Sets static file handling to
StaticFilesStorage
Like any other runner, you can use it for your django tests by changing your settings:
TEST_RUNNER = 'awl.waelsteng.WRunner'
Configuration is done inside of the django settings module:
WRUNNER = {
'CREATE_TEMP_MEDIA_ROOT':True,
'TEST_DATA':'package.module.function_name'
}
If CREATE_TEMP_MEDIA_ROOT
is set, then a directory is created and the
settings.MEDIA_ROOT
attribute is changed for the tests. The directory is
then destoryed upon test completion.
If TEST_DATA
is set, then the named function is loaded and run after the
database is created. This hook can be used to create test data.
Shortcut test labels (using waelstow.find_shortcut_tests()) are supported by this runner. Any label that starts with “=” is checked against all tests for substring matches.