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 Writer model

Alexander Doronin
Alexander Doronin
610 Points

Hi! I cant't understand what does it mean: "Make sure you used `assertIn` and checked the `email` attribute"

What is incorrect in my test?

articles/tests.py
from django.test import TestCase
from .models import Writer

class WriterModelTestCase(TestCase):
    def test_writer_creation(self):
        writer = Writer.objects.create(
            name = "Ivan",
            email = "test@test.com",
            bio = "Ivan was born in Russia"
        )
        email = "test@test.com"
        self.assertIn(email, writer.mailto())
articles/models.py
from django.db import models


class Article(models.Model):
    headline = models.CharField(max_length=255)
    publish_date = models.DateTimeField()
    content = models.TextField()
    writer = models.ForeignKey('Writer')

    def __str__(self):
        return self.headline


class Writer(models.Model):
    name = models.CharField(max_length=255)
    email = models.EmailField()
    bio = models.TextField()

    def __str__(self):
        return self.name

    def mailto(self):
        return '{} <{}>'.format(self.name, self.email)

1 Answer

Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

Hi Alexander,

The Challenge is looking for quite a subtle change to your code.

Instead of writing the same value for email twice in your test (once in your writer assignment and then again in your email assignment), just create writer (as you have done) and then check that writer.email is in the output of writer.mailto().

This might seem like a dumb change, especially when the challenge instructions aren't very helpful about exactly why you are creating this test. However, there is a reason for this: When writing unit tests, you are not testing that the framework works, you are testing that your code works (in this challenge we are pretending that the mailto() method is your code).

Accordingly, it's the Django team's job to make sure that the model creation process works, as users, we rely on them having done their testing. When you ask Django to create a Writer object with the specified email attribute and then type that value again as a test value, you are really (in part) testing that Django correctly used the email value you provided when it created the writer.

This is mixed up with what you really want to test: that your mailto() method works, i.e., that it returns the email attribute from the Writer object. Put another way, you are testing that the method agrees to the underlying model, not that the method outputs a particular value.

I hope that clears things up for you.

Cheers

Alex

Alexander Doronin
Alexander Doronin
610 Points

Thank you! Now everything is clear!