Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Django Basics Test Time Test our article list view

Django basics -> Test Time -> Test our article list view [RESOLVED]

Challenge Task 1 of 1

We want to be sure that our article_list view returns all of our articles. Add a test to the ArticleListViewTestCase that uses assertIn to make sure that self.article is in the "articles" context dict key when you use self.client.get() to get the list view. The view name is "articles:list".

Link to challenge:

http://teamtreehouse.com/library/django-basics/test-time/test-our-article-list-view

My Code:

articles/tests.py
import datetime

from django.core.urlresolvers import reverse
from django.test import TestCase

from .models import Article, Writer

class ArticleListViewTestCase(TestCase):
    '''Tests for the Article list view'''

    def setUp(self):
        self.writer = Writer.objects.create(
            name='Kenneth Love',
            email='kenneth@teamtreehouse.com',
            bio='Your friendly, local Python teacher'
        )
        self.article = Article.objects.create(
          writer=self.writer,
          headline='Article 0',
          content='Something about 0',
          publish_date=datetime.datetime.today()
        )
        for x in range(1, 3):
            Article.objects.create(
                writer=self.writer,
                headline='Article {}'.format(x),
                content='Something about {}'.format(x),
                publish_date=datetime.datetime.today()
            )
         def test_article_list_view(self):
              resp = self.client.get(reverse("articles:list"))
              self.assertIn(self.article, resp.context['articles'])
articles/views.py
from django.shortcuts import get_object_or_404, render

from .models import Article, Writer


def article_list(request):
    articles = Article.objects.all()
    return render(request, 'articles/article_list.html', {'articles': articles})


def article_detail(request, pk):
    article = get_object_or_404(Article, pk=pk)
    return render(request, 'articles/article_detail.html', {'article': article})


def writer_detail(request, pk):
    writer = get_object_or_404(Writer, pk=pk)
    return render(request, 'articles/writer_detail.html', {'writer': writer})

Result:

Bummer! Your test didn't pass.

Why not?


This seems to be similar to the help given in:

https://teamtreehouse.com/community/test-should-be-close-to-the-video-but-is-giving-me-the-wrong-answer

Note: I also tried it without the 'reverse' and got the same Bummer! Indenting 2 spaces instead of 4 also gave the same Bummer!


(Later...)

Okay, I solved it. It was an indenting issue.

The 'def test_article_list_view(self):' needs to align directly underneath

(and with the same indenting as) 'def setUp(self):'

It should NOT line up with 'for x in range(1, 3):'

Chris Freeman
Chris Freeman
Treehouse Moderator 68,454 Points

James, Add your last comment as an answer so this question can be marked as answered. Thanks.

1 Answer

Andrew Winkler
Andrew Winkler
37,739 Points

Nobody did what Chris requested, so I checked the indentation, and this version should pass:

import datetime

from django.core.urlresolvers import reverse
from django.test import TestCase

from .models import Article, Writer

class ArticleListViewTestCase(TestCase):
    '''Tests for the Article list view'''

    def setUp(self):
        self.writer = Writer.objects.create(
            name='Kenneth Love',
            email='kenneth@teamtreehouse.com',
            bio='Your friendly, local Python teacher'
        )
        self.article = Article.objects.create(
          writer=self.writer,
          headline='Article 0',
          content='Something about 0',
          publish_date=datetime.datetime.today()
        )
        for x in range(1, 3):
            Article.objects.create(
                writer=self.writer,
                headline='Article {}'.format(x),
                content='Something about {}'.format(x),
                publish_date=datetime.datetime.today()
            )
    def test_article_list_view(self):
      resp = self.client.get(reverse("articles:list"))
      self.assertIn(self.article, resp.context['articles'])