Python v2.6.1 documentation » The Python Standard Library » Development Tools » previous | next | modules | index
— Unit testing framework
Table Of Contents unittest
— Unit testing
unittest
framework New in version 2.1.
Basic example
Organizing test code
The Python unit testing framework, sometimes referred to as “PyUnit,” is a
Re-using old test code
Python language version of JUnit, by Kent Beck and Erich Gamma. JUnit
Classes and functions
is, in turn, a Java version of Kent!s Smalltalk testing framework. Each is
TestCase Objects
TestSuite Objects the de facto standard unit testing framework for its respective language.
TestResult Objects
TestLoader Objects supports test automation, sharing of setup and shutdown code
unittest
Previous topic for tests, aggregation of tests into collections, and independence of the
tests from the reporting framework. The unittest module provides
— Test interactive
doctest
classes that make it easy to support these qualities for a set of tests.
Python examples
Next topic To achieve this, supports some important concepts:
unittest
2to3 - Automated Python 2 to
3 code translation test fixture
A test fixture represents the preparation needed to perform one or
This Page
more tests, and any associate cleanup actions. This may involve, for
Show Source example, creating temporary or proxy databases, directories, or
Quick search starting a server process.
Testing Python with PyUnit
test case
Go
A test case is the smallest unit of testing. It checks for a specific
response to a particular set of inputs. unittest provides a base class,
Enter search terms or a module,
class or function name. TestCase , which may be used to create new test cases.
test suite
gareth rushgrove | morethanseven.net
A test suite is a collection of test cases, test suites, or both. It is used
Again, remember that you can use both systems side-by-side (even in the same
app). In the end, most projects will eventually end up using both. Each shines in
different circumstances.
Running tests
Once you've written tests, run them using your project's manage.py utility:
$ ./manage.py test
By default, this will run every test in every application in INSTALLED_APPS. If you
only want to run tests for a particular application, add the application name to the
command line. For example, if your INSTALLED_APPS contains
'myproject.polls' and 'myproject.animals', you can run the
myproject.animals unit tests alone with this command:
$ ./manage.py test animals
Note that we used animals, not myproject.animals.
New in Django 1.0: You can now choose which test to run.
If you use unit tests, as opposed to doctests, you can be even more specific in
choosing which tests to execute. To run a single test case in an application (for
example, the AnimalTestCase described in the quot;Writing unit testsquot; section), add
the name of the test case to the label on the command line:
$ ./manage.py test animals.AnimalTestCase
Django Test Runner
And it gets even more granular than that! To run a single test method inside a test
case, add the name of the test method to the label:
$ ./manage.py test animals.AnimalTestCase.testFluffyAnimals
The test database
gareth rushgrove | morethanseven.net
What to Test - Low Level Code
gareth rushgrove | morethanseven.net
- Functions
- Input/output
- Object methods
- Object creation
- Action at a distance via signals
Unit Tests and System Tests
gareth rushgrove | morethanseven.net
What to Test - High Level Code
gareth rushgrove | morethanseven.net
- HTTP Status codes
- Fragments of HTML from templatetags
- Broken links
- Presence of markup on pages
- Rendered HTML
- Check admin registration
- Functionality
Functional Tests
gareth rushgrove | morethanseven.net
garethr account | profile | guides | log out
repositories: all | search
0
Source Commits Graphs Wiki (1) Watchers (1) Network (1) Fork Queue Admin
master all branches all tags
garethr / django-test-extensions
Description: A set of custom assertions and examples for use testing django applications edit
Homepage: Click to edit edit
Public Clone URL: git://github.com/garethr/django-test-extensions.git
Your Clone URL: git@github.com:garethr/django-test-extensions.git
commit c327bac72d990af890c231e33d0e146a79b7c507
refactor into setup tools module and include custom test runners
tree 92148ec270467b41cb7d5ef5f194a14b44192d8d
garethr (author) parent 85237c52d0afd22eb3c1af1e7d639f21fa5bfde9
November 24, 2008
django-test-extensions /
name age message history
.gitignore November 24, 2008 refactor into setup tools module and include cu... [garethr]
README November 22, 2008 seperated out django assertions and added README [garethr]
setup.py November 24, 2008 refactor into setup tools module and include cu... [garethr]
Common Base Class
src/ November 24, 2008 refactor into setup tools module and include cu... [garethr]
PyUnit provides a basic set of assertions which can get you started with unit testing python, but it's always useful to
have more. Django also has a few specific requirements and common patterns when it comes to testing. This set of classes
aims to provide a useful starting point for both these situations.
gareth rushgrove | morethanseven.net
Unit System Functional Integration
Separate Test Suites
gareth rushgrove | morethanseven.net
Ellington’s test suite, which was
taking around 1.5-2 hours to
run on Postgres, has been
reduced to 10 minutes.
ericholscher
Speed Improvements
gareth rushgrove | morethanseven.net