Hello forum!
I'm trying to get my head around unit testing.
I chose this library, Catch: https://github.com/philsquared/Catch
I have a rather large project and I do not really understand on how to add tests to it.
Maybe I'm still misunderstand unit testing.
Should I store unit tests in extra files? When yes, why?
I thought I would do those once I obtain parameter values or execute a void and see if what I expected happened - checking whether code behaves as expected.
But it seems more of writing tests for a specific testing aspect in a different file for all possible cases:
TEST_CASE( "Factorials are computed", "[factorial]" ) {
REQUIRE( Factorial(1) == 1 );
REQUIRE( Factorial(2) == 2 );
REQUIRE( Factorial(3) == 6 );
REQUIRE( Factorial(10) == 3628800 );
}
Code excerpt from: https://github.com/philsquared/Catch/blob/master/docs/tutorial.md
Therefore, testing mathematical calculations is probably not a thing that should be tested for every possible outcome.
But testing whether my buffer-instance owns one instance after pushing one instance, would be a unit test?
Though, would I invoke them into the actual code or are they just residing in another file? And if yes, how do they actually interact then?
An example:
My "real" code that tries to solve a problem, runs a for-loop as often as needed (not known at compile time) and push one object per iteration onto the buffer class instance.
Now, it will be run seven times. How would a unit test look?
When it is in another file, it would be run first and only check whether adding one unit will add one unit to the buffer (assuming it owned 0 from its construction point). Another test would be checking on if the size of buffer is increasing after it reached it maximum capacity?
So that would be 2 unit tests.
I always thought I would just simply add REQUIRE( buffer_object.get_size() == i ); inside the for-loop.
I think, it would help a lot if you could show me a simple example on how to invoke unit testing into a tiny code example using a class and whatnot.
The greatest issue of understanding seems to be, where to place the unit tests? How to invoke them into code?