title: Basic unit test and C, how do I get started?
body: <p>After reading quite some threads here at StackOverflow, 
  I have come to the conclusion that I should adopt to some form of 
  test driven development/unit test (or at least explore the area).</p>
  
  <p>And since we are talking about c code under Linux, 
  I decided to give <a href="http://check.sourceforge.net/doc/check.html/index.html" rel="nofollow noreferrer">check</a> a try 
  (I don't know if this is the right choice but if it's no good I can always try something else later on).</p>
  
  <p>But since this concept of unit test and unit test frameworks is totally new to me, 
  I started out to do some unit test on a really small test code (but I was totally lost anyway and it felt like I was missing something).</p>
  
  <p>This is what I have done so far, I created the following file:</p>
  
  <ul>
  <li>main.c, a main that only calls a function called my_pow and prints the result.</li>
  <li>my_pow.c, contains the function my_pow.</li>
  <li>my_pow.h  </li>
  <li>my_pow_test.c, I figured that I should place the unit code for the my_pow function here.</li>
  </ul>
  
  <p>(So the "normal program" is the main.c, my_pow.c and my_pow.h.)</p>
  
  <p>This is my_pow.c</p>
  
  <pre>
  <code>
  #include "my_pow.h"
  int my_pow(int a, int b)
  {
      return (a*b);
  }
  </code>
  </pre>
  
  <p>Then I figured that in my_pow_test.c I put something like this:</p>
  
  <pre>
  <code>
  #include &lt;check.h&gt;
  #include "my_pow.h"
  
  START_TEST (test_my_pow)
  {
      /* unit test code */
  }
  END_TEST
  
  //do I need some sort off main here that calls test_my_pow?
  </code>
  </pre>
  
  <p>This is basically the same as in <a href="http://check.sourceforge.net/doc/check.html/index.html" rel="nofollow noreferrer">the check manual</a> chapter 3.1, 
  but still not....</p>
  
  <p>Could someone please push me in the right direction?</p>
  
  <p>Thanks 
  Johan</p>
  
  <hr>
  
  <p>Update: No reason why I tried to use check I just thought I should start somewhere,
  maybe CUnit is a better choice (I think I would try that as well and then make a educated choice).</p>
  
  <p>Update: Thanks @philippe for indirectly pointing out that the on-line documentation is only half of the truth, 
  the example code that clarifies what the documentation talks about was already installed with the check package.
  In the Ubuntu case /usr/share/doc/check/example/tests/</p>
  
  <p>Update: The code example was created so that you started out by looking at his first version, 
  then the second one etc etc. So that you could follow how he creates a very basic test case/code from nothing up to something that is useful in a traditional TTD way. </p>
  
  <p>And since my code was broken and I wanted the unit test to prove this, 
  I cheated a little and tested against the real pow function.
  Something like this: </p>
  
  <pre>
  <code>
  START_TEST (test_my_pow1)
  {
      int resultat = my_pow(3,3);
      int math     = pow(3,3);
      fail_unless ( resultat == math,
             "Error on 3^3 != %d (%d)",math, resultat);
  }
  </code>
  </pre>
  
  <p>However in the future I will not reproduce what is already in the stdlibs :-)</p>
  
  <hr>
  
  <p>Related:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/65820/unit-testing-c-code">Unit Testing C Code</a></li>
  <li><a href="https://stackoverflow.com/questions/148576/how-to-test-c-code">How to test C Code</a></li>
  <li><a href="https://stackoverflow.com/questions/177251/unit-testing-frameworks-for-c">Unit Testing Frameworks for C</a></li>
  <li><a href="https://stackoverflow.com/questions/437233/testing-frameworks-for-c">Testing Frameworks for C</a></li>
  <li><a href="https://stackoverflow.com/questions/453141/basic-unit-test-and-c-how-do-i-get-started">Basic unit test and C, how do I get started?</a></li>
  </ul>
  
  <p>taken from searching <code>[c] [unit-testing]</code>.</p>