![]() ![]() The CMakeLists.txt in the test folder would look something like this: project(MyLibTests LANGUAGES CXX)Īdd_executable(MyLibTests src/MyLibTests.cpp) # you don't usually want users of your library to ![]() Include(CTest) # We include CTest which is part of CMake Next we would need to add some things to the root CMakeLists file: #. Return 0 // You can put a 1 here to see later that it would generate an error Std::cout << "Tests (0) succeeded!" << std::endl Std::cout << "Tests started!" << std::endl We will start with a simple MyLibTests.cpp file: #include We would need a code to test so I define a MyLib target that would contain some imaginary functions and then our new test code will be contained within MyLibTests target. This is also what the test frameworks do but they allow you to describe your scanarios using macros as you will see in the next section. The C/C++ code inside should test whatever functions you want to test and return a success code (0) or an error code (any other number) if the tests succeed or fail. This can be achieved by creating a new target that is executable. You just want to compile your code and see if a certain scenario(s) work. The most simple example of unit testing is without any kind of testing framework. We are mosty going to be interested in a test target as well as the root CMakeLists.txt file. So lets get to it – how do you achieve running your tests using CTest? First lets define our project structure. If you liked this content consider checking out my course on CMake where I cover this and much more. Basics of C++ language – check out “ Introduction to C++“.I would suggest you follow other articles on this site linked to that. ![]() Before you startīefore you continue with unit testing you should know: Your mindset will change and you will start to write better designed functions and classes. ![]() My proposal is that you try implement this in a small section of your code and see the benefits it brings. But this time will always pay off in the end. The only major problem is that it takes time and it requires discipline. You would mostly get benefits from writing tests for your code. Also just to be more specific if the code you write interacts with a third party API (database, library, network, filesystem, etc.) it is called an integration test because you are testing if your code works with the second API / system. That other code that automatically checks if the first one works is called an unit test. When you first write a piece of code you can write another that would automatically check if it works. Suddenly you find out that your change affected another code and created a bug on another part of the game that you forgot to check. You deploy your game and it crashes for everyone else. Suddenly you change a function somewhere – test it with the current scenario and it works. While writing it you run the code and see it works… then you add more code on top… and more code on top… and you end up with a big system. It is easy to discard this for game development too – but don’t forget that all big engines support fully unit testing so it must be useful. Many people underestimate how powerful it is to spend some boring time to write unit tests but it will really save you some frustration time at a later point in time. Unit testing is an important part of every software project. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |