Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set up googletest wtih meson?

I have a git repository with some test code in C++ and I want to use Googletest to write some tests. I used git submodule to get it as part of the above repository. I want to use meson as the build engine. So far, so good.

However, I cannot fathom how to get meson to build and link my tests with the googletest submodule… Should I use a wrap? An external dependency? what?

Note that meson supports dependencies on packaged versions of gtest/gmock but this is not what I want since the developers of gtest/gmock recommend against it. Plus, I want bleeding edge 'cause I am crazy⸮

Furthermore, I do not think ninja plays a role here but I mentioned that I use it just in case.

like image 388
Sardathrion - against SE abuse Avatar asked Aug 13 '19 07:08

Sardathrion - against SE abuse


People also ask

How do I setup a Google Test in Visual Studio?

Add a Google Test project in Visual Studio 2022 In Solution Explorer, right-click on the solution node and choose Add > New Project. Set Language to C++ and type test in the search box. From the results list, choose Google Test Project. Give the test project a name and choose OK.

How do I set up the Test Adapter for Google Test?

From the main menu, choose Tools > Options > Test Adapter for Google Test to set additional options. See the Google Test documentation for more information about these settings.

What is googletest and how does it work?

When a test fails, googletest allows you to run it in isolation for quick debugging. Tests should be well organized and reflect the structure of the tested code. googletest groups related tests into test suites that can share data and subroutines. This common pattern is easy to recognize and makes tests easy to maintain.

How do I add a Google Test Project to a solution?

Add a Google Test project to the solution. In Solution Explorer, right-click on the solution node and choose Add > New Project. In the left pane, choose Visual C++ > Test and then choose Google Test Project in the center pane. Give the test project a name and click OK.

How do I set up a project with googletest in CMake?

CMake uses a file named CMakeLists.txt to configure the build system for a project. You’ll use this file to set up your project and declare a dependency on GoogleTest. First, create a directory for your project: Next, you’ll create the CMakeLists.txt file and declare a dependency on GoogleTest.


2 Answers

I tried using the wrap for gtest with

gtest_proj = subproject('gtest')
gtest_dep = gtest_proj.get_variable('gtest_dep')
gmock_dep = gtest_proj.get_variable('gmock_dep')

in meson.build. This builds a local copies of googletest which can then be used like so:

tests_src = [
  'tests/gtest-all.cpp',
  'tests/test_MyClass.cpp',
]  
e = executable(
  'gtest-all',
  tests_src,
  dependencies : [
    gtest_dep,
    gmock_dep],
  link_with : libshield,
)    
test('gtest tests', e)

Note that libshield is a shared library created from my (toy) code so I can link to it.

like image 59
Sardathrion - against SE abuse Avatar answered Sep 24 '22 01:09

Sardathrion - against SE abuse


If you would like to use a project that is not meson project you need to find that project in wrapDB:

meson wrap search gtest

If that command gives you name of the wrap then you need to install it to your project:

mkdir -p subprojects
meson wrap install gtest

Then you should reconfigure your project and meson will download that project for you:

meson --reconfigure path/to/build/dir

Additional information you can find in documentation of wrap tool.

--reconfigure - supported since 0.49.0

like image 32
Umed Avatar answered Sep 20 '22 01:09

Umed