Introduction to Google Test and CMake

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello today I'm gonna give a little bit of an introduction on how to use the Google test framework along with CMake so we're gonna start off with absolutely nothing except for Visual Studio code and I'll show you exactly what extensions I'm using that are important today I have a lot of extensions but really the only important ones we need our C C++ see make tools and that's it and so you make is a bit of a learning curve but for this small example it should be pretty easy to understand and maybe a good introduction to it I'm going to close that off and we have nothing open yet so let's open a new folder and we're going to create a new folder and call it example alright select that guy so first thing you need if you're gonna be using Google test is to clone Google test easy enough just open a terminal and we go to get loan HTTP github.com because if you can tell I'm totally saying this from memory and not looking at my second monitor cloning it from github.com google google test we have Google test now let's establish our project so the first thing we're going to want to do is create a new file call it C make lists text and let's hide the terminal here so I'm going to sort of type out the basic structure of a component they use a C mate so first thing I'm gonna do C make minimum required version and then you sort of say what version of C make you depend on and usually usually you want to pick the version that you're you stalled and you don't get that through the I don't actually I don't I don't know if you do but I don't think you get it through installing see make tools add-on you may I'm not sure I installed see make separately you just go to there see me Google search see make you go there website and install latest version and you're good to go this is what the see make tools plugin does is it it can detect what see make you have installed and also what other stuff you have like I'm gonna be using visual Studios tool chain to build things and it kind of helps you set that up hopefully if you have the compiler installed and you have C being installed everything is pretty much automatic so they say what version is to make we need and I like to have a variable that I set called this which I'll reuse multiple places actually in this example I'll only use it in two places but it's still sort of useful to have then if you decide to rename your code you'll have it and only one place you get to rename so for the top-level see make you want to establish the name of the project and also what languages you're using the name of the project is first and then the two languages C and C++ and then we're gonna set up some variables that help the build system generator and know what versions of the languages because there are different versions of C and C++ so C make C standard and C X X standard tell it what versions of C and C++ we're going to be using and there's one more that I like to set which is C make position independent code I don't know if you really need that but position independent code is nice saying to have a nice setting to have four libraries that are going to be mixed together and linked together into executables so we're going to want at the top level you're going to want to say this once enable testing that tells C make that you're going to have unit tests and then we're also going to pull in all our dependencies that are within our workspace right now we only have one so AB subdirectory Google test and then we're going to start describing what we're gonna build or our code so I'd like to break it up into two different variables called headers and source you don't have to do this but I like to do this and then for headers I like to have one line per filename and we'll just list the headers that we expect to have and the source files we expect to have right and then here's the key line to actually make code you either use add library or add executable quote me on that you see there ad executable ad program library means it's going to be linked with other things to form programs and that's important because when you're running Google test Google test is going to be your main program it's going to pull in your code as a library actually that's not exactly true we're gonna split our code into two pieces the actual code and then the tests that go with the code the tests that are gonna go with the code are gonna be a program the code that's going to be tested is this library so usually I like to use static like libraries but you can also make it shared and here's where you list all your sources and all your headers so that one statement there should be enough racemic to make a build system that compiles your example dot cpp and makes it into a library and then because we're going to keep our tests separate from our code will put our tests in a subdirectory called tests that's it for that file so let's go ahead and make the two files we need add a file example HPV add up add example Darcy VB and so this is just standard things you would want to have so I'd like to have it for every header file a guard if not deaf example hvp define that header guard and if there's the basic structure right and the first thing we'll want our example CBP to do is exclude our header fair enough right so that's minam Oh a hole we need to make the test we vary the test next test next so test will have its own see make lists that's important because ads sub-directory what it does is it goes into that subdirectory and looks for a cmakelists there and basically runs that in a child node of the build system so here is where we're going to describe the tests so the same kinds of things as the outside and I'm kind of copying from a recipe over on my other screen here so you make minimum required version 3.8 and set this is example tests and set sources and we don't really need headers for tests okay so we're going to set the sources up to be we're gonna have our tests in one file called example test at the CDP okay then we add executable so this is similar to the add library only instead of making a component that's linked into a program we're actually making the program itself so the will use the name example tests and we'll pull in the sources that's all you have to do to describe your end now things that you depend on you would use target linked libraries and you repeat this you say public meaning it's not just you but anything using this program would depend on it as well oh I forgot to n terminate that and here's where you're gonna list G test main that's provided that's a target provided by Google test which pulls in the main program so Google test provides the main program for you you're linking it in with your tests and then we need the thing we're testing of course example and then to tell C make that this program is actually a unit test we'll run the ad test command tell it the name and tell it the command to run which is also the name of the program again and that's it and now we just need to establish that example test CPP so here we go we'll make a new file example tests the CV so minimally you don't need anything so with the sea make see make tools yes see make tools extension you should get okay you I guess we're not starting with one yet because we need to reload this because we didn't have a/c make this when we started so what we'll do is we'll tell Visual Studio code to reload reload window when it reloads it into text that we have a/c make file it should initialize and say pick a kit it only do this the first time until you pick one so it basically detected all of the compilers I have it knows I have a cig wooden build of of clang and then we have Visual Studio community 2017 so let's pick Visual Studio community 2017 amd64 is a sixty bit 64-bit build how we can do x86 for 32-bit build do the 64-bit one and it is unless I made some mistakes what to do is it'll configure a generator and defaults to ninja which I kind of like and it'll detect all the tools and then it's basically building the build system for us right now and we're done and it says do you want us you want see make tools to configure intellisense sure allow so now if we hit f7 to build I made a mistake it should build everything okay so there's a little bit of a warning about the use of libraries so that's because Google test is using a different version of the runtime libraries than we default to so we could fix that if we go if I remember I do this right the bill directory so by the way the name of the bill directory depends on what you have set in your preferences I happen to have setup here or see make bill directory I have it set to build that sure yes code just to remind myself that I made this bill systems reviews code I think if you don't have this it defaults to just build or something like that the bill vs code and then it'll make a C make cash so here we're gonna look for is I think it's library I'm learning this along with you there's some there's something in here that controls whether we're linking aesthetically or dynamically it's an option in here somewhere or actually would it be under G test here we go one of these guys I'll find it once and then you can just follow along with me and you won't have to find it there we go I think it's this force shared CRT so I think we want to turn this two on and then when we do I think it's bill what is it I think it's under Tabb here we can do clean rebuild and we should see it we should make sure that that warning goes away times you get these two random errors where I kind of just ignore them cool so we go through and make make sure we didn't get any warnings that's that's essential you don't want to have any warnings or errors and all I should also show up in problems here there are no problems great but when we run this which we can run if we click on should be a tab here actually there's no tab at the bottom you can just again ctrl shift P to get the command palette and look for C test or C make run tests there we go it'll run our test for us and now it now it pops up something here we can click it again whenever we want we want to rerun our test so it passes because we have no tests everything passes great so let's make some tests this is standard test-driven development stuff so Google test lets you make really basic tests tests we have to give the suite of tests a name so we can call it anything we want let's just call it example test so B it doesn't have to match the name of the file but I generally do make it match just so it's consistent and we have to give the actual test a name so let's just show an example [Music] just demonstrate Kuji test macros okay and then we here are some basic macros you can do Oh hold on before this to make sense to the compiler we need to include G test or Google and let me see if I can get this right test /g test @h yes so it'll find the header because in our C make lists we told it that we want a G test main to be linked into our program so that not only links it in when we build it it also is a clue to intellisense and the compiler about where to find header files like G test retest @h alright so some simple macros you can do some sanity checking expect true true and then we can run that just hit f7 run the test again still passing now if we were to say false here oopss if i could spell it right live here only doing minimal editing to this video otherwise it's just like I'm streaming now when we run the test we see it fails and it should also print out exactly what line number had failed on and it'll say that the actual value was false the expected was true so here's an example like we could repeat this a couple times and we'll get three of those markers when we run the tests and make this bigger so you can see four five and six now if I were to use assert assert is a more strict version of expect it says if we don't pass that line don't continue so now when I run this we can see that it only flagged lines 4 & 5 because the assert it failed and so doesn't even get to line 6 you would do this if you have wanted to stop a test early like there's no sense and continuing or it might crash the test to continue running because some some condition isn't right for you to continue checking view that's where you've used an assert so there are lots of variations to these macros you can instead of saying as expect true you could say expect equals true and it's some variable or just say true right true is equal to true right and we can try it both ways we could say false is true dismiss that so we can see that line 5 says these values are expected to be equal and by convention I usually put the expected value first and then the actual value so if we were checking to see if a function returned a certain value we would say we would call we would say something like constable result equals some function f and then we will put the result here as the actual right this is just to demonstrate if we had a function f that returns something it might do that and you'll see it's slightly different it'll say result which is false so kind of it tells you that it expects true and result to be equal and the actual value of result which is false so we could fix you know it fix it like by turning that to true now it should pass right cool I don't know why it's complaining about dart configuration but we'll ignore that for now I think that has to do with some part of the C test and yeah I don't remember it's not important to this tutorial anyway that's basically it you can look at Google tests webpage here for they have more documentation they have a Google test primer which is really nice to look at and they kind of go through what were the philosophies and basic concepts and here they list the kinds of things you can assert so I went over true and there's also false and I did evil there's also not equal less than less nickel greater than greater than equal etcetera etcetera so they're all different variations and there's string versions of it it gets pretty elaborate one thing I like to do sometimes is let's say F is a esoteric function and just simply printing the result won't really tell us what's going on but if we want if we wanted to print additional information if it failed this is how you would do it you take advantage of the fact that there are operators defined by Google tests for shifting left whatever you shift left whether it's a string an object the number gets printed on the line when the test fails so if I and let's make this fail again actually let's hold yeah let's take a fail and we'll say I'll just say simply hello world when we run this test now was the failure it also prints out whatever we had here so if we had something like who made this an int and we had it returned 42 and if we said if we wanted to get the result and we expected it to equal 16 hold on no let's do it it's usually used for side effects we can't observe so let's leave it bool and we'll have we'll have it will have a global variable this is bad but just for just for illustration make a global variable called side to side effect or two too have it make a side effect change right and we'll say if it fails we'll print we will if you want to see what the value of side effect was equals that shifted out it's a bit like the C++ stream operators so now when we run it it actually prints out the actual value so that's useful like this is illustrating to print out side effects things that aren't directly observable in the assertion we can we can print later another useful place where you might have this is if this was in a loop like 4 + I is 0 to 16 for example and you want to run this you might print out that it failed in something like that so you know which iteration of the loop it failed on for example all right so enough of this this is just demonstrating the macros let's actually build up a component so what I like to do is start with the unit use case so let's say our example is dealing with computing a multiply and accumulate otherwise called an ma si let's see multiply cumulate mac test Mack so if we have eggs if we have a Mac function let's let's set up some variables so in x equals 42 into y equals 16 and and some is a hundred so what we would expect what do we expect when we were to if we were to call mac with x and y and the sum we would expect that to be equal to X a sum plus x times y right and then furthermore we would expect if we were to record the old Sun is the Sun actually we should say that here we should expect that the some is equal to this because the part of a queue I'm adding to that some so but here's a typical use case so multiply accumulate we expect the multiply accumulate to take two numbers and multiply them and add them to a sum and then also store the sum back into sum so first we expect the return value is equal to the old sum plus the x times y then we have another assumption that that same value be what's stored back into sum we could simplify this and say expected expected newsome is that that removes some repetition so anyway now our test case is set up how do we actually get this to build it's not building because a Mac is unidentified so we expect that to be in the thing that we're testing so that would be one directory up example that HTTP should define it right so let's collapse the bill directory so let's go to this example HTTP and define it so int Mac and X into y int reference some now you can see that old on oh it's strong with this one Oh forgot a comma now you can see it will build at least compiled that it won't link because it says it's unresolved we haven't given a body at to Mac let's do that next see the you can see our TDD is kind of designed backwards you design it from the test backwards so well first we the first thing we can do is simply do nothing to the wrong thing and that should build but it won't pass our tests we can see we expected 722 and we got zero so we can let's fix it the naive approach would be simply to return some plus x times y that should pass one of the tests but not the other if s is the one from line 11 but it doesn't pass the one here on I guess it's it's it doesn't pass the second one because we didn't actually do the accumulate part so we finally we passed that vice changing this around a little and say some plus equals return some now we should pass our tests and that's basically how it goes so you would continue building up tests by just simply adding more macros here so we could say we could have another test how about test square we'll have another function called square into X equals five and expected square equals x times X and then we'll expect equal expected square or square X because square doesn't exist we have to add it here int square now it's going to compile the not link because we had to make a definition for it we'll do that here initially doing the wrong thing does no matter what we do wrong because we want to see the test fail at first and it fails indeed it should have been 25 but it's actually 5 so then we fix it to do the correct thing run it again in our protest should pass yay okay so what what do you do when your tests get more and more involved let's say you had a lot of tests that had the same kind of setup well there's something called a fixture which you can employ a fixture is just simply a structure or a class I did make it a structure just because you don't need to encapsulate anything because it's all it we're all friends here in the same module and you name the structure the same as what you've been naming your tests only it's going to inherit from a special class called testing tests I get this right is a test is it underst yeah right the intellisense is helping me out it's uppercase T est so what that gives you if we look at that class really quick you gonna help me intellisense there you go it gives you a bunch of convenience things it'll to that I use most often are these setup and teardown so they're virtual functions we can override them we can do something like this it's they're both void virtual void set up right over right I don't always remember to say overwrite some compilers will give you a warning if you don't because they are virtual and teardown right yes so you can do whatever you want in here so for example if I wanted to I could have a variable and the set up might be to initialize that already - and teardown we could do other things like if we allocated memory let's say let's delegate some memory here this is a horrible thing to do but we could do this we just remember to always delete X here then in the way you would you would use this fixture and test is simply at underscore F now each one of these tests is not only a function it's also in a sense a subclass so you have access to X directly so we can remove our local X here and our local X here and then simply we could I could do this but it might be hard to read so what if we made a convenience function and we called it get X X right now we can say get X wherever we have this construct get X there there and there and their and their cool net stuff still passing and you could even do things like this printf whole world now whenever our test run I forgot to say if tests pass you won't actually see standard output so we're not going to see that nevermind anyway you get the idea I think if we did that standard error we actually might see something Yes No maybe so no anyway I bet if I run it on the terminal I'd see it so it's going to be billed example tests exam no test example tests there we go so we see it on the terminal see it in the in the test summary here because the test didn't fail so any kind of things you want to do and setup and teardown and you could do it here so I could say starting up you get the idea I might be kind of belaboring the point you build that guy turning down interesting thing is it only tells you the result of the test after we're done tearing down just kind of nice all the output for a run is is nicely bracketed like that so that gives you a basic idea on how to set of tests with Google test with fixtures and what you could do with common variables and function as helpers setup and teardown and how we break our tests up into individual cases how they're run couple of other points if you wanted to you could rename this and may take it out of the fixture back in annex now when we run it we'll see little bit interesting on the terminal actually you'll see it gets broken up into two different sections Cullum cases that's that's useful if you you usually do this when I have two different classes I want to test I'll usually put them in two different tests EPP files but you can do it in the same one like I did here because the names of these tests are different they go into different test cases and they have different bracketed sections it's also handy for if you wanted to only run test a certain test case you could have a filter that starts with example test to star and it will only run that second test case you can do any kind of regular expressions here as well it so I could say Star Mac and it runs the Mac test or a star square runs the square test so there's lots of other options you can get to cheat test and there are lots of other macros you can use with G test so I totally recommend looking at their test primer which you can get to from their get page and learn learn all the things you might find useful help hope that helped out and you got to see a little bit about Google test and see make and hope people watch me on Twitch and see again soon thanks guys
Info
Channel: rhymu8354
Views: 63,728
Rating: 4.8835979 out of 5
Keywords:
Id: Lp1ifh9TuFI
Channel Id: undefined
Length: 31min 31sec (1891 seconds)
Published: Thu Dec 27 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.