One of the things I missed when I migrated from cmake to meson was the ease at which cmake discovers tests.

1# Tests
2option(PACKAGE_TESTS "Build the tests" OFF)
3if(PACKAGE_TESTS)
4  find_package(GTest REQUIRED)
5  enable_testing()
6  include(GoogleTest)
7  add_subdirectory(gtests)
8endif()

Thankfully, meson can kind of emulate this behavior, even in its restricted syntax. The key concept is arrays and their iterators.

 1test_array = [#
 2  # ['Pretty name', 'binary_name', 'BlahTest.cpp']
 3  ['String parser helpers', 'strparse_run', 'StringHelpersTest.cpp'],
 4  ['Improved Dimer', 'impldim_run', 'ImpDimerTest.cpp'],
 5]
 6foreach test : test_array
 7  test(test.get(0),
 8       executable(test.get(1),
 9          sources : ['gtests/'+test.get(2)],
10          dependencies : [ prog_deps, gtest_dep, gmock_dep ],
11          link_with : proglib,
12          cpp_args : prog_extra_args
13                 ),
14      )
15endforeach

Now this is pretty neat already, but we can go a step further and augment this with a working directory concept.

 1test_array = [#
 2  # ['Pretty name', 'binary_name', 'BlahTest.cpp', 'working_dir']
 3  ['Improved Dimer', 'impldim_run', 'ImpDimerTest.cpp', '/gtests/data/'],
 4  ['Matter converter', 'matter_run', 'MatterTest.cpp', '/gtests/data/systems/sulfolene'],
 5  ['String parser helpers', 'strparse_run', 'StringHelpersTest.cpp', ''],
 6]
 7foreach test : test_array
 8  test(test.get(0),
 9       executable(test.get(1),
10          sources : ['gtests/'+test.get(2)],
11          dependencies : [ prog_deps, gtest_dep, gmock_dep ],
12          link_with : proglib,
13          cpp_args : prog_extra_args
14                 ),
15        workdir : meson.source_root() + test.get(3)
16      )
17endforeach

This is rather satisfying. Combined with some wraps, it is also pretty portable.