Introduction to Google Test and CMake

Поделиться
HTML-код
  • Опубликовано: 28 авг 2024
  • A viewer on one of my recent Twitch streams made an excellent suggestion/request for a video showing how to get started with the Google Test framework, to develop unit tests as part of Test Driven Development.
    I decided to throw in CMake as well and show how to start a new project using both, with a simple example that I hope anyone can follow along and do as well.
    I hope you enjoy this video! If you have ideas/requests for similar videos, please let me know. I can't promise to produce them right away, but I'll definitely put them on the list for consideration.
    Prerequisites for this example:
    * Visual Studio Code (VSCode)
    * C/C++ toolchain (examples: Visual Studio Community 2017, Xcode, Clang, gcc)
    * C/C++ extension for VSCode
    * CMake Tools extension for VSCode
    * CMake (either installed separately or as part of CMake Tools for VSCode)
    * git command-line tools (for cloning the googletest repository from github)

Комментарии • 79

  • @MaEtUgR
    @MaEtUgR 5 лет назад +33

    Awsome!! I learned so much in that half an hour, not only about cmake and gtest but also about VS code 👍

  • @DoggoYT
    @DoggoYT 2 года назад +2

    Best tutorial on Google Test I have found so far.
    Heres a tip, when you give your project a name, a PROJECT_NAME variable will be set. So basically you can put project ("ProjectName") and then use ${PROJECT_NAME}. Similar but works.

  • @timothywright4509
    @timothywright4509 5 лет назад +8

    It's almost like you're a real coder who actually knows what they are doing. XD (Unlike every other google test tutorial I've found so far). Thank you!

    • @rhymu
      @rhymu  4 года назад +2

      I only know what I'm doing sometimes. :D

  • @DawidSwitonManiakowski
    @DawidSwitonManiakowski 4 года назад

    I'm a Java developer and this is similar approach that is see in the java software development. Thank you for the video, this is the best approach and the superb explanation.

  • @SaifUlIslam-db1nu
    @SaifUlIslam-db1nu 4 года назад +4

    Pretty easy guide for learning how to test in C/C++. Thank you!

  • @guico3lho
    @guico3lho 5 лет назад

    I really dont know why he has a low number of viewers. He is AWESOME explaining

    • @guico3lho
      @guico3lho 5 лет назад

      @@rhymu Ohhh got it. Keep doing more videos like this one, especially in C++. 😝😉

    • @rhymu
      @rhymu  4 года назад +1

      @@guico3lho I have low viewers probably because most of my videos are simply exported VODs from Twitch. I haven't yet put a lot of serious time into making unique video content for RUclips. Hopefully that will change in the future though! :D (Also hopefully I will begin to reply more promptly and not 7 months late; sorry! )

  • @florentgarnier2201
    @florentgarnier2201 4 года назад +1

    Thank you for spending time to make this tutorial !

    • @rhymu
      @rhymu  4 года назад

      You're very welcome, and thank you for the feedback! I have made a few other tutorials, and plan to make more. If there is any particular topic in programming concepts or techniques, please let me know. If they are topics I am also interested in, I may cover them in some future video.

  • @XxGabberlordxX
    @XxGabberlordxX 4 года назад +2

    Very nice tutorial. I really learned a lot :) I would really appreciate a indepth video(series) about TDD in C++. Could be something super simple like a todo app where you store todos inside a SQLite database. You could show us then how to use gtest for the specific unit tests, gmock to mock external dependencies and maybe also integration tests.
    I think testing in C++ is super confusing compared to Rust or Python. Such a series would help a lot :)

    • @rhymu
      @rhymu  4 года назад

      Now that I've been learning Rust, I know you're right about C++ being super confusing! Rust comes with its own built-in testing framework, whereas with C++ you're really on your own, and hopefully you find a framework that suits you. I really enjoy using Google Test, although I haven't used gmock yet.
      The next opportunity I have to make a series developing something in C++, I will try to include TDD as an integral part of it, and see if I can include gmock in there as well.

    • @XxGabberlordxX
      @XxGabberlordxX 4 года назад

      @@rhymu Hey ty for the reply! More tutorials about TDD are awesome. There aren't many videos on RUclips about TDD especially on low level languages like C++ and Rust. Currently I'm also learning Rust and I have basically the same problem :) The test suite in Rust is fine but the mocking feels horrible. Keep up your great work!

  • @ameynarkhede8264
    @ameynarkhede8264 5 лет назад +4

    These tutorials are really helpful! Thank you so much.

  • @AvijitDas07
    @AvijitDas07 3 года назад +1

    Thank you ! It was really helpful.. It was exactly what I was looking for.

  • @vladyslavtkach2209
    @vladyslavtkach2209 3 года назад

    Everything worked just fine. Thanks!

  • @rabunbike
    @rabunbike 3 года назад

    Ok, that was pretty awesome and extremely useful.

  • @heyquantboy
    @heyquantboy 4 года назад

    Thanks for the intro. Very useful. I hear some echos of the Hitchhiker's guide in the code.

  • @deepknowledge2505
    @deepknowledge2505 5 лет назад +2

    Wonderful video, It helps me a lot, thank you very much

  • @beniamintucu2588
    @beniamintucu2588 3 года назад

    Thanks for the video, it was really helpful and straightforward !!

  • @eldarmusayev7653
    @eldarmusayev7653 4 года назад

    Thank you. I spent hours trying to get all of the pieces to play nicely together. As a noob to C++ its project structure and the hoops are very foreign compared to, say, go, java or python

  • @adamhendry945
    @adamhendry945 2 года назад

    In a sea of hour-long RUclips videos with meaningless slides and useless details, you (1) actually program, (2) show how to set up a test suite, and (3) take half the time. I'm convinced you know Kitware's packages better than the creators. It's mind-blowing how a company of 25+ PhD's cannot teach anything. They must have been born from the revolving door of university academics who make more academics that don't know how to work in the real world. Thank you for this!! Great video!!

    • @rhymu
      @rhymu  2 года назад

      I try, and hopefully I'm getting better each time I make one of these videos. My goal is to walk through things hands-on step by step, and hopefully people learn something new or it helps with some problem they're having using these tools. I'm glad you enjoyed the video!

  • @GeekyMino
    @GeekyMino 4 года назад +5

    Amazing tutorial. I learned a lot. Keep it up! I have a question. What if we have multiple submodules in our project? Do we need to create separate google test folder for each one of them? How do we run all tests from the main one?

    • @rhymu
      @rhymu  4 года назад +4

      I'm glad you liked the tutorial! For multiple submodules in your project, there are at least two approaches I can think of. I personally use approach #1 below:
      1. For each submodule, as you suggested in the question, have a "test" subdirectory containing source code and CMakeLists.txt file to build a test runner just for that submodule (test/CMakeLists.txt would have an "add_test" directive, and the parent/submodule CMakeLists.txt would have an "add_subdirectory(test)" directive). Your overall solution will end up building multiple test runners, one for each submodule. You could run some or all of them individually, or use the CMake VSCode plugin (or CTest, which comes with CMake) to locate and run them for you.
      2. Make an aggregate test runner at the top-level of your project, that picks test files from your various submodules and links them together into one test runner executable.

    • @GeekyMino
      @GeekyMino 4 года назад

      @@rhymu thank you so much for the reply. I like the first approach more so I will try that one first 😊

  • @ProduccionesLukaz
    @ProduccionesLukaz 3 года назад

    simply amazing, thanks!

  • @guilhermedantas5067
    @guilhermedantas5067 4 года назад

    Incredible video. You're a master!

  • @sanojs009
    @sanojs009 3 года назад

    Thank you ! This is really helpful

  • @saintofallsaints
    @saintofallsaints 5 лет назад

    Awsome!! Very informative tutorial! Thank you so much.

  • @pavelhorvath576
    @pavelhorvath576 3 года назад

    Great tutorial. Thank you !

  • @maddocrusmad464
    @maddocrusmad464 3 года назад

    Прикольно, как раз то что мне нужно, и на VSCode. Попытаюсь так же, библиотеку igloo подключить что бы на codewars проверочные тесты запускать;)

  • @fadhlallahbaklouti9111
    @fadhlallahbaklouti9111 4 года назад +2

    Hello I have trouble compiling a simple gtest program for win 10 with cmake (even though the same example works on Linux). The error I get is this:
    LNK2038 mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug'
    Any ideas how to solve this?

    • @rhymu
      @rhymu  4 года назад +2

      Yes, I do! On Windows, the compiler flags /MD, /MDd, /MT, and /MTd can be used select between four popular multi-threaded versions of the C/C++ runtime library: dynamic-link (/MD), dynamic-link-debug (/MDd), static-link (/MT), and static-link-debug (/MTd). You can read more about these options here: docs.microsoft.com/en-us/cpp/build/reference/md-mt-ld-use-run-time-library?view=vs-2019
      It's important that all parts of a program linked together select the *same* version of the run-time library. This linker error will happen if you have one or more parts that are accidentally selecting different versions of the runtime library. To fix the error, you'll need to go through all your projects and verify that you are using the same compiler flag (/MD, /MDd, /MT, or /MTd) on all of them.
      Also try deleting all build products (*.lib, *.dll, etc.) which is easy to do if you use CMake because you simply delete the "build" folder. Then when you build again it should use a consistent set of compiler flags, unless of course you customized your `CMakeLists.txt` or `CMakeCache.txt` files for specific projects.
      If you told CMake to use one of the "Visual Studio" generators, and you have a Visual Studio solution (*.sln) file, try opening that in Visual Studio, then for each project in the workspace in Visual Studio, inspect the compiler flags for runtime library (Configuration Properties -> C/C++ -> Code Generation -> Runtime Library).

  • @SquezzBill
    @SquezzBill 4 года назад +1

    Hi, I have stopped on the building part. I get getting this error: "[build] LINK : fatal error LNK1104: cannot open file "Example.lib"". I have checked my CMakeLists.txt failed, and they are 1:1 with CMakes on the videos

    • @SquezzBill
      @SquezzBill 4 года назад +1

      Okayy, took a while but i figure it. In CMakeLists.txt (that is not inside the test folder) I just simply changed add_library to not use variables (Headers and Sources), I modify it to add_library (${This} STATIC Example.cpp Example.hpp ). Reason why i did this is because linker it just ignore my "Example.cpp" file. When i checked in the "CMake" toolbar on the left, my "Example.lib" contains only "Example.hpp"

  • @hudkmr
    @hudkmr 5 лет назад

    Very informative Tutorial. Thankyou :)

  • @niklasohlsson
    @niklasohlsson 5 лет назад +1

    Thanks Richard!

  • @adamkrawczyk6600
    @adamkrawczyk6600 4 года назад

    So useful, thank you!! :)

  • @asmbaty
    @asmbaty 4 года назад

    Thanks for the intro!

  • @Misterbee0407
    @Misterbee0407 4 года назад

    Great stuff, thank you mate! But I have a question, is there anyway to set gtest_force_shared_crt:BOOL=ON without having to manually edit the CMakeCache.txt?

  • @theoremus
    @theoremus 2 года назад

    Thank you for your video. None of this is intuitive. Face it, you have to be a geek and slug through it. That is part of software engineering.

    • @rhymu
      @rhymu  2 года назад

      At this point several years later, I think I agree with you! CMake and Google Test are _hard_ to get set up, compared to the tools that come automatically and work so easily in other languages/environments (such as Rust).

  • @VitaminRide
    @VitaminRide 3 года назад

    Great tutorial!

  • @md.alveenoor7978
    @md.alveenoor7978 2 года назад

    What a great video, thanks a lot. By the way, how can we include 'main()' function in the main program? It shows error as there are two main functions (another in GTest).

  • @Sorrel555
    @Sorrel555 2 года назад +1

    I don't understand why test and source need two different CMakeLists files.......... -_-

  • @mrconchapelua
    @mrconchapelua 5 лет назад

    Sos un capo Richard me suscribo Saludos

  • @daserstereichen
    @daserstereichen 4 года назад

    Can you please do a CMake tutorial :) you explain it quite clearly

    • @rhymu
      @rhymu  4 года назад

      I would like to make more CMake tutorials, definitely. I will try to set aside some time to do this. Thank you!

  • @guico3lho
    @guico3lho 4 года назад

    Hi, im having a problem, when i debug, the compiler want a a.out program, and he cant find his path besides i have had created on my folder

    • @rhymu
      @rhymu  4 года назад +1

      If I understand you correctly, your compiler generated a program and named the image file "a.out", and now you're trying to run it? You may need to put a dot and slash in front when you try to run it from the shell, if you're on Linux or similar operating system.
      So instead of typing "a.out" to run it, try "./a.out".
      I hope that helps!

  • @JfhuTheJedi
    @JfhuTheJedi 4 года назад

    I have a questions about this.
    I moved the folder to another directory and now it's giving me issues.
    test\ExampleTests.exe : fatal error LNK1169: one or more multiply defined symbols found
    Will it always break if i move the folder or change the file name?

    • @rhymu
      @rhymu  4 года назад +1

      Moving it to another folder will usually break things unless you clean out the build folder (just delete it or don't move it with the rest of the files). The build folder contains intermediate build products that have path names in them, as well as CMake's cache (CMakeCache.txt) which also contains path names.

    • @JfhuTheJedi
      @JfhuTheJedi 4 года назад

      @@rhymu Thank you!

  • @federicosperoni7695
    @federicosperoni7695 4 года назад

    Thank you for your video rhymu8354, it's really well explained.
    I just have an issue when building from the termial. When I type the command build-vscode/test/ExampleTests.exe, with the same path as in your example, the builder seems to not acknowledge the command. It answers "'build-vscode' is not recognized as an internal or external command". Could you please tell me what am I doing wrong? Thank you in advance.

    • @rhymu
      @rhymu  4 года назад

      The directory "build-vscode" is where I made the build folder. It can be whatever you want. So whatever directory you created when you first ran CMake, that will be the directory you want to use in the later command.

  • @wilmirosa7076
    @wilmirosa7076 2 года назад

    Hi, thanks for this amazing tutorial! I have successfully integrated tests. Do you know how to launch the tests from command line?

    • @rhymu
      @rhymu  2 года назад

      If you built successfully you should be able to execute the test runner executable directly from the command line. There's also "ctest" which goes along with cmake and can run your tests for you from the command line.

  • @SaifUlIslam-db1nu
    @SaifUlIslam-db1nu 4 года назад

    I wish to automate the "CMake: Run Tests" by writing code in shell script that I can just execute once so that it can automate all the commands for me.
    How can I do that?

    • @SaifUlIslam-db1nu
      @SaifUlIslam-db1nu 4 года назад

      Nevermind, I found the solution. In your output tab at the bottom, like for commands that start with [proc], they basically tell what commands have been run. In my case, for building it was,
      /usr/bin/cmake --no-warn-unused-cli -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE \
      -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_C_COMPILER:FILEPATH=/usr/bin/gcc \
      -DCMAKE_CXX_COMPILER:FILEPATH=/usr/bin/g++ \
      -H/path/to/project \
      -B/path/to/project/build -G Ninja
      And for running the tests, it was,
      /usr/bin/cmake --build /path/to/project/build --config Debug --target all -- -j 6

    • @SaifUlIslam-db1nu
      @SaifUlIslam-db1nu 4 года назад

      The crossed out words mean that there were actually two -, not a single -.

  • @MattNeundorf
    @MattNeundorf 4 года назад

    awesome

  • @karun4663
    @karun4663 3 года назад

    you have not added it in your root CmakeLists.txt add_subdirectory(googletest), how does it even buids successfully, By the way your videos are excellent, please do more videos in c++

  • @agnesakne4409
    @agnesakne4409 2 года назад

    How can I setup gtest with visual studio?

    • @rhymu
      @rhymu  2 года назад +1

      You may be able to get it set up in VS via CMake, although the last time I tried that it was a disaster. I've moved from VS to VSC because I've found it's a much better and easier experience for most projects. I would probably only use VS at this point for two specific kinds of projects: Windows device drivers and C#/.NET. Neither of which are compatible with gtest.
      In the past before VSC when I wanted to set up gtest in VS I would make a VS project file that represents my unit test runner, and it would pull in the gtest source files along with those of my unit tests, linking them all together.

  • @kesterbelgrove818
    @kesterbelgrove818 5 лет назад

    When I go to select a kit for cmake tool it shows that I have no kits. Can someone help me please?

    • @frogical9553
      @frogical9553 5 лет назад +1

      Have you installed a compiler (like Clang or Visual Studio)?

    • @kesterbelgrove818
      @kesterbelgrove818 5 лет назад

      @@frogical9553 I did not

    • @frogical9553
      @frogical9553 5 лет назад +1

      Download a compiler for your OS

    • @kesterbelgrove818
      @kesterbelgrove818 5 лет назад

      @@frogical9553 I have visual studio code only. I'd add the compilers

    • @kesterbelgrove818
      @kesterbelgrove818 5 лет назад

      @Frogical Thanks a million I did as you said and I have the kits now.

  • @user-gw1sy6mu3i
    @user-gw1sy6mu3i 2 года назад

    What does it mean ? : error LNK2038: обнаружено несоответствие для "RuntimeLibrary": значение "MTd_StaticDebug" не соответствует значению "MDd_DynamicDebug" в ExampleTests.obj

    • @rhymu
      @rhymu  2 года назад

      What that means is you have tried to link a program or shared library with another library that was built with a different compiler option for selecting the C++ runtime library. Go through all the libraries you're linking and make sure they are all selecting the same C++ runtime library. There are many choices, among them static link multithreaded, dynamic link multithreaded, static link multithreaded debug, dynamic link multithreaded debug, etc.

  • @Capestang100
    @Capestang100 2 года назад

    This is great tutorial, but when I #include "gmock/gmock.h" cmake cannot find it at all. Any advice regarding this?

    • @rhymu
      @rhymu  2 года назад +1

      I haven't actually used gmock before. Check the directory and name of the header file and also check that it's in your include path.