2

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??

1

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.