Sunday, October 18, 2009

Re: How to get multiple test cases to work with manage.py test?

On Sun, Oct 18, 2009 at 11:49 AM, Jeff <jeffreycharles@gmail.com> wrote:
>
> Hi,
>
> I'm fairly new to Django and unittest and I seem to be having a
> problem getting manage.py to execute all of my tests. I can only get
> it to execute one test at a time by specifying the test method. When I
> just specify the TestCase class expecting it to execute all of the
> test methods, it won't run any of the tests.
>
> In my articles/tests.py file I have the following:
> -------------------------------------------------------------------------------------------------------------
> # -*- coding: utf-8 -*-
> from django.test import TestCase
> from django.test.client import Client
> from oslaurier.articles.models import Article
>
> class ArticleTestCase(TestCase):
>    fixtures = ['test_fixture.json']
>
>    def setUp(self):
>        self.client = Client()
>
>    def test_index(self):
>        """
>        Test that article index page returns a 200 response
>        """
>        response = self.client.get('/articles/')
>        self.failUnlessEqual(response.status_code, 200)
>
>    def test_index_default_list(self):
>        """
>        Test that index is only displaying non-draft, non-hidden
> articles
>        """
>        response = self.client.get('/articles/')
>        hello_world = Article.objects.get(slug='hello-world')
>        two_author = Article.objects.get(slug='two-author-article')
>        multi_author = Article.objects.get(slug='multi-author-
> article')
>        self.assertEqual(list(response.context
> ['articles'].object_list).sort(),
>            [multi_author, two_author, hello_world].sort())
>
> class ArticlePaginationTestCase(TestCase):
>    fixtures = ['pagination.json']
>
>    def setUp(self):
>        self.client = Client()
>
>    def default_num_per_page(self):
>        """
>        Test that the default number of articles per page is 10
>        """
>        response = self.client.get('/articles/')
>        self.assertEqual(len(response.context
> ['articles'].object_list), 10)
>
>    def first_set_has_next(self):
>        """
>        Test that first set of articles has a next set
>        """
>        response = self.client.get('/articles/?num_per_page=10')
>        self.assertTrue(response.context['articles'].has_next)
> -------------------------------------------------------------------------------------------------------------
> If I run python manage.py test articles.ArticlePaginationTestCase I
> get:
>
> Creating test database...
> Creating table django_admin_log
> Creating table auth_permission
> Creating table auth_group
> Creating table auth_user
> Creating table auth_message
> Creating table django_content_type
> Creating table django_flatpage
> Creating table django_redirect
> Creating table django_session
> Creating table django_site
> Creating table articles_article
> Installing index for admin.LogEntry model
> Installing index for auth.Permission model
> Installing index for auth.Message model
> Installing index for flatpages.FlatPage model
> Installing index for redirects.Redirect model
> Installing index for articles.Article model
>
> ----------------------------------------------------------------------
> Ran 0 tests in 0.000s
>
> OK
> Destroying test database...
>
> I would've expected it to run both test methods under
> ArticlesPaginationTestCase.

Ah.. but there's the rub - it _did_ run all the test cases in
ArticlesPaginationTestCase :-)

The point of confusion is that there are no tests in
ArticlesPaginationTestCase - at least, not according to the criteria
of unittest.TestCase. TestCase requires that test functions _must_
have a name starting with "test" - thats how it automatically
discovers the test cases.

So, from the point of view of unittest.TestCase:

class ArticleTestCase(TestCase):
def test_index(self):
...
def test_index_default_list(self):
...

contains two tests, but:

class ArticlePaginationTestCase(TestCase):
def default_num_per_page(self):
...
def first_set_has_next(self):
...

contains no tests - it just contains two utility methods.

When you explicitly name the test on the command line, you work around
the problem. Since no automatic discovery is required, the prefix
isn't strictly required, and so the test can run. If you want unittest
to discover the tests automatically, just rename the test cases and
prepend the "test" prefix.

This isn't something unique to Django - it's a common feature of all
the xUnit family of APIs.

Yours,
Russ Magee %-)k

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate