Stoiczkow
Stoiczkow

Reputation: 49

Testing Django - Saving test DB

In my tests.py I have:

from functions import function
from .models import Student

def setUp(self):
    self.student = Student.objects.create(status=False)

def test_function():
    function() # function changes student's status to True
    print(self.student.status) # I'm still getting False

While running tests, my function doesn't change the attributes of objects created for tests so i can't test if function works properly. Is there a way to make sth like scenario below:

Function I want to test doesn't return anything, it only changes attriubtes of objects.

Upvotes: 0

Views: 284

Answers (1)

Thaian
Thaian

Reputation: 1245

Let's put setUp in class which is your TestCase class. To do this lets create class for example TestXXX and inside put setup and your test_function:

from django.test import TestCase

class TestXXX(TestCase):

    def setUp(self):
        self.student = Student.objects.create(status=False)

    def test_function():
        function() # function changes student's status to True
        print(self.student.status) # I'm still getting False

Function which you run make some changes in your DB but new data is not reflect on your self.students variable so u need get new data using:

self.student.status.refresh_from_db()
print(self.student.status)

Model.refresh_from_db

Upvotes: 2

Related Questions