Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在Django功能测试中触发空值验证?_Python_Django_Tdd - Fatal编程技术网

Python 如何在Django功能测试中触发空值验证?

Python 如何在Django功能测试中触发空值验证?,python,django,tdd,Python,Django,Tdd,我是Django的新手,现在正在阅读“使用Python进行测试驱动开发”的学习手册。 但是,当我想要测试空值验证时,单元测试是可以的,但是功能测试失败了 我现在使用的是Django 2.1,Python3。我已经尽力检查了问题的原因,但是仍然没有通过功能测试 models.py ... class Item(models.Model): text = models.TextField(default='') list = models.ForeignKey(List, defau

我是Django的新手,现在正在阅读“使用Python进行测试驱动开发”的学习手册。 但是,当我想要测试空值验证时,单元测试是可以的,但是功能测试失败了

我现在使用的是Django 2.1,Python3。我已经尽力检查了问题的原因,但是仍然没有通过功能测试

models.py

...
class Item(models.Model):
    text = models.TextField(default='')
    list = models.ForeignKey(List, default=None, on_delete=models.CASCADE)
views.py

...
def new_list(request):
    list_ = List.objects.create()
    item = Item.objects.create(text=request.POST['item_text'],list=list_)
    try:
        item.full_clean()
        item.save()
    except ValidationError:
        list_.delete()
        error = "You can't have an empty list item"
        return render(request, 'home.html', {"error": error})

    return redirect(list_)
test_views.py(单元测试成功)

测试列表项目验证.py(功能测试失败)

base.html(所有html文件都从此扩展)

。。。
{%if错误%}
{{error}}
{%endif%}

我希望单元测试和功能测试都是正确的,但实际结果是单元测试通过了,但功能测试失败了。这本书很棒。我自己读。我相信这是你缺少的部分:

def test_cannot_add_empty_list_items(self):
    # Edith goes to the home page and accidentally tries to submit
    # an empty list item. She hits Enter on the empty input box
    self.browser.get(self.live_server_url)
    self.get_item_input_box().send_keys(Keys.ENTER)

    '''
    If you keep reading a little farther, it should tell you that modern browsers
    will not submit forms when fields that are 'required' left are blank.
    '''
    # The browser intercepts the request, and does not load the
    # list page
    self.wait_for(lambda: self.browser.find_elements_by_css_selector(
        '#id_text:invalid'  
    ))
如果你继续往下读,它会告诉你,“当
必填字段保留为空时,现代浏览器不会提交
表单
。它会给你上面的代码修改

...
def test_cannot_add_empty_list_items(self):
    self.browser.get(self.live_server_url)
    self.browser.find_element_by_id('id_new_item').send_keys(Keys.ENTER)
    error = self.browser.find_element_by_css_selector('.has-error')
    self.assertEqual(error.text, "You can't have an empty list item")
...
{% if error %}
    <div class="form-group has-error">
        <span class="help-block">{{ error }}</span>
    </div>
{% endif %}
def test_cannot_add_empty_list_items(self):
    # Edith goes to the home page and accidentally tries to submit
    # an empty list item. She hits Enter on the empty input box
    self.browser.get(self.live_server_url)
    self.get_item_input_box().send_keys(Keys.ENTER)

    '''
    If you keep reading a little farther, it should tell you that modern browsers
    will not submit forms when fields that are 'required' left are blank.
    '''
    # The browser intercepts the request, and does not load the
    # list page
    self.wait_for(lambda: self.browser.find_elements_by_css_selector(
        '#id_text:invalid'  
    ))