Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit Testing - How to go about it?

I want to unit test my program (in C) because I know of the benefits of doing so as well, as it shows where the problem is.

I also like to blackbox test, since it tells me if the program works (at least, for the tests).

At the moment, I am using Autotest (Which comes with Autoconf) in order to not add a dependency.

At this point, I wouldn't mind so much using a better framework, but the problem is I don't want to use a different framework for blackbox and unit tests. Would it be possible to run the blackbox tests from a unit test framework? The thing I really would like is good log output, saying exactly what went wrong and where.

My other option is to unit test with Autotest. The problem is there is no framework. I have written a small "test driver" that accepts the name of the function to test and arguments to pass to the function, and calls that function. The problem is that I am not sure what boundary to use between assertions and outputting the return value of the function (for logging purposes, since I like how Autotest will give me a diff). Since most functions return lists, it is quicker to prepare using the diff with expected output (expout using Autotest).

like image 415
alternative Avatar asked Nov 25 '09 23:11

alternative


People also ask

How do you do unit testing?

To get started, select a method, a type, or a namespace in the code editor in the project you want to test, right-click, and then choose Create Unit Tests. The Create Unit Tests dialog opens where you can configure how you want the tests to be created.

What is unit testing and how it is done?

Unit Testing is defined as a type of software testing where individual components of a software are tested. Unit Testing of the software product is carried out during the development of an application. An individual component may be either an individual function or a procedure.


1 Answers

Would it be possible to run the blackbox tests from a unit test framework?

Yes, you could invoke the Autotest with system() from the unit tests and then assert on the returned value.

But I wouldn't recommend doing that as unit tests are executed very often, they shall be very fast i.e. measured in seconds, not in minutes.

Unit tests and integration tests (which you call blackbox tests) serve different purposes: unit tests validate that the units in the code (whatever this means, function or clusters of function) works as expected by the tests, while integration tests cover the program end-to-end, validating it as a whole.

So, typically unit tests are ran after every few changes in the code, especially if you apply TDD, while integration tests are executed when a capability has been added.

I would rather have a typical unit-test program(s), with assertions, and an integration suite that would invoke the unit-tests in addition to your blackbox tests.

The problem is that I am not sure what boundary to use between assertions and outputting the return value of the function (for logging purposes, since I like how Autotest will give me a diff).

With assertions there is nothing to output: either the expected and the actual values are equal and nothing happens, or they are different and the UT framework prints out an error message (expected is X, actual is Y). That is one let the computer do the job of testing.

With logging output diff, one has still to manually (visually) inspect the outcome of the diff (for instance: is there one item missing in the list or one extra item ...).

Since most functions return lists, it is quicker to prepare using the diff with expected output (expout using Autotest).

You might want to write a function that compares lists using assertions.

like image 56
philant Avatar answered Nov 08 '22 21:11

philant