mirror of
https://github.com/django/django.git
synced 2025-07-05 02:09:13 +00:00
[gsoc2009-testing] Extending documentation, fixing rst formatting.
git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/test-improvements@11144 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
0c11738415
commit
b1b9d34fa5
@ -11,80 +11,48 @@ popular `Windmill`_ framework. Writing a windmill test
|
|||||||
is simple, following these steps:
|
is simple, following these steps:
|
||||||
|
|
||||||
.. _Windmill: http://getwindmill.com
|
.. _Windmill: http://getwindmill.com
|
||||||
|
|
||||||
#. Your windmill tests must be their own module, named ``wmtests`` or ``windmilltests``.
|
#. Your windmill tests must be their own module, named ``wmtests`` or ``windmilltests``.
|
||||||
|
|
||||||
|
#. Django must be able to run any function in the module without arguments.::
|
||||||
|
|
||||||
#. Django must be able to run any function in the module without arguments.
|
from windmill.conf import global_settings
|
||||||
::
|
ADMIN_URL = "%s/test_admin/admin" % global_settings.TEST_URL
|
||||||
from windmill.conf import global_settings
|
from windmill.authoring import WindmillTestClient
|
||||||
ADMIN_URL = "%s/test_admin/admin" % global_settings.TEST_URL
|
from django.test.utils import calling_func_name
|
||||||
from windmill.authoring import WindmillTestClient
|
|
||||||
from django.test.utils import calling_func_name
|
|
||||||
|
|
||||||
def test_loginAndSetup():
|
def test_loginAndSetup():
|
||||||
'''Mostly just a proof of concept to test working order of tests.'''
|
'''Mostly just a proof of concept to test working order of tests.'''
|
||||||
client = WindmillTestClient(calling_func_name())
|
client = WindmillTestClient(calling_func_name())
|
||||||
|
|
||||||
client.open(url='http://localhost:8000/admin')
|
client.open(url='http://localhost:8000/admin')
|
||||||
client.waits.forPageLoad(timeout=u'20000')
|
client.waits.forPageLoad(timeout=u'20000')
|
||||||
...
|
...
|
||||||
|
|
||||||
#. Your windmill testing module must load any files other than the module loader
|
#. Your windmill testing module must load any files other than the module loader in ``__init__.py``.::
|
||||||
in ``__init__.py``.
|
|
||||||
::
|
|
||||||
from primary.py import *
|
|
||||||
|
|
||||||
Your custom storage system may override any of the storage methods explained in
|
from primary import *
|
||||||
:ref:`ref-files-storage`, but you **must** implement the following methods:
|
...
|
||||||
|
|
||||||
* :meth:`Storage.delete`
|
Setup and Teardown are done differently for windmill tests, and consist of the following
|
||||||
* :meth:`Storage.exists`
|
functions. :
|
||||||
* :meth:`Storage.listdir`
|
|
||||||
* :meth:`Storage.size`
|
|
||||||
* :meth:`Storage.url`
|
|
||||||
|
|
||||||
You'll also usually want to use hooks specifically designed for custom storage
|
* :meth:`setup_module`
|
||||||
objects. These are:
|
* :meth:`teardown_module`
|
||||||
|
|
||||||
``_open(name, mode='rb')``
|
A sample implementation, in ``__init__.py``.::
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
**Required**.
|
from primary import *
|
||||||
|
|
||||||
Called by ``Storage.open()``, this is the actual mechanism the storage class
|
def setup_module(module):
|
||||||
uses to open the file. This must return a ``File`` object, though in most cases,
|
module.property = fetch_property()
|
||||||
you'll want to return some subclass here that implements logic specific to the
|
|
||||||
backend storage system.
|
|
||||||
|
|
||||||
``_save(name, content)``
|
def teardown_module(module):
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~
|
module.property = None
|
||||||
|
|
||||||
Called by ``Storage.save()``. The ``name`` will already have gone through
|
|
||||||
``get_valid_name()`` and ``get_available_name()``, and the ``content`` will be a
|
|
||||||
``File`` object itself.
|
|
||||||
|
|
||||||
Should return the actual name of name of the file saved (usually the ``name``
|
|
||||||
passed in, but if the storage needs to change the file name return the new name
|
|
||||||
instead).
|
|
||||||
|
|
||||||
``get_valid_name(name)``
|
These methods can be included in any file with windmill tests. See `functest`_
|
||||||
------------------------
|
for more information.
|
||||||
|
|
||||||
Returns a filename suitable for use with the underlying storage system. The
|
.. _functest : http://functest.pythonesque.org/
|
||||||
``name`` argument passed to this method is the original filename sent to the
|
|
||||||
server, after having any path information removed. Override this to customize
|
|
||||||
how non-standard characters are converted to safe filenames.
|
|
||||||
|
|
||||||
The code provided on ``Storage`` retains only alpha-numeric characters, periods
|
|
||||||
and underscores from the original filename, removing everything else.
|
|
||||||
|
|
||||||
``get_available_name(name)``
|
|
||||||
----------------------------
|
|
||||||
|
|
||||||
Returns a filename that is available in the storage mechanism, possibly taking
|
|
||||||
the provided filename into account. The ``name`` argument passed to this method
|
|
||||||
will have already cleaned to a filename valid for the storage system, according
|
|
||||||
to the ``get_valid_name()`` method described above.
|
|
||||||
|
|
||||||
The code provided on ``Storage`` simply appends underscores to the filename
|
|
||||||
until it finds one that's available in the destination directory.
|
|
@ -688,6 +688,28 @@ arguments at time of construction:
|
|||||||
and session data cleared to defaults. Subsequent requests will appear
|
and session data cleared to defaults. Subsequent requests will appear
|
||||||
to come from an AnonymousUser.
|
to come from an AnonymousUser.
|
||||||
|
|
||||||
|
|
||||||
|
Making mock requests
|
||||||
|
~~~~~~~~~~~~~~~~~~~~
|
||||||
|
.. versionadded:: 1.1
|
||||||
|
|
||||||
|
Use the ``django.test.mocks.RequestFactory`` class to create mock requests. Usage is as follows.
|
||||||
|
::
|
||||||
|
rf = RequestFactory()
|
||||||
|
get_request = rf.get('/hello/')
|
||||||
|
post_request = rf.post('/submit/', {'foo': 'bar'})
|
||||||
|
|
||||||
|
Once you have a request object you can pass it to any view function, just as if
|
||||||
|
that view had been hooked up using a URLconf.
|
||||||
|
|
||||||
|
|
||||||
|
.. class:: RequestFactory()
|
||||||
|
There is only one method on a ``RequestFactory``.
|
||||||
|
|
||||||
|
.. method:: RequestFactory.request(path, **request)
|
||||||
|
This will return a request object with the proper environment.
|
||||||
|
|
||||||
|
|
||||||
Testing responses
|
Testing responses
|
||||||
~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
@ -920,6 +942,8 @@ This means, instead of instantiating a ``Client`` in each test::
|
|||||||
response = self.client.get('/customer/index/')
|
response = self.client.get('/customer/index/')
|
||||||
self.failUnlessEqual(response.status_code, 200)
|
self.failUnlessEqual(response.status_code, 200)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
.. _topics-testing-fixtures:
|
.. _topics-testing-fixtures:
|
||||||
|
|
||||||
Fixture loading
|
Fixture loading
|
||||||
|
Loading…
x
Reference in New Issue
Block a user