2

I have this test in my Django 1.7 project:

class DummyTestTest(TestCase):
    def setUp(self):
        self.test_user = User.objects.create(username='tester', first_name='test', last_name='er', password="1234", email="foo@bar.com")

    def test_sanity(self):
        qs = User.objects.filter(pk = self.test_user.pk)
        self.assertIn(self.test_user, qs)

In my opinion, that means that during execution of the test, the test user should be part of the database. If I execute that test however:

(venv)tinloaf@janeway alumnet % python manage.py test core.tests.DummyTestTest
Creating test database for alias 'default'...
F
======================================================================
FAIL: test_sanity (core.tests.DummyTestTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/mnt/data/home/tinloaf/src/alumnet/alumnet/core/tests.py", line 41, in test_sanity
    self.assertIn(self.test_user, qs)
AssertionError: <User: tester> not found in []

----------------------------------------------------------------------
Ran 1 test in 0.012s

FAILED (failures=1)
Destroying test database for alias 'default'...

It does not matter whether I use TestCase or TransactionTestCase, the user is just not there. Did I hit a bug or am I doing something wrong?

Lukas Barth
  • 2,734
  • 18
  • 43
  • Can you please try: `u = User.objects.get(pk = self.test_user.pk)` and then `self.assertEqual(self.test_user.pk, u.pk)`. What happens then? – Jingo Mar 20 '15 at 15:24
  • Have you ever solved the issue? I am hitting the same problem right now. Thank's in advance! – Anton Feb 09 '17 at 10:14

0 Answers0