Background Image
Table of Contents Table of Contents
Previous Page  348 / 478 Next Page
Information
Show Menu
Previous Page 348 / 478 Next Page
Page Background

Ran 15 tests in 0.033s

OK

You can easily imagine how you could test more combinations at this point, if you

wanted different error messages for

response.ok != True

, and so on.

Wrap-Up

We now have test fixtures that work both locally and on the server, and we’ve got some

more robust logging configuration.

But before we can deploy our actual live site, we’d better actually give the users what

they wanted—the next chapter describes how to give them the ability to save their lists

on a “My Lists” page.

Fixtures and Logging

De-duplicate your FTs, with caution

Every single FT doesn’t need to test every single part of your application. In our

case, we wanted to avoid going through the full login process for every FT that needs

an authenticated user, so we used a test fixture to “cheat” and skip that part. You

might find other things you want to skip in your FTs. A word of caution however:

functional tests are there to catch unpredictable interactions between different parts

of your application, so be wary of pushing de-duplication to the extreme.

Test fixtures

Test fixtures refers to test data that needs to be set up as a precondition before a test

is run—often this means populating the database with some information, but as

we’ve seen (with browser cookies), it can involve other types of preconditions.

Avoid JSON fixtures

Django makes it easy to save and restore data from the database in JSON format

(and others) using the

dumpdata

and

loaddata

management commands. Most

people recommend against using these for test fixtures, as they are painful to man‐

age when your database schema changes. Use the ORM, or a tool like

factory_boy .

Fixtures also have to work remotely

LiveServerTestCase

makes it easy to interact with the test database using the

Django ORM for tests running locally. Interacting with the database on the staging

server is not so straightforward—one solution is Django management commands,

as I’ve shown, but you should explore what works for you, and be careful!

320

|

Chapter 17: Test Fixtures, Logging, and Server-Side Debugging