Django client test (get, post) response load data from main db instead fixtures loaded in test db
<p>I'm new in Django, and I have this problem:
I use self.client to test routes in my app.
e.g.</p>

<pre><code>class BookSell(TestCase):
    fixtures = [
        'sellers.yaml',
        'books.yaml'
          ...
    ]

    def test_get_book_sell(self):
        book = Books.objects.get(pk=1)
        print(book.name)#it prints "El Quijote" in the test output
        response = self.client.get('/sell/book-sell')
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "Book Sell")
        self.assertContains(response, "El Quijote")#book name 
</code></pre>

<p>The view method linked to that path should load some data from the database and then render it. 
But when I run </p>

<pre><code>python manage.py test app_name
</code></pre>

<p>it doesn't load data from fixtures, because the response doesn't contain "El Quijote". Only when I run:</p>

<pre><code>python manage.py loaddata fixture_name.yaml ...
</code></pre>

<p>and then run again the tests the view method loads data from the database.</p>

<p>So I conclude that when I run the tests, my views methods load data from main database instead of test database</p>

<p>What is that I'm doing wrong?
Is there a way to load just data from test database?</p>

<p>django version: 1.9.4</p>

<p>python version: 2.7.6</p>

<p>I'm sure that the fixtures load with sucess in test database</p>

<p>Finally I discovered what I was doing wrong, thanks to this answer: <a href="http://stackoverflow.com/questions/29773665/django-reload-form-data-after-db-values-change">Django: reload form data after db values change</a></p>

<p>I had a form like this:</p>

<pre><code>class BookSellForm(forms.Form):

    books = [('', 'Choose a book')]
    books.extend(Books.objects.values_list('id', 'name'))
    libro = forms.ChoiceField(books)
</code></pre>

<p>And that was the problem, I had to change to this</p>

<pre><code>class BookSellForm(forms.Form):

    def __init__(self, *args, **kwargs):
        super(BookSellForm, self).__init__(*args, **kwargs)
        books = [('', 'Choose a book')]
        books.extend(Books.objects.values_list('id', 'name'))
        self.fields['book'] = forms.ChoiceField(books)
</code></pre>

<p>and then everything worked fine, I hope this will be helpful to someone in the future.</p>

python django testing fixtures