2

I am a beginner for writing test-cases so i don't know about test-cases, can you briefly explain about test-cases and also, how can i write test-cases for my c++ program(classes,member functions,member variables,....) help me in writing those and compiling and running it.

Advance in thanks

Bhargava
  • 301
  • 5
  • 13

2 Answers2

9

First of all:

  1. First find your self a framework of your liking: wikipedia list of frameworks here
  2. Install and read its documentation
  3. Identify the invariants, valid and invalid input of your methods and write tests that makes sure that they are enforced. My recommendation is to write tests that test in a black box way. I.e. you do not care how the methods does it, but you check that the results are ok with good input (corner cases and a normal case) and also that it fails in a good way when the input is bad.
  4. This answer elaborates on how to write good tests

Some general advice:

  • Write tests that will either fail or not, writing tests that probably will fail when something is wrong is a pita.
  • Never trust a test that you haven't seen fail.
  • Write one test at the time
  • Try to keep the testcases as autonomous as possible. Make sure that you test the code submitted to testing, not it's infrastructure. Unit testing is a great way to make sure that your classes depends on interfaces.
  • It is most often a bad idea to test implementation details of a method. You do not want tests to fail when someone rewrites the code correctly. If you feel the need to test the implementation you probably have another class hiding in the code that also should be unittested.
Community
  • 1
  • 1
daramarak
  • 6,115
  • 1
  • 31
  • 50
  • I do not agree to this sentence "It is most often a bad idea to test implementation details of a method." . It is false if you are providing an infrastructure API, when you change the implementation (for performance issue as an example) you may change involuntary the behavior. Constraining the test in this case is an easy way to know that the upward compatibility is broken. – VGE Feb 03 '11 at 07:45
  • @VGE Well then we disagree, I will say that in most cases when you have the situation you describe you should have another class under test that provide the implementation detail. Ofcourse unless this is hampering your performance, but that is why I say "most often". – daramarak Feb 07 '11 at 14:37
1

Cxxtest is useful indeed.

sud03r
  • 19,109
  • 16
  • 77
  • 96