TDD Django 测试似乎跳过了视图代码的某些部分



我正在使用django TDD为一个网站编写一些测试。

问题是当我手动转到测试服务器时。填写表格并提交,然后它似乎工作正常。但是当我使用测试 wiki 运行测试时 manage.py 它似乎跳过了视图中的部分代码。页面部分似乎都工作正常。但是代码中的pagemod部分,甚至是我创建的write()只是为了看看发生了什么,似乎都被忽略了。

我不知道是什么原因造成的,似乎找不到解决方案。有什么想法吗?

这是代码:

test.py

#imports
class WikiSiteTest(LiveServerTestCase):
....
def test_wiki_links(self):
    '''Go to the site, and check a few links'''
    #creating a few objects which will be used later
    .....
    #some code to get to where I want:
    .....
    #testing the link to see if the tester can add pages
    link = self.browser.find_element_by_link_text('Add page (for testing only. delete this later)')
    link.click()
    #filling in the form
    template_field = self.browser.find_element_by_name('template')
    template_field.send_keys('homepage')
    slug_field = self.browser.find_element_by_name('slug')
    slug_field.send_keys('this-is-a-slug')
    title_field = self.browser.find_element_by_name('title')
    title_field.send_keys('this is a title')
    meta_field = self.browser.find_element_by_name('meta_description')
    meta_field.send_keys('this is a meta')
    content_field = self.browser.find_element_by_name('content')
    content_field.send_keys('this is content')
    #submitting the filled form so that it can be processed
    s_button = self.browser.find_element_by_css_selector("input[value='Submit']")
    s_button.click() 
    # now the view is called

和视图:

views.py

def page_add(request):
'''This function does one of these 3 things:
    - Prepares an empty form
    - Checks the formdata it got. If its ok then it will save it and create and save
      a copy in the form of a Pagemodification.
    - Checks the formdata it got. If its not ok then it will redirect the user back'''
.....
if request.method == 'POST':
    form = PageForm(request.POST)
    if form.is_valid():
        user = request.user.get_profile()
        page = form.save(commit=False)
        page.partner = user.partner
        page.save() #works
        #Gets ignored
        pagemod = PageModification() 
        pagemod.template = page.template
        pagemod.parent = page.parent 
        pagemod.page = Page.objects.get(slug=page.slug)
        pagemod.title = page.title
        pagemod.meta_description = page.meta_description
        pagemod.content = page.content
        pagemod.author = request.user.get_profile()
        pagemod.save()
        f = open("/location/log.txt", "w", True)
        f.write('are you reaching this line?')
        f.close()
        #/gets ignored
        #a render to response

然后后来我做了:

test.py

print '###############Data check##################'
print Page.objects.all()
print PageModification.objects.all()
print '###############End data check##############'

并获得:

终端:

###############Data check##################
[<Page: this is a title 2012-10-01 14:39:21.739966+00:00>]
[]
###############End data check##############

所有的进口都很好。将 page.save() 放在被忽略的代码之后没有任何区别。这仅在通过 TDD 测试运行时发生。

提前谢谢。

多么奇怪。 可能是视图在Pagemodification阶段出现了某种错误吗? 您是否在测试的后期进行了任何检查,断言来自视图的响应正确通过,即没有返回 500 错误?

这是很久以前的事了。

解决了,但解决方案有点尴尬。基本上,是我愚蠢。我不记得确切的细节,但我相信调用了不同的视图,而不是我在这里展示的视图。该视图具有相同的代码,但"跳过"部分除外。

我向任何花时间研究此事的人道歉。

相关内容

最新更新