Active TopicsActive Topics  Display List of Forum MembersMemberlist  CalendarCalendar  Search The ForumSearch  HelpHelp
  RegisterRegister  LoginLogin


 One Stop Testing ForumSoftware Testing @ OneStopTestingTest Cases @ OneStopTesting

Message Icon Topic: Interview Tip: Writing test cases

Post Reply Post New Topic
Author Message
Pallavi
Newbie
Newbie


Joined: 23Feb2007
Online Status: Offline
Posts: 1
Quote Pallavi Replybullet Topic: Interview Tip: Writing test cases
    Posted: 23Feb2007 at 8:55am

If during a dev interview, you're asked to write a set of test cases for some function you wrote, be sure to have test cases that reach every basic block. In other words, make sure your test cases at least hit every line of code you wrote.

For example, if you need to write 'bool IsEven(int x)' which returns true iff x is even, and you write something like this:

1: bool IsEven(int x)
2: {
3:    if (x & 1)
4:        return false; // odd
5:    else
6:        return false;  // even:   bug here!! Should return true
7: }

It's bad enough that you have a bug in line 6.

But then if the followup interview question is "Write some test cases for your function", and your test cases are:
    assert(IsEven(7) == false)
    assert(IsEven(3) == false)

You're doubley doomed.

Other quick rules for test cases. At least ensure your test cases:

  1. Hit every line of code you wrote
  2. Test corner cases such as  0, null, recursion base cases, large values, etc
  3. expose bugs in alternative buggy implementations. For example, if you choose not to use implementation X because it has bug Y, then include a test case that hits bug Y.
  4. hit Error cases if applicable.

 

I think some good test cases for IsEven above would be:

assert(IsEven(0) == true)   // 0 is a common corner case
assert(IsEven(-1) == false) // negatives are a good corner case.
assert(IsEven(-2) == true) // test negative  numbers
assert(IsEven(5) == false)  //
assert(IsEven(18) == true)  //
assert(IsEven(3000000) == true) // A recursive implementation would probably hit a stack overflow here.
assert(IsEven(3000001) == false) // A recursive implementation would probably hit a stack overflow here

Bonus points: more complex test cases.
The tests above are all "baseline tests" that take an input and match it against an expected output. This has the challenge of manually picking good numbers for the baseline. 

If the baseline tests are all the interviewer is interested in, then you're good with the above. If that satisfies the interview, great! Move on to the next question. (related Interview tip: don't wast the interviewer's time)

But if you're asked something like "how else could you test this to be super confident that it's really correct in all case", you could have some fun with more complex tests that use properties / identities of your function.  For example, you know that:
    assert(IsEven(x) == !IsEven(x+1))

Thus you could write a test that iterates through all possible inputs and verifies it against all outputs. Toss in that your function is stateless and now your talking about empirically provably correctness. That's sure to get some bonus points in an interview. :)

 




Post Resume: Click here to Upload your Resume & Apply for Jobs

IP IP Logged
somanianand
Newbie
Newbie


Joined: 07May2007
Location: India
Online Status: Offline
Posts: 1
Quote somanianand Replybullet Posted: 07May2007 at 11:14pm
Testcase should be :
1. only single purpose.
2. not more complex. easy steps.
3. Starting & end point should be same.
4. Self understanded.
5. with all commits
6. variable name should understandable like "int abc" - > int i_abc
7. testcase should clear & reusable.
8. testcasse should maintainable.
IP IP Logged
Post Reply Post New Topic
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum



This page was generated in 1.805 seconds.
Vyom is an ISO 9001:2000 Certified Organization

© Vyom Technosoft Pvt. Ltd. All Rights Reserved.

Privacy Policy | Terms and Conditions
Job Interview Questions | Placement Papers | Free SMS | Freshers Jobs | MBA Forum | Learn SAP | Web Hosting