Django测试:测试表单域的初始值 [英] Django testing: Test the initial value of a form field

查看:114
本文介绍了Django测试:测试表单域的初始值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个观点,即应设置用于基于GET值的表单字段的初始值。我想测试一下我目前正在使用 Django的测试客户端,但我可以查看其他工具。

I have a view that should be setting an initial value for a form field based on a GET value. I want to test this. I'm currently using Django's test client but I am open to looking at other tools.

对不起,我没有提到我很清楚<一个href =http://docs.djangoproject.com/en/dev/topics/testing/#django.test.TestCase.assertContains> assertContains 方法,但我希望有一个比搜索更好的方法该HTML用于输入标记和属性。

Sorry, I did not mention that I am well aware of the assertContains method but I was hoping there was a better way other than searching the HTML for an input tag and the value attribute.

推荐答案

恨回答我自己的问题(像我第三次这样做),但在嘲笑测试客户端之后,我找到了一个更好的方法:

Hate to answer my own question (like the 3rd time I've done it) but after mocking around with the test client, I've found a better way:

def test_creating_stop(self):
    c = self.client

    # Check that name is pre-filled
    response = c.get('%s?name=abcd' % reverse('add_new_stop'))
    self.assertEqual(response.context['form'].initial['name'], 'abcd')



有谁看到什么错呢?我会留下一段时间看看人们的想法。

Does anyone see anything wrong with this? I'll leave it up for a while see what people think.

这篇关于Django测试:测试表单域的初始值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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