views:

95

answers:

2

Hi folks! Big companies like Microsoft often ask you to write some function (for example reverse all words in the statement) and then write test cases for it. What is the best way to prepare for writing such test cases?

+5  A: 

The successful tests are usually the easiest to handle. It shouldn't take long to find a couple of simple examples that should work as expected. The thing to concentrate on are the unusual use cases - the edge cases. To do this you need to think of all the possible ways that the function could conceivably break and design a test case that would test that.

Example inputs for your specific question:

  • The null reference.
  • The empty string.
  • Only one word.
  • Unusual characters - symbols, foreign letters.
  • If you are fiddling bytes, make sure you have some multibyte encoded letters.
  • Different types of whitespace between words (space, tab, new-line, non-breaking whitespace, etc). See for example here for more types of whitespace.
  • Try with words containing symbols. Examples are shouldn't and non-breaking.
  • Try "words" that contain digits like usernames JoeBloggs123.
  • Try "words" that contain symbols like emoticons :-) or :-p.
  • What about Chinese text? In Chinese there are typically no spaces or other word-boundary tokens. You must understand the meaning of the symbols to determine where the words start and end.
  • Extremely large input texts.

Some more general guidelines that apply more widely:

  • Explore the entire input range.
  • Think of ways you can break things - don't forget about invalid inputs.
  • Consider the sort of errors people typically might make when implementing the function and use that as inspiration for test cases. You can see lots of examples of broken code on Stack Overflow.
  • Even if you don't know what the result should be, you should still create the test.
Mark Byers
+5  A: 

The best way to prepare? By writing actual test cases for real functions. Same as any other field of endeavour. Practice, practice and practice again.

Depending on the testing required, you will either:

  • base your tests on documented behaviour, testing that behaviour (success and failure); or
  • base your tests on internal knowledge, testing all the success and failure paths through the code.
paxdiablo