-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unit testing for c code in simple way #47
Comments
Hi Alessio! Thanks for the raising the issue. By 'Unit testing' we mean testing small pieces of code, possibly functions or collections of functions under different input conditions. There are a number of advantages to adopting Unit test frameworks when testing code. They encourage modularity thereby promoting separation of concerns; we can write tests on functions, groups of functions and organise tests together. In addition, unit testing frameworks come with many helper functions (many more than simply asserts) including checks on floating point values, inequalities, fixtures (keeping data for multiple tests), which have all been well tested for us. What is suggested above is more akin to writing assertions than unit tests and runs the risk that it may make the code more difficult to read due to extra complexity and length. I think that undertaking testing in this way would likely miss some of the benefits of unit testing described above. The suggestion of uTest is a good one. I propose that I experiment with uTest in the ONEFlux pipeline first and submit a PR for you to review? I can then demonstrate how and why a unit testing framework has advantages. This will also give me an opportunity to assess the differences between Check and uTest and make a more informed opinion about which we should use in future. I believe Check is more feature rich which may prove useful down the line but it would probably be useful to have a discussion at the development meetings around this. We can summarise the outcome in this issue thread. |
Hi, as per the agreements made at our last meeting, I report a small comparative analysis between "check" and "utest.h" unit test libraries. A brief summary:
check: https://libcheck.github.io/check/
utest.h: public domain
check: a full library that needs to be compiled
Let's analyze a small example that is taken from check test package:
Suite and SRunner are two data types of check library. Let's see these tests:
these tests are simple and need no explanation. Let's see how these tests are called:
Summing up this function create one suite called Money with two test cases called Core and Limits Again TCase is a check data type used to create a named test case.
We can use the fixtured testcase with utest, from the documentation:
Running the example we have:
We can rewrite that example using _utest_in this way:
if we compile it and run it with
we'll have
if we change line 70 slightly comparing 4 instead of 5 so that the test fails, we have
with check library we have
As you can see in this case it is quite simple to use the utest library but obviously not all the features of the check library have been compared. |
Hi,
I want to share you some info on how we can add some checking (unit testing) to existing code.
There are many unit testing libraries floating around the web but I think for our needs we can use a C features to mimic unit testing usage: macros.
In C language, we can have parts of code that can be enabled by defining a symbol (keyword):
if the compiler during compilation will find ENABLE_TEST defined, the code inside the block will be compiled.
Let's make a working example:
if we compile it with gcc:
gcc main.c && ./a.out
we have as output:
the value of a is 5
but if we compile it declaring ENABLE_TEST definition using -D parameter
gcc -DENABLE_TEST main.c && ./a.out
we'll have
As you can see, we can easily enable or disable this kind of checking.
Let's do a better example:
In this example the macro TEST will evaluate only if ENABLE_TEST is defined, otherwise nothing happens.
Again if we compile and run it
gcc -DENABLE_TEST main.c && ./a.out
we have
We can eventually adopt this to encapsulated a unit testing library too:
Any ideas ?
Otherwise this seems like a good candidate for unit testing: https://github.com/sheredom/utest.h
The text was updated successfully, but these errors were encountered: