在测试仪中获取错误消息... 5/8正确

时间:2013-04-01 15:11:08

标签: python testing filter triggers counter

我正在运行一个过滤故事并返回已过滤故事数量的函数。代码如下 - 测试中的错误低于该值。错误似乎只是在计算过滤器故事的数量上计算错误 - 简单错误但在尝试解决它时遇到了问题。过滤的故事数量不匹配。请帮忙。

我的代码:

def filterStories(stories, triggerlist):

   newstories = []
   for story in stories:
       for trigger in triggerlist:
           if trigger.evaluate(story) == True:
               newstories.append(story)

       stories = newstories
   return stories

class WordTrigger(Trigger):
    def __init__(self,word):
        self.word = word.lower()

    def changeChars(self,text):
        final = ""
        for c in text:
            if c in string.punctuation:
                final += " "
            else:
                final += c
        return final

    def isWordIn(self,text):
        comp = self.changeChars(text)
        list = comp.lower().split(" ")
        return self.word in list

# TODO: TitleTrigger
class TitleTrigger(WordTrigger):
    def evaluate(self,story):
        return self.isWordIn(story.getTitle())

# TODO: SubjectTrigger
class SubjectTrigger(WordTrigger):
    def evaluate(self,story):
        return self.isWordIn(story.getSubject())

# TODO: SummaryTrigger
class SummaryTrigger(WordTrigger):
    def evaluate(self,story):
        return self.isWordIn(story.getSummary())

class PhraseTrigger(Trigger):
    def __init__(self,phrase):
        self.phrase = phrase

    def evaluate(self,story):
        return self.phrase in story.getTitle() or self.phrase in story.getSubject() or self.phrase in story.getSummary() 

-----------------来自测试的错误信息-------------------------- -----------------------

Test: filterStories 3
Your output:
Trigger 1: TrueTrigger() - always returns True
Trigger 2: TrueTrigger() - always returns True
Story with title: "MIT and Russia Partner with Skolkovo Tech" and subject: "New school located near Moscow"
In filteredStories?  True
Story with title: "Russian dignitaries Visit USA" and subject: "Obama meets with Russian President Vladimir Putin"
In filteredStories?  True
Story with title: "Russia Overrun With Friendly Tigers" and subject: "Putin poses for photo op with cuddly felines"
In filteredStories?  True
Story with title: "Mitt Romney Defeated" and subject: "Obama edges win over Republican challenger Mitt Romney"
In filteredStories?  True
Number of filtered stories:  8
Story titles in the list of filtered stories your code generated:
['MIT and Russia Partner with Skolkovo Tech', 'MIT and Russia Partner with Skolkovo Tech', 'Mitt Romney Defeated', 'Mitt Romney Defeated', 'Russia Overrun With Friendly Tigers', 'Russia Overrun With Friendly Tigers', 'Russian dignitaries Visit USA', 'Russian dignitaries Visit USA']
Correct output:
Trigger 1: TrueTrigger() - always returns True
Trigger 2: TrueTrigger() - always returns True
Story with title: "MIT and Russia Partner with Skolkovo Tech" and subject: "New school located near Moscow"
In filteredStories?  True
Story with title: "Russian dignitaries Visit USA" and subject: "Obama meets with Russian President Vladimir Putin"
In filteredStories?  True
Story with title: "Russia Overrun With Friendly Tigers" and subject: "Putin poses for photo op with cuddly felines"
In filteredStories?  True
Story with title: "Mitt Romney Defeated" and subject: "Obama edges win over Republican challenger Mitt Romney"
In filteredStories?  True
Number of filtered stories:  4
Story titles in the list of filtered stories your code generated:
['MIT and Russia Partner with Skolkovo Tech', 'Mitt Romney Defeated', 'Russia Overrun With Friendly Tigers', 'Russian dignitaries Visit USA']
Test: filterStories 4
Your output:
Trigger 1: TitleTrigger("Russia")
Trigger 2: SubjectTrigger("Putin")
Trigger 3: TitleTrigger("MIT")
Story with title: "MIT and Russia Partner with Skolkovo Tech" and subject: "New school located near Moscow"
In filteredStories?  True
Story with title: "Russian dignitaries Visit USA" and subject: "Obama meets with Russian President Vladimir Putin"
In filteredStories?  True
Story with title: "Russia Overrun With Friendly Tigers" and subject: "Putin poses for photo op with cuddly felines"
In filteredStories?  True
Story with title: "Mitt Romney Defeated" and subject: "Obama edges win over Republican challenger Mitt Romney"
In filteredStories?  False
Number of filtered stories:  5
Story titles in the list of filtered stories your code generated:
['MIT and Russia Partner with Skolkovo Tech', 'MIT and Russia Partner with Skolkovo Tech', 'Russia Overrun With Friendly Tigers', 'Russia Overrun With Friendly Tigers', 'Russian dignitaries Visit USA']
Correct output:
Trigger 1: TitleTrigger("Russia")
Trigger 2: SubjectTrigger("Putin")
Trigger 3: TitleTrigger("MIT")
Story with title: "MIT and Russia Partner with Skolkovo Tech" and subject: "New school located near Moscow"
In filteredStories?  True
Story with title: "Russian dignitaries Visit USA" and subject: "Obama meets with Russian President Vladimir Putin"
In filteredStories?  True
Story with title: "Russia Overrun With Friendly Tigers" and subject: "Putin poses for photo op with cuddly felines"
In filteredStories?  True
Story with title: "Mitt Romney Defeated" and subject: "Obama edges win over Republican challenger Mitt Romney"
In filteredStories?  False
Number of filtered stories:  3
Story titles in the list of filtered stories your code generated:
['MIT and Russia Partner with Skolkovo Tech', 'Russia Overrun With Friendly Tigers', 'Russian dignitaries Visit USA']

1 个答案:

答案 0 :(得分:0)

在将您的故事插入filterstories之前,只需在您的函数filterlist中添加以下测试:

   for trigger in triggerlist:
       if trigger.evaluate(story) == True:
           if story not in newstories:
               newstories.append(story)

   stories = newstories

应该工作。

实际上,您正在复制导致测试失败的条目。