使用远程认证进行单元测试

编程入门 行业动态 更新时间:2024-10-26 14:32:47
本文介绍了使用远程认证进行单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我的应用程序使用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.

更多推荐

使用远程认证进行单元测试

本文发布于:2023-06-13 09:29:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/678899.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单元测试

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!