开发者

Unit Test Design

few days back i wrote a code as a part of my interview process. The question was given a text find if a query text is present in the given text or not. I used hashtable to save the given text (keys were the words present in the text and value was the position of that word in the text). So now given the query string, I could find the location of the words present in the text and display a snippet of the text with maximum query words in it. I thought all was fine till now.

But i was also asked to write unit test for it. Though i had never written unit tests before, but i knew why they are so needed in the development process. So i created some test cases keeping in mind the average test case as well as the boundary case. But what wasnt clear to me was do we need to know the correct output beforehand in order to write the test case or not.

I started by getting some texts for the input to the program, and the corresponding output, put them in a class and later read them as input for my program.

The unit test code is shown below:

import unittest
import random
import generate_random_test
class c_Known_Output():
Input_Text1 = '''We ordered the traditional deep dish pizza and a Manchego salad. Were started off with a complimentary bread, that looks like a really big hamburger bun top at first glance. Even though it was free bread, it was soft and slightly sweet and delicious. I liked dipping it in the balsamic reduction and olive oil from the salad plate. The salad dish was perfectly fine, but I wish the Manchego slices on top were somehow sliced a bit thinner. The deep dish traditional pizza came out a bit later (remember the 40 min. cooking time, folks!), piping hot and smelling delicious. At first bite, I wasnt sure how much I liked it.'''

Output_Text1 = '''I liked dipping it in the balsamic reduction and olive oil from the salad plate. The salad [[HIGHLIGHT]]dish[[ENDHIGHLIGHT]] was perfectly fine, but I wish the Manchego slices on top were somehow sliced a bit thinner. The [[HIGHLIGHT]]deep dish[[ENDHIGHLIGHT]] traditional pizza came out a bit later (remember the 40 min. cooking time, folks!), piping hot and smelling delicious.'''

Input_Text2 = '''Best tacos I have ever had Lived down the road from this truck for years. Watched almost every episode of BSG eating these tacos with beer. Moved to Az and El Chato is one of the things I miss the most! ANYONE that is around them, you have to go here.'''

Output_Text2 = '''Best [[HIGHLIGHT]]tacos[[ENDHIGHLIGHT]] I have ever had Lived down the road from this truck for years. Watched almost every episode of BSG eating these [[HIGHLIGHT]]tacos[[ENDHIGHLIGHT]] with beer. Moved to Az and El Chato is one of the things I miss the most!'''

Query_Not_found = '''Query Not Found'''


class c_myTest( unittest.TestCase ):
Generator = generate_random_test.TestCaseGenerator()
KnowOutput = c_Known_Output()

def testAverageCase1(self):
    """no keywords present...no highlight"""
    output = highlight.m_Highlight_doc( self.KnowOutput.Input_Text1, 'deep dish')
    print "\nTest Case 1"
    print output
    self.assertEqual(output, self.KnowOutput.Output_Text1)

def testAverageCase2(self):
    output = highlight.m_Highlight_doc( self.KnowOutput.Input_Text2, 'burrito taco take out')
    print "\nTest Case 2"
    print output
    self.assertEqual(output, self.KnowOutput.Output_Text2)

def testSnippetLength(self):
    """ if the search word is present only once in the text...check if the snippet is of optimum length...optimum length is defined as one sentence before
    and after the sentence in which the query word is present"""
    output = highlight.m_Highlight_doc( self.KnowOutput.Input_Text3, 'tacos')
    print "\nTest Case 3"
    print output
    self.assertEqual(output, self.KnowOutput.Output_Text3)

def testSmallText(self):
    """The text is just one sentence, with the query present in it. The same sentence should be the output"""
    output = highlight.m_Highlight_doc( self.KnowOutput.Input_Text4, 'deep dish pizzas')
    print "\nTest Case 4"
    print output
    self.assertEqual(output, self.KnowOutput.Output_Text4)

def testBadInput(self):
    """no keywords present...no highlight"""
    output = highlight.m_Highlight_doc( self.KnowOutput.Input_Text4, 'tacos')
    print "\nTest Case 5"
    print output
    self.assertEqual(output, self.KnowOutput.Query_Not_found)

#Now test with randomly generated text
def testDistantKeywords(self):
    """the search queries are very distant in the text. 6 query words are generated. First 4 of these queries are inserted in one paragraph and the last two
    queries are inserted in another. The snippet should be of the first paragraph which has the maximum number of query words present in it."""
    query = self.Generator.generateSentence(6, 5)
    text1 = self.Generator.generateTextwithQuery( query[0:4], 10, 10, 5, 3 )
    text2 = self.Generator.generateTextwithQuery( query[5:], 10, 10, 5, 3 )
    text1.append('\n')
    text1.extend(text2)
    print "\nTest Case 6"
    print "=========================TEXT=================="
    print ' '.join(text1)
    print "========================QUERY=================="
    print ' '.join(query)
    print " "
    output_text = highlight.m_Highlight_doc( ' '.join(text1), ' '.join(query))
    print "=======================SNIPPET================="
    print output_text
    print " "


开发者_JAVA百科if __name__=='__main__':
    unittest.main()

Clearly i flunked and no reason was given to me and now i am trying to figure out if this code was the ONE. Could someone help me identify the problems in the unit tests what should one do if u do know the output of your code beforehand ans have to write unit tests for it. for example, can we write unit tests for a random number generator?

Thanks in advance!!!


I'd say if you know what your code is supposed to do, then you can write a unit test. In your test search case, I think it is safe to say that you can identify the expected output for a set of given inputs. Your interviewer might have had issues more with the coding of the text matcher and tests than with the principles you used. For a random number generator, yes, you can test it, as long as you remember that in computing you only have pseudo-random number generators. Realistic, useful things that you can test that spring to mind are that the generator produce the same output for the same seed, and that the period be no shorter than whatever you have defined it to be. You may or may not care that a given seed produces an pre-established sequence. This should be reflected in the test suite and the documentation.

My approach is to start with the tests and write the code such that they all pass (see test driven development). Not only does it give you good test coverage, but it helps define the code's functionality before you write it.


The current approach in software development suggests that almost any piece of software can and should be tested. Google have a lot of answers regarding testing random number generators. See http://en.wikipedia.org/wiki/Software_testability and http://www.google.com/search?q=testing+random+number+generators

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜