[Fixed]-Django LiveServerTestCase: User created in in setUpClass method not available in test_method?


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.



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.


You should be able to use TestCase.setUpTestData as follows (slight changes to your base class):


from selenium.webdriver.firefox.webdriver import WebDriver
from django.test import LiveServerTestCase, TestCase

class CustomLiveTestCase(LiveServerTestCase, TestCase):

    def setUpClass(cls):
        cls.wd = WebDriver()
        super(CustomLiveTestCase, cls).setUpClass()

    def tearDownClass(cls):
        super(CustomLiveTestCase, cls).tearDownClass()


from django.contrib.auth.models import User
from django.test.utils import override_settings
from test_utils import CustomLiveTestCase
from test_constants import *

@override_settings(STRIPE_SECRET_KEY='xxx', STRIPE_PUBLISHABLE_KEY='xxx')
class MembershipTests(CustomLiveTestCase):

    fixtures = [

    def setUpTestData(cls):
        super(MembershipTests, cls).setUpTestData()
        user = User.objects.create_user(

    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!

Leave a comment