M. hasbini M. hasbini - 14 days ago 5
Python Question

Django's StaticLiveServerTestCase test pass alone but fail when grouped

I'm using

Selenium
along with
StaticLiveServerTestCase
to test a Django app.

The test is as follow:

class e2eTest(StaticLiveServerTestCase):
@classmethod
def setUpClass(cls):
super(IntegrationTest, cls).setUpClass()
cls.data = load_data()
cls.driver = webdriver.PhantomJS("path_to_phantomjs")
cls.common = common(cls.driver, cls.live_server_url + settings.STATIC_URL + 'index.html')

def setUp(self):
for data in self.data:
data.refresh_from_db()

def test_login_1(self):
self.common.login('admin')

def test_login_2(self):
self.common.login('admin')


load_data()
is used to populate test database (using apps
models
).

If I run:

1-
python manage.py test login.tests
: the tests start with
test_login_1
.
test_login_1
succeed and
test_login_2
fail.

2-
python manage.py test login.tests --reverse
: the tests start with
test_login_2
.
test_login_2
succeed and
test_login_1
fail.

I believe it have something to do with data begin erased after each test.
P.S. When using same approach using
django.test.TestCase
&
setUpTestData
and Django's test client it works ( sending the data to the login api directly ).

Can I have something like
setUpTestData
from
django.test.TestCase
?

EDIT 1

@luke_aus:
load_data()
contain methods to populate the test database:

from my_app import User, priceModel
user = User(username='test').set_password('test')
user.save()
priceModel = priceModel(name='test')
priceModel.save()
....


@knbk: I'm using Django 1.9. The test still fail after passing
--parallel=1
.

Thanks you both for your feedback!

Answer

The problem was caused by load_data(). The database will get populated in the first call to load_data() from setUpClass, and the first test will passes. For the second test, the database data will be cleared and not refreshed.

Changing load_data() method to load fixtures solved the problem.