17

I've never written a unit test or any test for my C++ programs. I know just that they are meant to testing if the function/program/unit does exactly what you think it does, but I have no idea how to write one.

Can anybody help me with a test for my example function?

void doMode(int i) {
    int a = fromString<int>(Action[i][1]);
    int b = fromString<int>(Action[i][2]);

    std::cout << "Parameter:\t" << a << "\t" << b << "\t" << std::endl;
    Sleep(200);

    return;
}

I'm not asking for a framework. I just don't know where and how to start.

  • What is the syntax I have to use?
  • Is it different depending on which framework I use?
  • Do I write tests for every function of my code and all the branches, or just the ones I think can be tricky?
VLL
  • 9,634
  • 1
  • 29
  • 54
tinkerbell
  • 229
  • 1
  • 2
  • 9
  • I would check out [this thread](https://www.reddit.com/r/cpp/comments/4e9afx/most_popular_c_unit_testing_frameworks/), choose a framework, read the docs and examples (this will explain what a test framework is), then implement a couple of tests. I you have specific questions then, post a new question here. This question in the current form is not specific enough. – lubgr Sep 11 '18 at 09:47
  • 1
    Possible duplicate of [Comparison of c++ unit test frameworks](https://stackoverflow.com/questions/242926/comparison-of-c-unit-test-frameworks) – Sander De Dycker Sep 11 '18 at 09:48
  • @lubgr but I have no idea where and how to start – tinkerbell Sep 11 '18 at 09:49
  • the indicated duplicate question also has lots of links with reading material about how to write unit tests. – Sander De Dycker Sep 11 '18 at 09:49

3 Answers3

21

Without a framework

This is how you would write a unit test without a framework.

#include <iostream>

// Function to test
bool function1(int a) {
    return a > 5;   
}

// If parameter is not true, test fails
// This check function would be provided by the test framework
#define IS_TRUE(x) { if (!(x)) std::cout << __FUNCTION__ << " failed on line " << __LINE__ << std::endl; }

// Test for function1()
// You would need to write these even when using a framework
void test_function1()
{
    IS_TRUE(!function1(0));
    IS_TRUE(!function1(5));
    IS_TRUE(function1(10));
}

int main(void) {
    // Call all tests. Using a test framework would simplify this.
    test_function1();
}

With Catch2 framework

This is how you would write the same test using Catch2, a simple header-only framework:

main.cpp

#define CATCH_CONFIG_RUNNER
#include "Catch2/catch.hpp"

#include <iostream>

int main(int argc, char* argv[])
{
    const int retval = Catch::Session().run(argc, argv);
    std::cin.get();
    
    return retval;
}

test.cpp

#include "Catch2/catch.hpp"

// Assuming the function under test is in this file
#include "function1.h"

TEST_CASE("function1", "function1")
{
    REQUIRE_FALSE(function1(0));
    REQUIRE_FALSE(function1(5));
    REQUIRE(function1(10));
}

Note: Neither of these files needs a header file. You can simply add the cpp file to your project, and as long as it has TEST_CASE, it is executed.

VLL
  • 9,634
  • 1
  • 29
  • 54
3

You could start with no other library at all, just write a program that calls your function with different values and check the result if it matches the expectation.

Which will lead you to the insight that this function (with this design) is not easy to test ...

A test framework usually

  • takes away the burden of writing a main() function that calls all the tests
  • allows to write multiple test functions
  • provide features like test-setup per test program/per test function
  • and much more

"What to test" is always a good start for lengthy discussions. I would say, in an ideal world, yes, you test everything. In existing projects, start by writing tests for things that you add/change. In new projects, test as much as you can, maybe use TDD, use code coverage tools to check what you missed.

Rene
  • 2,466
  • 1
  • 12
  • 18
1

You should really read some introduction to unit testing (e.g this. But to give you some hints and answer some of your questions.

Choosing a test framework:
You can take whatever you prefer, but if you don't know which one to choose I would suggest to try GoogleTest which is very popular and has a nice documention.

Do I write tests for every function of my code and all the branches, or just the ones I think can be tricky?

You should write tests for every function and every branch of your code.

FreshD
  • 2,914
  • 2
  • 23
  • 34
  • 1
    I'd like to add, that while you "should", in any non-trivial program you *can't* write tests for every branch (with exception of extreme projects where failure is not an option and cost is not an objection). – hyde Sep 11 '18 at 11:20