Django Test Runner

Tutorial

Django Test Runner

The author selected the Open Internet/Free Speech Fund to receive a donation as part of the Write for DOnations program.

Django test client

Introduction

You must either define the environment variable DJANGOSETTINGSMODULE or call settings.configure before accessing settings. Process finished with exit code 1 The test runs fine on console using-./manage.py run test It looks like things are not setup before executing the tests? The best base class for most tests is django.test.TestCase. This test class creates a clean database before its tests are run, and runs every test function in its own transaction. The class also owns a test Client that you can use to simulate a. I'm attempting to set up a new django project, and I've configured TESTRUNNER in settings.py to be djangonose.NoseTestSuiteRunner. I chose this test runner because it seems to be the only one I can find that has the following features: writes xunit xml test report. Captures logging/stdout and only displays for failed tests.

It is nearly impossible to build websites that work perfectly the first time without errors. For that reason, you need to test your web application to find these errors and work on them proactively. In order to improve the efficiency of tests, it is common to break down testing into units that test specific functionalities of the web application. This practice is called unit testing. It makes it easier to detect errors because the tests focus on small parts (units) of your project independently from other parts.

Testing a website can be a complex task to undertake because it is made up of several layers of logic like handling HTTP requests, form validation, and rendering templates. However Django provides a set of tools that makes testing your web application seamless. In Django, the preferred way to write tests is to use the Python unittest module, although it is possible to use other testing frameworks.

In this tutorial, you will set up a test suite in your Django project and write unit tests for the models and views in your application. You will run these tests, analyze their results, and learn how to find the causes of failing tests.

Prerequisites

Before beginning this tutorial, you’ll need the following:

  • Django installed on your server with a programming environment set up. To do this, you can follow one of our How To Install the Django Web Framework and Set Up a Programming Environment tutorials.
  • A Django project created with models and views. In this tutorial, we have followed the project from our Django Development tutorial series.

Step 1 — Adding a Test Suite to Your Django Application

A test suite in Django is a collection of all the test cases in all the apps in your project. To make it possible for the Django testing utility to discover the test cases you have, you write the test cases in scripts whose names begin with test. In this step, you’ll create the directory structure and files for your test suite, and create an empty test case in it.

Django Test Runner

If you followed the Django Development tutorial series, you’ll have a Django app called blogsite.

Django Test Runner

Let’s create a folder to hold all our testing scripts. First, activate the virtual environment:

Then navigate to the blogsite app directory, the folder that contains the models.py and views.py files, and then create a new folder called tests:

Next, you’ll turn this folder into a Python package, so add an __init__.py file:

You’ll now add a file for testing your models and another for testing your views:

Finally, you will create an empty test case in test_models.py. You will need to import the Django TestCase class and make it a super class of your own test case class. Later on, you will add methods to this test case to test the logic in your models. Open the file test_models.py:

Now add the following code to the file: Outlook 2016 show ruler.

You’ve now successfully added a test suite to the blogsite app. Next, you will fill out the details of the empty model test case you created here.

Step 2 — Testing Your Python Code

In this step, you will test the logic of the code written in the models.py file. In particular, you will be testing the save method of the Post model to ensure it creates the correct slug of a post’s title when called.

Let’s begin by looking at the code you already have in your models.py file for the save method of the Post model:

You’ll see the following:

~/my_blog_app/blog/blogsite/models.py

We can see that it checks whether the post about to be saved has a slug value, and if not, calls slugify to create a slug value for it. This is the type of logic you might want to test to ensure that slugs are actually created when saving a post.

Close the file.

To test this, go back to test_models.py:

Then update it to the following, adding in the highlighted portions:

This new method test_post_has_slug creates a new post with the title 'My first post' and then gives the post an author and saves the post. After this, using the assertEqual method from the Python unittest module, it checks whether the slug for the post is correct. The assertEqual method checks whether the two arguments passed to it are equal as determined by the ' operator and raises an error if they are not.

Save and exit test_models.py.

This is an example of what can be tested. The more logic you add to your project, the more there is to test. If you add more logic to the save method or create new methods for the Post model, you would want to add more tests here. You can add them to the test_post_has_slug method or create new test methods, but their names must begin with test.

You have successfully created a test case for the Post model where you asserted that slugs are correctly created after saving. In the next step, you will write a test case to test views.

Step 3 — Using Django’s Test Client

In this step, you will write a test case that tests a view using the Django test client. The test client is a Python class that acts as a dummy web browser, allowing you to test your views and interact with your Django application the same way a user would. You can access the test client by referring to self.client in your test methods. For example, let us create a test case in test_views.py. First, open the test_views.py file:

Then add the following:

~/my_blog_app/blog/blogsite/tests/test_views.py

The ViewsTestCase contains a test_index_loads_properly method that uses the Django test client to visit the index page of the website (http://your_server_ip:8000, where your_server_ip is the IP address of the server you are using). Then the test method checks whether the response has a status code of 200, which means the page responded without any errors. As a result you can be sure that when the user visits, it will respond without errors too.

Apart from the status code, you can read about other properties of the test client response you can test in the Django Documentation Testing Responses page.

In this step, you created a test case for testing that the view rendering the index page works without errors. There are now two test cases in your test suite. In the next step you will run them to see their results.

Step 4 — Running Your Tests

Now that you have finished building a suite of tests for the project, it is time to execute these tests and see their results. To run the tests, navigate to the blog folder (containing the application’s manage.py file):

Then run them with:

You’ll see output similar to the following in your terminal:

In this output, there are two dots ., each of which represents a passed test case. Now you’ll modify test_views.py to trigger a failing test. First open the file with:

Then change the highlighted code to:

Case

Here you have changed the status code from 200 to 404. Now run the test again from your directory with manage.py:

You’ll see the following output:

You see that there is a descriptive failure message that tells you the script, test case, and method that failed. It also tells you the cause of the failure, the status code not being equal to 404 in this case, with the message AssertionError: 200 != 404. The AssertionError here is raised at the highlighted line of code in the test_views.py file:

~/my_blog_app/blog/blogsite/tests/test_views.py

It tells you that the assertion is false, that is, the response status code (200) is not what was expected (404). Preceding the failure message, you can see that the two dots . have now changed to .F, which tells you that the first test case passed while the second didn’t.

Conclusion

In this tutorial, you created a test suite in your Django project, added test cases to test model and view logic, learned how to run tests, and analyzed the test output. As a next step, you can create new test scripts for Python code not in models.py and views.py.

Following are some articles that may prove helpful when building and testing websites with Django:

  • The Django Unit Tests documentation
  • The Scaling Django tutorial series

Django Test Case

You can also check out our Django topic page for further tutorials and projects.

Latest version

Django Test Runner

Yasu. Released: Catalystex 4.5.

Django Selenium test runner.Incorporate functional testing into Django's manage.py test subcommandusing Selenium web testing tools.

Django Test Db

Project description

###########################
django-selenium-test-runner
###########################
`django-selenium-test-runner`_ incorporates functional testing into Django's
manage.py test subcommand using `Selenium web testing tools`_.
Background
This package was made to facilitate and simplify functional testing in Django
using Selenium tools.
Selenium tests are code that emulate a user/web browser interaction allowing
automatic web server testing. These tests can be created using `selenium-ide`_
and exported as python files for this test runner to use them. Selenium-ide
allows to record in real time a user interaction with a web browser, in a
similar way as a macro recorder in word processing applications.
`Fixtures`_ are fixed data fed into the database at the beginning of each
test run. The idea is that each test run against a consistent predefined state.
Fixtures can be created using manage.py dumpdata [options] [appname ..]
Installation
If you have `setuptools`_ installed, you can simply run the following command::
sudo easy_install django-selenium-test-runner
If you downloaded the package, you can just unpack it with::
tar zxvf django-selenium-test-runner-0.1.0.tar.gz
and copy 'dstest' directory tree to Python's site-packages directory, which is
usually located at:
/usr/lib/python2.4/site-packages (Unix, Python 2.4)
/usr/lib/python2.5/site-packages (Unix, Python 2.5)
/usr/lib/python2.6/dist-packages (Unix, Python 2.6)
django-selenium-test-runner is enabled in the project's settings.py with::
TEST_RUNNER = 'dstest.test_runner.run_tests'
Usage
Both, django unittest and selenium tests will be run with the standard command::
python manage.py test [options] [appname ..]
The exported selenium tests will be searched in django_app_dir/tests/selenium/
directories where django_app_dir is an application defined in INSTALLED_APPS.
This default can be changed with the setting SELENIUM_TESTS_PATH. Test names
start with 'test_'. As these tests will be imported, please be sure to create
django_app_dir/tests/__init__.py and django_app_dir/tests/selenium/__init__.py
files as any python package.
Fixture data is loaded by default from django_app_dir/fixtures/tests/data.json
at the beginning of each selenium test. This default can be change using the
FIXTURES setting.
Settings
There is only one required setting into your project's settings.py, assuming
django-selenium-test-runner is correctly installed:
TEST_RUNNER = 'dstest.test_runner.run_tests'
optional settings are:
* SELENIUM_TESTS_PATH - Changes default directories to look for Selenium tests
within the application directories. (Default: 'tests/selenium')
* FIXTURES - List of fixture files to load within the django_app_dir/fixtures
directories. (Default: ['tests/data.json'])
* SELENIUM_PATH - Directory path for Selenium RC jar its python driver
(i.e.: selenium-server.jar and selenium.py)
(Default: path where django-selenium-test-runner/dstest is installed)
Testing the package
django-selenium-test-runner comes with its own test suite based on the Django
`tutorial`_. It is designed to serve as example in a Django admin application,
and showcase django-selenium-test-runner capabilities. To run it, cd into the
tests directory of the package and execute::
python runtests
Dependencies
Most dependencies are integrated in the django-selenium-test-runner package.
For now, either Sqlite 3 or Postgres is required as more testing is needed to
make it database agnostic.
Included in django-selenium-test-runner package:
* `Selenium RC server and python driver`_. Provide selenium testing engine.
Tested with selenium-server.jar and selenium.py v1.0.1
* `CherryPy WSGI multi-thread web server`_. Provide a reliable web server.
Tested with wsgiserver.py v3.1.2
* `Django mediahandler.py`_, by Artem Egorkine. Provide static media handler.
Not included in the package:
* `Python 2.x`_ where x >= 4. Tested with Python v2.6
* `Django 1.x`_. Tested with Django v1.1
* `Java VM command line runner`_. Provide selenium-server.jar dependency.
Tested with java openjdk-6-jre.
* `Sqlite 3`. Provided by Python v2.5 or higher.
* `Postgres`_ as a database engine. Provide database replication for fixtures.
Tested with Postgres v8.2
* `Python-PostgreSQL database driver`_. Provide access to postgres database.
Tested with psycopg2 v2.0.5
. _django-selenium-test-runner: http://pypi.python.org/pypi/django-selenium-test-runner
. _Selenium web testing tools: http://seleniumhq.org/
. _selenium-ide: http://seleniumhq.org/movies/intro.mov
. _Fixtures: http://docs.djangoproject.com/en/dev/howto/initial-data/
. _setuptools: http://pypi.python.org/pypi/setuptools
. _tutorial: http://docs.djangoproject.com/en/dev/intro/tutorial01/
. _Selenium RC server and python driver: http://release.seleniumhq.org/selenium-remote-control/1.0.1/selenium-remote-control-1.0.1-dist.zip
. _CherryPy WSGI multi-thread web server: http://www.cherrypy.org/wiki/CherryPyInstall
. _Django mediahandler.py: http://www.arteme.fi/blog/2009/02/26/django-cherrypy-dev-server-and-static-files
. _Python 2.x: http://www.python.org/download/
. _Django 1.x: http://docs.djangoproject.com/en/dev/topics/install/
. _Java VM command line runner: http://openjdk.java.net/install/
. _Postgres: http://www.postgresql.org/download/
. _Python-PostgreSQL database driver: http://pypi.python.org/pypi/psycopg2/

Release historyRelease notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for django-selenium-test-runner, version 0.1.0
Filename, sizeFile typePython versionUpload dateHashes
Filename, size django-selenium-test-runner-0.1.0.tar.gz (5.3 MB) File type Source Python version None Upload dateHashes
Close

Hashes for django-selenium-test-runner-0.1.0.tar.gz

Django Test Runner Free

Hashes for django-selenium-test-runner-0.1.0.tar.gz
AlgorithmHash digest
SHA2567abcca84dc05cc5971d78a9871d427dabc3791e18bddd6f17767f802c34619f2
MD5e78ba28ba99e4a6e6a147808a249f104
BLAKE2-256a1df04057b926389fd62c9145a496cfa3322d5b19c066093d4338014c1c8d52c