每个应用程序都有它的Setup()方法
def setUp(self):设置共享测试数据。 self.client.login(username ='admin',password ='letmein ')我正在通过评论AUTHENTICATION_BACKENDS来解决这个问题,但是,不要在CI服务器上工作。
我不认为我遇到的错误很重要,但是为了完整性:
URLError:< urlopen错误[Errno 8]提供nodename或servname,或者不知道>我已经尝试将认证后端添加到AUTHENTICATION_BACKENDS中,我仍然得到相同的结果。 >
我有什么选择让这些测试通过?
有没有办法强制用户登录?我可以嘲笑auth对象吗?
我可以在设置中对AUTHENTICATION_BACKENDS部分进行检查,以检查它是否在测试模式下运行?但是我正在为我的测试编写特殊情况,而且这种方式会使对象失败。
解决方案您可以更改 AUTHENTICATION_BACKENDS 设置在 setUp 方法中,然后在 tearDown 。这个问题的接受答案就是一个例子,但设置不同。
I have a suite of tests that I wrote while my app was using Django's default authentication, but now I've added Atlassian Crowd as the authentication method and those tests now fail, mainly because the Crowd server isn't there when I want to run my tests from home.
Each app has this in it's Setup() method
def setUp(self): """Set up the shared test data.""" self.client.login(username='admin', password='letmein')I'm working around it at the moment by commenting out the AUTHENTICATION_BACKENDS, but that isn't going to work on the CI server.
I don't think the error I'm getting is important, but for completeness:
URLError: <urlopen error [Errno 8] nodename nor servname provided, or not known>I've tried adding both auth backends into AUTHENTICATION_BACKENDS and I still get the same results.
What are my options for getting these tests to pass?
Is there any way to force the user to be logged in? Can I mock the auth object somehow?
Could I put some check around the AUTHENTICATION_BACKENDS section in setting to check if it's running in test mode? but then I'm writing special cases for my tests and that kind of defeats the object.
解决方案You could change the AUTHENTICATION_BACKENDS setting in the setUp method, then change it back in tearDown. This question's accepted answer has an example just that, but with a different setting.
更多推荐
使用远程认证进行单元测试
发布评论