Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Unit Testing based on fork()

So I'm interested in doing some unit testing of a library that interacts with a kernel module. To do this properly, I'd like to make sure that things like file handles are closed at the end of each test. The only way this really seems possible is by using fork() on each test case. Is there any pre-existing unit test framework that would automate this?

An example of what I would expect is as follows:

TEST() {
    int x = open("/dev/custom_file_handle");
    TEST_ASSERT_EQUAL(x, 3);
}

TEST() {
    int y = open("/dev/other_file_handle");
    TEST_ASSERT_EQUAL(x, 3);
}

In particular, after each test, the file handles were closed, which means that the file descriptor should likely be the same value after each test.

I am not actually testing the value of the file descriptor. This is just a simple example. In my particular case, only one user will be allowed to have the file descriptor open at any time.

This is targeting a Linux platform, but something cross platform would be awesome.

like image 771
Bill Lynch Avatar asked Oct 15 '25 19:10

Bill Lynch


2 Answers

Google Test does support forking the process in order to test it. But only as "exit" and/or "death" tests. On the other hand, there is nothing to prevent you from writing every test like that.

Ideally, though, I would recommend that you approach your problem differently. For example, using the same Google Test framework, you can list test cases and run them separately, so writing a simple wrapper that invokes each binary multiple times to run different test will solve your problem. Fork has its own problems, you know.

The Check unit testing library for C by default executes each test in a separate child process.

It also supports two different kinds of fixtures - ones that are executed before/after each test - in the child process - (called 'checked') and ones that are executed before/after a test-suite - in the parent process - (called 'unchecked' fixtures).

You can disable the forking via the environment variable CK_FORK=no or an API call - e.g. to simplify debugging an issue.

Currently, libcheck runs under Linux, Hurd, the BSDs, OSX and different kinds of Windows (mingw, non-mingw etc.).

like image 21
maxschlepzig Avatar answered Oct 18 '25 12:10

maxschlepzig



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!