Test a query using Django nose

78 views Asked by At

I am new to testing in django. I am using django nose for TDD.I am using django nose version 1.2 in my virtual environment. I referred the link below for creating my tests.

http://kokoko.fluxionary.net/testing-django-part-1-nose

Currently I need to test the query that I am going to write in my views ie to check whether the query output is correct. I used the code below but test fails:

import nose.tools as nt

nt.assert_true('obj_list' in resp.context)
nt.assert_equal([obj.pk for obj in resp.context['obj_list']], [1])

Any help will be much appreciated. Thanks in advance.

1

There are 1 answers

0
Kamil Rykowski On

It looks like you don't have any objects in your database, so the test fails - when you run your tests a new database is created, so data from development database is not going to be transfered into your isolated test environment.

Choose one of the available solutions:

  1. Create a fixture file, so it will hold data for all of your tests: https://docs.djangoproject.com/en/dev/howto/initial-data/

  2. Create objects in a setUp method or in the test method, and then try to do some asserts.

Read this first, if don't have experience with testing in Django: https://docs.djangoproject.com/en/1.6/topics/testing/overview/