Django LiveServerTestCase: User created in in setUpClass method not available in test_method?
The database is torn down and reloaded on every test method, not on the test class. So your user will be lost each time. Do that in setUp
not setUpClass
.
Since you're using LiveServerTestCase it's almost same as TransactionTestCase which creates and destroys database (truncates tables) for every testcase ran.
So you really can't do global data with LiveServerTestCase.
You should be able to use TestCase.setUpTestData
as follows (slight changes to your base class):
test_utils.py:
from selenium.webdriver.firefox.webdriver import WebDriverfrom django.test import LiveServerTestCase, TestCaseclass CustomLiveTestCase(LiveServerTestCase, TestCase): @classmethod def setUpClass(cls): cls.wd = WebDriver() super(CustomLiveTestCase, cls).setUpClass() @classmethod def tearDownClass(cls): cls.wd.quit() super(CustomLiveTestCase, cls).tearDownClass()
tests.py:
from django.contrib.auth.models import Userfrom django.test.utils import override_settingsfrom test_utils import CustomLiveTestCasefrom test_constants import *@override_settings(STRIPE_SECRET_KEY='xxx', STRIPE_PUBLISHABLE_KEY='xxx')class MembershipTests(CustomLiveTestCase): fixtures = [ 'account_extras/fixtures/test_socialapp_data.json', 'membership/fixtures/basic/plan.json', ] @classmethod def setUpTestData(cls): super(MembershipTests, cls).setUpTestData() user = User.objects.create_user( TEST_USER_USERNAME, TEST_USER_EMAIL, TEST_USER_PASSWORD ) def test_signup(self): print "users: ", User.objects.all()
Instead of changing the base class, you could inherit from TestCase
in MembershipTests, but you'll have to do this everytime you need test data.
Note that I've also removed the def setUp: pass
, as this will break the transaction handling.
Check out this thread for further details: https://groups.google.com/forum/#!topic/django-developers/sr3gnsc8gig
Let me know if you run into any issues with this solution!