ranjanistic/knotters

Tests optimization

Closed this issue · 0 comments

Some tests fail arbitrarily sometimes, even on CI.

One approach to resolve this can be this: localize the Client object for every test to avoid testing session collisions particularly when the tests need a logged in test user.
This also means that, if required, session should be created for each test individually.
For example,

Instead of doing the following (which is currently being done for most of the tests)

...
def setupTestData(self):
    self.client = Client()
    self.client.login(username,password)
    ...

def test_some_individual_test_function(self):
    response = self.client.get(...)
    ....

or

...
def setUp(self):
    self.client = Client()
    self.client.login(username, password)
    ...

def test_some_individual_test_function(self):
    response = self.client.get(...)
    ....

do the following

...
def test_some_individual_test_function(self):
    # before anything else in this test
    client = Client()
    client.post(authroot(URL.LOGIN), dict(login=username, password=password)) # whatever the login URL and username, password is
    response = client.get(...)
    ...

Doing this will ensure that each test function is getting its own session (client object) for testing, and thus will reduce chances of session clashes. Also, try to do the same for setting up required database records for individual tests rather than creating records globally in setUp or setupTestData methods.