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

Test should be close to the video but is giving me the wrong answer

The test for the exercise is nearly identical to what we did in the video, but for some reason it is not giving me the right answer when it should. Is there something I'm overlooking, or is it just a matter of something wrong with the syntax of the test itself?

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.assertEqual( resp.status_code, 200 )
        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})

2 Answers

Check your indentation on the test_article_list_view method. Line it up with the setUp method. Maybe a couple of spaces to the left?

Ryan Ruscett
Ryan Ruscett
23,309 Points

Hey,

You have the right idea here. The 200 error isn't required for this exercise. It could throw off the test. So I would remove it. Your indentation is off as well but that doesn't matter for this.

What does matter are all of your spaces. Remove them all.

#NO you put spaces after and before each ( ) and after the method with : Don't do that. 
def test_article_list_view(self) :
    self.assertEqual( resp.status_code, 200 )
    self.assertIn( self.article, resp.context['articles'] )


#YES this will fix it
def test_article_list_view(self):
    self.assertEqual(resp.status_code, 200)
    self.assertIn(self.article, resp.context['articles'])

Let me know if that works for you. You just about had it. If not, let me know and I will help you through it!

Thanks!

Hey, I got it thanks a ton.