Introduction to Google Test and CMake

Sdílet
Vložit
  • čas přidán 26. 12. 2018
  • 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)
  • Věda a technologie

Komentáře • 79

  • @MaEtUgR
    @MaEtUgR Před 5 lety +33

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

  • @DawidSwitonManiakowski

    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 Před 4 lety +4

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

  • @ameynarkhede8264
    @ameynarkhede8264 Před 5 lety +4

    These tutorials are really helpful! Thank you so much.

  • @timothywright4509
    @timothywright4509 Před 5 lety +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  Před 4 lety +2

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

  • @AvijitDas07
    @AvijitDas07 Před 3 lety +1

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

  • @beniamintucu2588
    @beniamintucu2588 Před 3 lety

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

  • @saintofallsaints
    @saintofallsaints Před 5 lety

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

  • @DoggoYT
    @DoggoYT Před 2 lety +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.

  • @rabunbike
    @rabunbike Před 3 lety

    Ok, that was pretty awesome and extremely useful.

  • @guilhermedantas5067
    @guilhermedantas5067 Před 4 lety

    Incredible video. You're a master!

  • @florentgarnier2201
    @florentgarnier2201 Před 4 lety +1

    Thank you for spending time to make this tutorial !

    • @rhymu
      @rhymu  Před 4 lety

      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.

  • @vladyslavtkach2209
    @vladyslavtkach2209 Před 3 lety

    Everything worked just fine. Thanks!

  • @eldarmusayev7653
    @eldarmusayev7653 Před 4 lety

    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

  • @deepknowledge2505
    @deepknowledge2505 Před 5 lety +2

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

  • @heyquantboy
    @heyquantboy Před 4 lety

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

  • @hudkmr
    @hudkmr Před 5 lety

    Very informative Tutorial. Thankyou :)

  • @ProduccionesLukaz
    @ProduccionesLukaz Před 3 lety

    simply amazing, thanks!

  • @asmbaty
    @asmbaty Před 4 lety

    Thanks for the intro!

  • @pavelhorvath576
    @pavelhorvath576 Před 3 lety

    Great tutorial. Thank you !

  • @sanojs009
    @sanojs009 Před 3 lety

    Thank you ! This is really helpful

  • @Misterbee0407
    @Misterbee0407 Před 3 lety

    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?

  • @adamkrawczyk6600
    @adamkrawczyk6600 Před 4 lety

    So useful, thank you!! :)

  • @md.alveenoor7978
    @md.alveenoor7978 Před 2 lety

    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).

  • @XxGabberlordxX
    @XxGabberlordxX Před 4 lety +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  Před 4 lety

      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 Před 4 lety

      @@rhymu Hey ty for the reply! More tutorials about TDD are awesome. There aren't many videos on CZcams 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!

  • @VitaminRide
    @VitaminRide Před 3 lety

    Great tutorial!

  • @guico3lho
    @guico3lho Před 5 lety

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

    • @guico3lho
      @guico3lho Před 5 lety

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

    • @rhymu
      @rhymu  Před 4 lety +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 CZcams. Hopefully that will change in the future though! :D (Also hopefully I will begin to reply more promptly and not 7 months late; sorry! )

  • @niklasohlsson
    @niklasohlsson Před 5 lety +1

    Thanks Richard!

  • @mrconchapelua
    @mrconchapelua Před 5 lety

    Sos un capo Richard me suscribo Saludos

  • @maddocrusmad464
    @maddocrusmad464 Před 3 lety

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

  • @GeekyMino
    @GeekyMino Před 4 lety +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  Před 4 lety +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 Před 4 lety

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

  • @federicosperoni7695
    @federicosperoni7695 Před 4 lety

    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  Před 4 lety

      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.

  • @guico3lho
    @guico3lho Před 4 lety

    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  Před 4 lety +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!

  • @fadhlallahbaklouti9111
    @fadhlallahbaklouti9111 Před 4 lety +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  Před 4 lety +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).

  • @wilmirosa7076
    @wilmirosa7076 Před 2 lety

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

    • @rhymu
      @rhymu  Před 2 lety

      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.

  • @SquezzBill
    @SquezzBill Před 4 lety +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 Před 4 lety +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"

  • @JfhuTheJedi
    @JfhuTheJedi Před 4 lety

    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  Před 4 lety +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 Před 4 lety

      @@rhymu Thank you!

  • @SaifUlIslam-db1nu
    @SaifUlIslam-db1nu Před 4 lety

    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 Před 4 lety

      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 Před 4 lety

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

  • @Sorrel555
    @Sorrel555 Před 2 lety +1

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

  • @agnesakne4409
    @agnesakne4409 Před rokem

    How can I setup gtest with visual studio?

    • @rhymu
      @rhymu  Před rokem +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.

  • @MattNeundorf
    @MattNeundorf Před 4 lety

    awesome

  • @daserstereichen
    @daserstereichen Před 4 lety

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

    • @rhymu
      @rhymu  Před 4 lety

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

  • @kesterbelgrove818
    @kesterbelgrove818 Před 4 lety

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

    • @frogical9553
      @frogical9553 Před 4 lety +1

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

    • @kesterbelgrove818
      @kesterbelgrove818 Před 4 lety

      @@frogical9553 I did not

    • @frogical9553
      @frogical9553 Před 4 lety +1

      Download a compiler for your OS

    • @kesterbelgrove818
      @kesterbelgrove818 Před 4 lety

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

    • @kesterbelgrove818
      @kesterbelgrove818 Před 4 lety

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

  • @adamhendry945
    @adamhendry945 Před 2 lety

    In a sea of hour-long CZcams 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  Před 2 lety

      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!

  • @karun4663
    @karun4663 Před 3 lety

    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++

  • @theoremus
    @theoremus Před 2 lety

    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  Před rokem

      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).

  • @user-gw1sy6mu3i
    @user-gw1sy6mu3i Před 2 lety

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

    • @rhymu
      @rhymu  Před 2 lety

      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 Před 2 lety

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

    • @rhymu
      @rhymu  Před 2 lety +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.