How to run unit tests for C/C++ code

My projects use “cmake && make && make install” process to build.
Executing unit tests can be don by invoking make test, as long as Makefile has been generated already.

I also provide a simple “build.sh” script for CI builds which takes care about creating build folders, calling cmake, make, make install and make test.

But how could I possible run one of these commands using “arc unit”?
I could not find anything in the docs.

Hi again,

I managed to write my own UnitTest subclass based on code I found here:

My code now executes ctest and parses the result.

The result looks promising (https://paste.pics/d7e6ca1171afa470c41bf8625f41ccc9), but what I don’t like is that the console is so long unresponsive while showing “Running unit tests…”.

This is because I run ‘ctest’ to run all unit tests at once using ExecFuture and afterwards parse the results.

I think it would be better to get a list of test cases and then execute them individually.
This way one could print each individual result using ArcanistUnitRenderer when it is finished.

Does somebody has an example for this? I’m neither a PHP nor arcanist expert.

this is the code that does most of the ctest execution so far:

        $future = new ExecFuture('ctest .');
        $future->setCWD($this->projectRoot . '/bld');

        list($builderror, $ctest_stdout, $ctest_stderr) = $future->resolve();

        if ($builderror !== 0) {
            return array(id(new ArcanistUnitTestResult())
                ->setName("CTest engine")
                ->setUserData($ctest_stderr)
                ->setResult(ArcanistUnitTestResult::RESULT_BROKEN));
        }

        $lines = explode("\n", $ctest_stdout);
        $results = array();
        foreach($lines AS $line) {
            $ret = preg_match("/(\d+)\/(\d+) Test +#\d+: (\w+) \.+ + (Passed|Failed) + ([0-9.]+) sec/", $line, $captures);
            if ($ret != 1) {
                continue;
            }

            $index    = $captures[1];
            $count    = $captures[2];
            $name     = $captures[3];
            $status   = $captures[4];
            $duration = floatval($captures[5]);

            $result = new ArcanistUnitTestResult();
            $result->setName($name);
            if ($status == 'Passed') {
                $result->setResult(ArcanistUnitTestResult::RESULT_PASS);
            } else {
                $result->setResult(ArcanistUnitTestResult::RESULT_FAIL);
            }
            $result->setDuration($duration);
            # print result
            #$this->renderer->renderUnitResult($result);

            array_push($results, $result);
        }
        return $results;

https://secure.phabricator.com/diffusion/ARC/browse/master/src/unit/engine/PhutilUnitTestEngine.php$128 has an example of a test engine guessing at which test files to execute (looks like it returns a list of classes). It’s heavily based around the code-base specific structure.

hi, thanks for your help.

This does not really answer my questions, but it answer another questions I had too :wink:

In this topic I wanted to know more about how I could make the intermediate results visible to the user, so that it is possible to see the progress of the tests. My current code executes all tests which can take some minutes, and then shows all results at once. This is not so pleasant for the user.

hi again,

I rethought the thing, and initially I wanted to start all test asynchronously using the futures and wait for them.
But actually this can have all kinds of weird side effects, so I implemented a much simpler solution.
Just calling one test after each other in a loop and show the progress.

It works :wink:
I push my work (still not ready for production) to github for the case you or somebody else is interested in this code.

1 Like