Do you even test? (your code with CMake)

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 มิ.ย. 2024
  • It's time we talked about testing our C++ code! We chat about what testing is and look into how to integrate one of the most used testing frameworks (Googletest) into our CMake project.
    Script 🎓
    github.com/cpp-for-yourself/s...
    Related materials 📄
    - Googletest Primer: google.github.io/googletest/p...
    - Video about CMake: • CMake - the essential ...
    - Video about Libraries: • C++ libraries and what...
    - CMake tutorial from which I took the submodule idea: cliutils.gitlab.io/modern-cmake/
    Support Ukraine 🇺🇦
    Ukraine is fighting a war against unprovoked russian aggression. Please donate to one of the funds to support them: www.defendukraine.org/donate
    Please support my free work 🙏
    - Through Patreon: / code_for_yourself
    - Through GitHub Sponsors: github.com/sponsors/niosus
    Contents
    00:00 - Intro
    00:53 - What is testing?
    01:39 - Which framework to use?
    02:18 - One way to integrate testing into your project
    05:11 - Maybe, let's add the C++ code?
    08:42 - A better way to integrate testing into your project
    11:02 - Adding some magic
    Reference
    - Image of Wednesday taken from: deadline.com/gallery/wednesda...
    - All the stock footage is from Pexels
    - The cat video is from TH-cam

ความคิดเห็น • 65

  • @CodeForYourself
    @CodeForYourself  ปีที่แล้ว +11

    Minor correction, the --test-dir flag was only added in CMake 3.20, so if you have an older version, run the tests with these commands instead:
    cd build
    GTEST_COLOR=1 ctest --output-on-failure -j12

    • @kompila
      @kompila ปีที่แล้ว +2

      This is amazing, thanks.

    • @HylianEvil
      @HylianEvil 6 หลายเดือนก่อน

      I'm interested in your computer chair. Is it good for tall people?

    • @CodeForYourself
      @CodeForYourself  6 หลายเดือนก่อน

      @@HylianEvil well, it’s ok. It is a simple IKEA chair 🤷‍♂️

  • @siddevsam
    @siddevsam 3 หลายเดือนก่อน +3

    That clip of cat failing to jump is exact visual representation of me running my code! 🤣🤣

    • @CodeForYourself
      @CodeForYourself  3 หลายเดือนก่อน +1

      Yeah, don't even mention it! Been there many times myself 🫂

  • @alib5503
    @alib5503 ปีที่แล้ว +5

    Fantastic video!
    I had a lot of problem linking to gtest because I installed it on system... I fixed it with setting right environment variables, but this way is much better!
    About submodule, yes! Please show us how you would use git in projects

  • @longvuong2600
    @longvuong2600 ปีที่แล้ว +1

    Looking forward to git submodule video. Thank you for another awesome video.

  • @theintjengineer
    @theintjengineer ปีที่แล้ว +1

    First😎
    Another fascinating video, Igor🔥
    Keep it up✨

  • @blablabla7796
    @blablabla7796 ปีที่แล้ว +6

    This tutorial is criminally underrated. You deserve more subscribers. 👍

    • @blablabla7796
      @blablabla7796 ปีที่แล้ว

      @@CodeForYourself I’ve actively searched for cmake + gtest setup tutorials and they help but they get really confusing really fast. I randomly got recommended this video and, no offence to the other guys but, this is 100x better.

    • @CodeForYourself
      @CodeForYourself  ปีที่แล้ว +1

      Wow, this really warms my heart. Thanks for such a praise! This is _really_ important to me. 🙏

  • @tienvuhong3536
    @tienvuhong3536 หลายเดือนก่อน +1

    That pretty much helpful and so easy to understand, thank you!

  • @andreacassotti2561
    @andreacassotti2561 2 หลายเดือนก่อน +1

    Thanks a lot for your tutorial! It's very clear.

    • @CodeForYourself
      @CodeForYourself  2 หลายเดือนก่อน

      Thanks for watching and for the kind words!

  • @MrCynosure4sure
    @MrCynosure4sure ปีที่แล้ว +3

    This was a very infomative, engaging and entertaining video. Great work!!! Would love to see a video on your git workflow and if you use continuous integration with github. Thank you

    • @CodeForYourself
      @CodeForYourself  ปีที่แล้ว

      Gotcha. Will record this video in the coming weeks most probably. Thanks!

  • @onemoremagistr
    @onemoremagistr 8 หลายเดือนก่อน +1

    Контенту від українців стає більше і це не може не радувати!) Дякую)

  • @ilarioantonioazzollini66
    @ilarioantonioazzollini66 ปีที่แล้ว +1

    Hi Igor, great content once again, thanks!
    One minor comment: looks like for using the "--test-dir build" option, at least CMake 3.20 is required. I am using 3.16 and took some time to figure out why it didn't discover any tests (well not too much as the message was pretty clear in saying that it was not looking for tests in the build folder, but still I expected an error or something like that!)

    • @CodeForYourself
      @CodeForYourself  ปีที่แล้ว +1

      Yeah, that is correct. With the older cmake we have to run the tests from the tests folder 🤷‍♂️ Sorry for not mentioning it upfront.

  • @reubenthomas1033
    @reubenthomas1033 ปีที่แล้ว +1

    Great video!

  • @jackie7023
    @jackie7023 7 หลายเดือนก่อน

    This video is so cool. Is it possible to use google test in the microcontroller project??

    • @CodeForYourself
      @CodeForYourself  7 หลายเดือนก่อน

      I guess so, depends on your setup. Which microcontroller are you talking about?

  • @jcbrites
    @jcbrites 5 หลายเดือนก่อน

    Python and java (and all languages that run on the jvm) have build systems that can access library repositories. It's just then a matter of declaring the exact version of the dependency we use in our code. The library is then fetched automatically by the build system (including testing frameworks). Isn't there an equivalent thing for c++?

    • @CodeForYourself
      @CodeForYourself  5 หลายเดือนก่อน

      Not built into the language. I guess just as it is not built into python for example. For python you would use pip, for C++ there is Conan or CPM (the ones that come to mind). They are not as standardized though.

  • @silentage6310
    @silentage6310 หลายเดือนก่อน +1

    Does this code at the end of each test run attempt to update submodules? Will this slow down the testing process if tests are run frequently?

    • @CodeForYourself
      @CodeForYourself  หลายเดือนก่อน

      Thanks for the question! The update code is only run at the cmake configuration stage. So it is not run when the tests are run. Does this make sense?

  • @keepgoing335
    @keepgoing335 ปีที่แล้ว +1

    Can i ask what are the advantages of adding google test as submodule over using FetchContent?

    • @CodeForYourself
      @CodeForYourself  ปีที่แล้ว +3

      Not too many. And mostly quite obscure. If we have a weird network configuration, like a firewall it feels more cumbersome to set up FetchContent, at least I remember struggling with it recently but I don't remember exactly what was so hard about it. 🤷‍♂️ Just remember it was something with permissions.
      The other thing that comes to mind is a situation when I need to clean and rebuild code without access to the internet. When using FetchContent the build folder including all external sources will be removed. This situation bit me a couple of times before. With the submodules as long as they were checked out when we had internet we're going to be good.
      There might be other reasons but as you see the ones I have are pretty obscure. FetchContent is not a bad solution, just a little bit finicky at times for my taste.

    • @keepgoing335
      @keepgoing335 ปีที่แล้ว +2

      @@CodeForYourself ah.. I see the advantage of not deleting the external sources when rebuilding. on my commute to the office my laptop isn't connected to the internet mostly and it'll be nice to still have the external sources available

  • @mek101whatif7
    @mek101whatif7 ปีที่แล้ว +2

    FOR THE ALGORHYTHM!!!

  • @BenUK1
    @BenUK1 ปีที่แล้ว

    What I don’t understand is was CTest actually gives you here. I build my GoogleTest based Unit Test projects with Cmake but without using CTest and I just run the created Unit Test executable. It gives me the exact same GTest output as in your video, so I don’t really get what CTest is for?

    • @CodeForYourself
      @CodeForYourself  ปีที่แล้ว +1

      CTest is really just a way to "register" gtest with the rest of CMake. So that you could run the ctest binary from the same folder where you are building from. It makes sure you can run all the test targets in a unified way. Imagine having multiple test targets in multiple folders. As long as they all are registered through CTest you can run them all with a single unified command. Which is by the way the same should you use a different testing framework as an additional benefit. Does this make any sense?

  • @Andrei-cp5jr
    @Andrei-cp5jr 6 หลายเดือนก่อน

    When I type #include the IDE doesn't find that file. In fact, even if I try to locate the file in the googletest folder and I type it in manually, the IDE still signals that the file cannot be found. What should I do? Where could the problem come from?

    • @CodeForYourself
      @CodeForYourself  6 หลายเดือนก่อน +1

      It is hard to answer your question precisely unfortunately as I don't know what you did before and which IDE you are using? Did you follow the video precisely? Did you copy all the code from the video script?

    • @Andrei-cp5jr
      @Andrei-cp5jr 5 หลายเดือนก่อน +1

      @@CodeForYourself Yep, I did everything in the video, even started over with a new project and followed every step. I was able to solve the problem by importing the framework automatically from GitHub and restarting the IDE. The folder structure was a lot less complicated. I also had a hard time understanding the dependencies in the CMakeLists file, but once I got that right and linked everything with the framework, it was fine. I had a few moments where I got confused during the video and maybe did something wrong and maybe that's why it didn't work. Anyway I'm glad it works now and that I was able to submit my programming assignment on time.

    • @CodeForYourself
      @CodeForYourself  5 หลายเดือนก่อน

      @@Andrei-cp5jr glad that it eventually worked out! What I’m a bit confused about is what do you mean by an IDE? Which one do you use? I find that heavy IDEs are very cumbersome to use and when you’re starting out I would suggest to only use text editors without the semantic autocomplete as this makes one learn faster in my experience.

    • @Andrei-cp5jr
      @Andrei-cp5jr 5 หลายเดือนก่อน

      @@CodeForYourself I'm using CLion

  • @whac-a-robot
    @whac-a-robot หลายเดือนก่อน

    Hi Igor, I am using CMAKE 3.16, when I tried the above I am getting a message stating that no tests found. How to resolve this?

    • @CodeForYourself
      @CodeForYourself  หลายเดือนก่อน

      Yeah, that’s a bit of a limitation. There was another comment under this video where we come up with a solution. Does that solution work for you?

    • @whac-a-robot
      @whac-a-robot หลายเดือนก่อน

      @@CodeForYourself where is that test folder is mapped? I couldn't find it.

    • @CodeForYourself
      @CodeForYourself  หลายเดือนก่อน

      @whac-a-robot8623 it should be just the build folder itself. So you have to change the directory into that build folder and run the cmake command from there. If that doesn’t work out I’ll have a look more deeply into this.

    • @whac-a-robot
      @whac-a-robot หลายเดือนก่อน +1

      @@CodeForYourself ok will check and let you know

    • @whac-a-robot
      @whac-a-robot หลายเดือนก่อน +1

      @@CodeForYourself I understood it now. For quick reference for others you putting my solution here. Command 1 : cd build . Command 2 : GTEST_COLOR=1 ctest --output-on-failure -j12.
      Thank you for the support.