Reactoo
Reactoo

Reputation: 1032

Why object is not being created while running Django TestCase

I created a simple model test case using TestCase class. I created an object using setUp function and test the count of the object using assertEqual inside another function.

The test ran successfully, but when I check the Django admin, there was no object that I just created, and also there was no table named django_test.

My test case:

class TestContact(TestCase):

    def setUp(self):
        self.contact = Contact.objects.create(
            full_name = "John Van",
            email = "[email protected]",
            phone = 9845666777,
            subject = "My query",
            message = "test message"
        )

    def test_contact_count(self):
        self.assertEqual(Contact.objects.count(),1)

Also, is setUp, a built in function or we can use any function name while creating the object inside it??

Upvotes: 2

Views: 1055

Answers (1)

Brian Destura
Brian Destura

Reputation: 12068

Tests create separate blank test databases that are different from your 'real' database. This means your Django admin will not be using the same test database.

You can read more here: https://docs.djangoproject.com/en/3.2/topics/testing/overview/#the-test-database

About setUp, its part of the TestCase class and is called before every test. By itself it does nothing and you can create custom functions that prepare your models and call them inside setUp

You can read more here: https://docs.python.org/3/library/unittest.html#unittest.TestCase.setUp

Upvotes: 1

Related Questions