使用远程认证进行单元测试 [英] Unit testing with remote authentication

查看:276
本文介绍了使用远程认证进行单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序使用Django的默认身份验证时,我写了一套测试,但现在我添加了Atlassian Crowd作为身份验证方法,这些测试现在失败了,主要是因为Crowd服务器不在那里想要从家里运行我的测试。



每个应用程序都有它的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.

这篇关于使用远程认证进行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆