CMake Best Practices by Dominik Berner Mustafa Kemal Gilor

CMake Best Practices by Dominik Berner Mustafa Kemal Gilor

Author:Dominik Berner, Mustafa Kemal Gilor
Language: eng
Format: epub
Publisher: Packt Publishing Pvt. Ltd.
Published: 2022-05-12T00:00:00+00:00


Catch2 and GoogleTest are just two of the many testing frameworks out there; there might be more test suites that bring this functionality with them that might be unknown to the authors. Now, let's move on from finding tests and have a closer look at how to control test behavior.

Advanced ways to determine test success or failure

By default, CTest determines whether a test failed or passed based on the return value of the command. 0 means all tests were successful, anything other than 0 is interpreted as a failure.

Sometimes, the return value is not enough to determine whether a test passes or fails. If you need to check program output for a certain string, the FAIL_REGULAR_EXPRESSION and PASS_REGULAR_EXPRESSION test properties can be used, as shown in the following example:

set_tests_properties(some_test PROPERTIES

FAIL_REGULAR_EXPRESSION "[W|w]arning|[E|e]rror"

PASS_REGULAR_EXPRESSION "[S|s]uccess")

These properties would cause the some_test test to fail if the output contains either "Warning" or "Error". If the "Success" string is found, the test is considered passed. If PASS_REGULAR_EXPRESSION is set, the test is considered passed only if the string is present. In both cases, the return value will be ignored. If a certain return value of a test needs to be ignored, it can be passed with the SKIP_RETURN_CODE option.

Sometimes, a test is expected to fail. In those, setting WILL_FAIL to true will cause the test result to be inverted:

add_test(NAME SomeFailingTerst COMMAND SomeFailingTest)

set_tests_properties(SomeFailingTest PROPERTIES WILL_FAIL True)

This is often better than disabling the test because the test will still be executed on each test run, and if the test unexpectedly starts to pass again, the developer is made aware of it. A special case of test failures is when tests fail to return or take too much time to complete. For this case, CTest provides the means of adding timeouts of tests and even retrying tests in the case of failure.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.