【发布时间】:2013-02-05 17:44:51
【问题描述】:
我正在使用 Django 1.4 的 LiveServerTestCase 进行 Selenium 测试,但在使用 setUpClass 类方法时遇到了问题。据我了解MembershipTests.setUpClass 在单元测试运行之前运行一次。
我已经在MembershipTests.setUpClass 中添加了将用户添加到数据库的代码,但是当我运行MembershipTests.test_signup 测试时,没有将用户添加到测试数据库中。 我做错了什么?我希望我在 setUpClass 中创建的用户在所有单元测试中都可用。
如果我将用户创建代码放在MembershipTests.setUp 并运行MembershipTests.test_signup 我可以看到用户,但不希望在每个单元测试之前运行这个setUp。如您所见,我使用自定义 LiveServerTestCase 类在我的所有测试中添加基本功能 (test_utils.CustomLiveTestCase)。我怀疑这与我的问题有关。
提前致谢。
test_utils.py:
from selenium.webdriver.firefox.webdriver import WebDriver
from django.test import LiveServerTestCase
class CustomLiveTestCase(LiveServerTestCase):
@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',
]
def setUp(self):
pass
@classmethod
def setUpClass(cls):
super(MembershipTests, cls).setUpClass()
user = User.objects.create_user(
TEST_USER_USERNAME,
TEST_USER_EMAIL,
TEST_USER_PASSWORD
)
def test_signup(self):
print "users: ", User.objects.all()
【问题讨论】:
标签: python django selenium django-testing