我正在尝试测试当 session 中存储了某些数据时 View 的行为方式。为此,我在测试方法中创建了 session ,并在 View 的最开始调用了一个交互式 shell:
测试方法:
def test_user_with_unused_tests(self):
"User is given a test and sent to test start"
# todo: insure that the user is given a test that he hasn't done
#login
login = self.client.login(username='xxx', password='xxx')
self.failUnless(login)
# build the screener
user = User(username='xxx', password='xxx')
user_screener = UserScreener(user=user)
# put the screener in session
self.client.session['user_screener'] = user_screener
已测试的 View :
@login_required
def screener_start(request):
import code
code.interact(local=locals())
但显然 session 不会在我的测试方法和对 View 的调用之间持续存在:
不持久的证据:
>>> request.session.values()
[1, 'django.contrib.auth.backends.ModelBackend']
有什么办法可以解决这个问题吗?我错过了一些重要的东西吗?
我正在使用 Django 1.0。
提前感谢您的想法。
最佳答案
看起来您需要一个setUp
方法,请查找http://docs.python.org/library/unittest.html setUp
和 tearDown
方法文档。 setUp 将为所有测试运行,每个单独测试的代码将在每次测试运行结束时销毁。
本质上,您需要将登录逻辑放在 setUp
中,将实际测试逻辑放在测试方法中。
希望对你有帮助
关于python - Django 1.0 测试 : how do I get a session to persist between test code and view being tested?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1515446/