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 Model Tests

When creating model tests, is it good to hit our live database?

When we write these model tests, aren't we actually creating instances in our database? Is that the best convention, or should we use a temporary database? If the latter, how would you do that? It just seems weird to run tests that effect our real db.

Use SQLite that would create a database file on the root directory of your script file that you can use for testing.

1 Answer

Greg Kaleka
Greg Kaleka
39,021 Points

Hi Eddy,

Great question. I expect we'll cover mocking at some point, which gets around this issue entirely, but the issue isn't that we're messing with your live database - we're not - it's about performance. Once you have lots of tests in a big application, database operations can slow down testing considerably, especially if testing large operations. For this tiny project, it's not a problem.

About that live database. Take a careful look at the output when you run a test:

Creating test database for alias 'default'...
System check identified no issues (0 silenced).
..
----------------------------------------------------------------------
Ran 2 tests in 0.001s

OK
Destroying test database for alias 'default'...

See the first and last line? Django is creating and then destroying a test database, rather than using your actual database. So no need to worry about that aspect.

Cheers :beers:

-Greg