Skip to content

Running a single test from the unittests directory

Mandeep Singh Grang edited this page Jan 26, 2021 · 2 revisions

The tests in the llvm/unittests and the clang/test/Unit dirs are based on the Google Test Framework. This wiki details how to run a single tests from these dirs.

Let's assume we want to run the tests in the clang/test/Unit dir.

  1. First run the unit tests through ninja. Make sure you redirect the output to a log file as we will need this later.
ninja -v check-clang-unit 2>&1 | tee log
  1. Let's say the following clang-unit test is failing:
Failed Tests (1):
  Clang-Unit :: ASTMatchers/./ASTMatchersTests/ASTMatchersTests/ASTMatchersTest.IgnoringParens/0
  1. Search for the test name ASTMatchersTest.IgnoringParens/0 in the log. You should see something like:
******************** TEST 'Clang-Unit :: ASTMatchers/./ASTMatchersTests/ASTMatchersTests/ASTMatchersTest.IgnoringParens/0' FAILED ********************
Note: Google Test filter = ASTMatchersTests/ASTMatchersTest.IgnoringParens/0
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from ASTMatchersTests/ASTMatchersTest
[ RUN      ] ASTMatchersTests/ASTMatchersTest.IgnoringParens/0
src/clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp:1501: Failure
  1. The above log tells us the following. Make a note of these.
  • The file containing the failing test is ASTMatchersNodeTest.cpp.
  • The Google Test filter is ASTMatchersTests/ASTMatchersTest.IgnoringParens/0.
  1. The object file for the source file ASTMatchersNodeTest.cpp will be called ASTMatchersNodeTest.cpp.o. Now we need to get the name of the exe into which this object file is linked. We search for the object file name in the log. If we do not find it then it means the file was compiled in a previous run of ninja check-clang-unit. So we need to recompile this file to get the name of the exe file. To force recompilation, we will delete the object file.
cd $BUILD_DIR
find . -name ASTMatchersNodeTest.cpp.o | xargs rm -rf
  1. Now rebuild the tests:
ninja -v check-clang-unit 2>&1 | tee log
  1. Now open the log file again and search for the link command line containing the object file ASTMatchersNodeTest.cpp.o. In the link line the name of the exe should be the after the -o. Grab that exe name. We are now ready to execute our test.

  2. Execute the test with the Google Test filter found in step 4.

tools/clang/unittests/ASTMatchers/ASTMatchersTests --gtest_filter=ASTMatchersTests/ASTMatchersTest.IgnoringParens/0