I asked myself the same question before. I believe that doctrines have limited usefulness for things like representations, model methods, and managers, because
- You need to be able to customize and delete the test data set that is actually used for testing.
- Views must have a request object. In the doctrine, where does this come from?
For this reason, I have always used the Django unit testing unit , which handles all this for you. Unfortunately, you do not get some of the benefits of doctrines, and this makes TDD / BDD more difficult. The following is pure speculation on how you can do this:
I think you want to capture doctrines from your respective modules and functions and execute them as part of unit testing. This will take care of setting up / deleting test data. If your doctrines were executed from a test method for subclassing Django unittest.TestCase, they will be able to use this test database. You can also pass the mock request object to the doc test execution context. Here's a Django snippet that provides a mock request object and info on it. Let's say you wanted to test docstrings from all kinds of applications. You can do something like this in tests.py:
from ??? import RequestFactory from doctest import testmod, DocTestFailure from django.test import TestCase from myapp import views class MyAppTest(TestCase): fixtures = ['test_data.json'] def test_doctests(self): try: testmod(views, extraglobs={ 'REQUEST': RequestFactory() }, raise_on_error=True) except DocTestFailure, e: self.fail(e)
This should allow you to do something like this:
def index(request): """ returns the top 10 most clicked products >>> response = index(REQUEST) >>> [test response content here] """ products = Product.objects.all()[:10] products = match_pictures_with_products( products, 10) . return render_to_response('products/product_list.html', {'products': products})
Again, this is not at all in my head, but not tested at all, but this is the only way that, as I think, you could wish for simply not putting all your tests for viewing in a unit testing system.
mazelife
source share