14👍
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.
17👍
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
.
- How to assign to a Django PointField model attribute?
- Models inside tests – Django 1.7 issue
- Override Django allauth signup "next" redirect URL
- How to debug Django custom management command using VS Code
- Django filter on APIView
2👍
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 WebDriver
from django.test import LiveServerTestCase, TestCase
class 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 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 = [
'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!
- Django Test — Unable to drop and recreate test database
- Allow a task execution if it's not already scheduled using celery
- Using Django minus the web server
- Returning form errors for AJAX request in Django
- Can I cut out the string in the Django template?