CMake-Episode 023: CMake - Testing with Catch2 | CMake Starts Here

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello everyone and welcome to this video in this one we will be using another library to do testing and this is just another option if you want testing you can either use catch 2 as we are doing here or use Google test like we showed in the previous video again we will start by pulling in the third party dependency that's going to be catch too I actually think before you even try to use the library you should VIs visit it on GitHub and see what it does it is a modern C++ native test framework for using unit tests it does a lot of things and you can really read about that I think even if you go down they will give you a link to know why you need a testing framework and they really give a good number of points that you should know if you are just getting started with this topic here reading the documentation we will be brought to how do we get here okay let's try to find the documentation together so let's go down and see how we can use this they give a link to a tutorial so this is a good place to go if we go to the tutorial you see they have a link about cement integration you can read that and if they is talk about CAC Target they talk about include fetch content they talk about using fetch content like we are doing here fetch content declare fetch content make available you see this is what we are used to do now and they give us the name of the target we should link to this is Vital Information remember this and further down they say that when using fetch content include catch will fail so for this to work we need to update CAC module path to contain a location to the custom seac modules that are brought in by the cat to library and we do this by appending this to our simic module path this is what we are doing in our coding here so include Fage content fet content declare Fage content make available and we are appending to our cake module path as instructed by the documentation of the library that we are trying to use here this is where I got the information once you have the dependency pulled in you need to link against it it is really going to be exactly the same thing we did with Google test we will include the module that brings in our third party dependency which is our catch to library and the code to do that leaves in this custom module here once we have the third party dependency we will set up an executable which is going to be made up of our testing code in this test C CPP file we will link against the catch Library we will also link against the calculator Target that contains the code that we want to test I think this is something I didn't specify explicitly in the last video but this is my chance to save myself here we are including C test C catch and we are discovering the tests as instructed by the documentation again if you go there you see that it is the same thing we are doing here you have to read your documentation to be able to use your third party Library once we have this we can hop over into our root cmakelist.txt file we will enable testing and we will bring in our test subdirectory which is going to bring in the code that we are interested in to enable testing for our Pro project here let's hop over to visual studio code and look at this inaction let's look at the build folder it contains thanks so we can reveal in Explorer go in the folder delete everything okay and close this off now we have a clean slate off of which to start you can look at what we have in the source code it is exactly the same thing we had in the last lecture with Google test we have a C list. txt file which is going to set up a calculator Target okay this target is going to be what we link against in our testing code we go in our test folder we have a test Cal file okay before we look at that let's look at how we bring in the library so we have our Target setup here unit tests and we are linking to this Target and what we want to link against is the calculator Target and the cach Target and we know this name from the documentation I really want you to see this this is a main pain point for many people beginning with cake and testing and these kinds of things here once we have this we do as instructed by the documentation to include C test include catch and uh make our tests visible to the C test module that is built into to cake I think this is really all we have the main CPP file which is going to use the calculator as we had earlier and by the way if you are finding this video useful please remember to share like And subscribe to support me that's going to tell the YouTube algorithm that these videos are good and I am really working on them to be good so if you find them good please let me know let's try to configure the project and see if we can build our project with testing through catch 2 integrated we can do that so cake s and the source folder is going to be the current folder The Bu folder is going to be the build directory let's configure it is going to be Stu minute downloading our catch tool library and it is going to configure it for use if we go in the build directory we have a depths folder if we go in you see that we have a few things related to catch too okay now we have the build files written to our build folder we can see make build build to build we can do that this is going to build our executable and tests let's wait for this to happen Okay the build is successful you can see our unit test the target here which is built you can see our rooster Target which is built so we can CD into build and do debug I think we can do that and do rooster this is going to run our binary we can do debug no we can do test debug unit test to run the tests manually but in The Bu folder you can also do c test okay and it is going to run your test tests again if we go in our source folder for example and mess up the add function by adding six6 and try to build again can do that see make build build or the current directory that's where we are at and run C test again it is going to say that one test failed because the add function the multiplication function it is what we messed up it is not working correctly because it is not multiplying a and B it is multiplying a with another number but no problem we can fix it and build again this is the workflow you go through to build things let's build again and run C test and everything is going to go fine please use testing in your project doesn't really matter what your library you use you can use Google test you can use cach to just make sure you are testing your code because that's what all the cool kids are doing and it is not just because it is popular it is really useful to test you could other people working on your project are going to know when they break things and that's going to save a lot of time another good side about tasting is that you can integrate this with your CI pipeline if you don't know what a CI pipeline please don't worry that's something people do to make things integrate well with other things maybe we are going to talk about that in another video but in this video I am talking about tasting using cmake for your C++ project this is really all I had to share in this video if you found it useful please let me know in the comments below I am going to stop here and I will see you next time
Info
Channel: LearnQtGuide
Views: 1,722
Rating: undefined out of 5
Keywords: #Cpp, #cplusplus, #Cplusplus, #ModernCpp, #CMake, #Qt, #QML, #Programming, #Coding, #CodeNewbie, #LearnProgramming, #CodeWithMe, #DeveloperCommunity, #TechTalk, #ProgrammingTips, #SoftwareDevelopment, #CodeLife, #CodingJourney, #CodeForBeginners, #CppDevelopment, cmake, testing, catch2
Id: eBByZNtnpFM
Channel Id: undefined
Length: 8min 8sec (488 seconds)
Published: Tue Jan 02 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.