No video

Introduction to Google Test and CMake

  Рет қаралды 137,109

Rhymu's Videos

Rhymu's Videos

Күн бұрын

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 жыл бұрын
Awsome!! I learned so much in that half an hour, not only about cmake and gtest but also about VS code 👍
@DoggoYT
@DoggoYT 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 жыл бұрын
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 жыл бұрын
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 жыл бұрын
Pretty easy guide for learning how to test in C/C++. Thank you!
@florentgarnier2201
@florentgarnier2201 4 жыл бұрын
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.
@ameynarkhede8264
@ameynarkhede8264 5 жыл бұрын
These tutorials are really helpful! Thank you so much.
@AvijitDas07
@AvijitDas07 3 жыл бұрын
Thank you ! It was really helpful.. It was exactly what I was looking for.
@rabunbike
@rabunbike 3 жыл бұрын
Ok, that was pretty awesome and extremely useful.
@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 жыл бұрын
@@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 KZfaq. Hopefully that will change in the future though! :D (Also hopefully I will begin to reply more promptly and not 7 months late; sorry! )
@XxGabberlordxX
@XxGabberlordxX 4 жыл бұрын
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 KZfaq 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!
@beniamintucu2588
@beniamintucu2588 3 жыл бұрын
Thanks for the video, it was really helpful and straightforward !!
@vladyslavtkach2209
@vladyslavtkach2209 3 жыл бұрын
Everything worked just fine. Thanks!
@deepknowledge2505
@deepknowledge2505 5 жыл бұрын
Wonderful video, It helps me a lot, thank you very much
@adamhendry945
@adamhendry945 2 жыл бұрын
In a sea of hour-long KZfaq 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!
@heyquantboy
@heyquantboy 4 жыл бұрын
Thanks for the intro. Very useful. I hear some echos of the Hitchhiker's guide in the code.
@guilhermedantas5067
@guilhermedantas5067 4 жыл бұрын
Incredible video. You're a master!
@GeekyMino
@GeekyMino 4 жыл бұрын
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 жыл бұрын
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 😊
@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
@saintofallsaints
@saintofallsaints 5 жыл бұрын
Awsome!! Very informative tutorial! Thank you so much.
@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).
@sanojs009
@sanojs009 3 жыл бұрын
Thank you ! This is really helpful
@ProduccionesLukaz
@ProduccionesLukaz 3 жыл бұрын
simply amazing, thanks!
@pavelhorvath576
@pavelhorvath576 3 жыл бұрын
Great tutorial. Thank you !
@hudkmr
@hudkmr 5 жыл бұрын
Very informative Tutorial. Thankyou :)
@fadhlallahbaklouti9111
@fadhlallahbaklouti9111 4 жыл бұрын
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 жыл бұрын
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).
@asmbaty
@asmbaty 4 жыл бұрын
Thanks for the intro!
@adamkrawczyk6600
@adamkrawczyk6600 4 жыл бұрын
So useful, thank you!! :)
@Sorrel555
@Sorrel555 2 жыл бұрын
I don't understand why test and source need two different CMakeLists files.......... -_-
@SquezzBill
@SquezzBill 4 жыл бұрын
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 жыл бұрын
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"
@niklasohlsson
@niklasohlsson 5 жыл бұрын
Thanks Richard!
@VitaminRide
@VitaminRide 3 жыл бұрын
Great tutorial!
@maddocrusmad464
@maddocrusmad464 3 жыл бұрын
Прикольно, как раз то что мне нужно, и на VSCode. Попытаюсь так же, библиотеку igloo подключить что бы на codewars проверочные тесты запускать;)
@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!
@mrconchapelua
@mrconchapelua 5 жыл бұрын
Sos un capo Richard me suscribo Saludos
@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).
@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?
@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 жыл бұрын
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!
@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
@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.
@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 жыл бұрын
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!
@agnesakne4409
@agnesakne4409 2 жыл бұрын
How can I setup gtest with visual studio?
@rhymu
@rhymu 2 жыл бұрын
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.
@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++
@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.
@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 жыл бұрын
Have you installed a compiler (like Clang or Visual Studio)?
@kesterbelgrove818
@kesterbelgrove818 5 жыл бұрын
@@frogical9553 I did not
@frogical9553
@frogical9553 5 жыл бұрын
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 жыл бұрын
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.
Do you even test? (your code with CMake)
12:38
Code for yourself
Рет қаралды 21 М.
Unit Tests and Test Doubles like Mocks, Stubs & Fakes
17:32
Cognitive Programmer
Рет қаралды 133 М.
Kind Waiter's Gesture to Homeless Boy #shorts
00:32
I migliori trucchetti di Fabiosa
Рет қаралды 12 МЛН
The Joker saves Harley Quinn from drowning!#joker  #shorts
00:34
Untitled Joker
Рет қаралды 67 МЛН
👨‍🔧📐
00:43
Kan Andrey
Рет қаралды 10 МЛН
طردت النملة من المنزل😡 ماذا فعل؟🥲
00:25
Cool Tool SHORTS Arabic
Рет қаралды 12 МЛН
Test Driven C++ - Phil Nash - CppCon 2020
1:01:48
CppCon
Рет қаралды 35 М.
C++Now 2017: Daniel Pfeifer “Effective CMake"
1:27:02
CppNow
Рет қаралды 179 М.
Introduction to CMake Crash Course
14:08
PunchedTape
Рет қаралды 15 М.
Florent Castelli: Introduction to CMake
1:25:49
SwedenCpp
Рет қаралды 46 М.
unit testing in VS2022 with google test
16:10
Andy Blackburn
Рет қаралды 10 М.
C++ Weekly - Ep 208 - The Ultimate CMake / C++ Quick Start
32:51
C++ Weekly With Jason Turner
Рет қаралды 76 М.
Deep CMake for Library Authors - Craig Scott - CppCon 2019
1:01:35
Back to Basics: Unit Tests - Ben Saks - CppCon 2020
1:01:56
CppCon
Рет қаралды 34 М.
BEST WAY to make Desktop Applications in C++
26:00
The Cherno
Рет қаралды 906 М.
Kind Waiter's Gesture to Homeless Boy #shorts
00:32
I migliori trucchetti di Fabiosa
Рет қаралды 12 МЛН