How to use UnitTest++ installed with apt in Ubuntu

962 views Asked by At
$ sudo apt install libunittest++-dev

After that

$ sudo find / -iname "*UnitTest++.*" 2> /dev/null
/usr/include/UnitTest++/UnitTest++.h
/usr/lib/x86_64-linux-gnu/pkgconfig/UnitTest++.pc
/usr/lib/x86_64-linux-gnu/libUnitTest++.so
/usr/lib/x86_64-linux-gnu/libUnitTest++.so.2
/usr/lib/x86_64-linux-gnu/libUnitTest++.so.2.0.0

But there's no libUnitTest++.a as I have in Windows where I compiled UnitTest++ myself according the instructions here. Also I don't see the source code files.

Can I use this installation for my testing or need to download the sources and compile them myself? Or how can I use libUnitTest++.so instead of libUnitTest++.a?

Here is one of my tests:

#include "UnitTest++/UnitTest++.h"
#include "skip_list.cpp"
#include <iostream>

using namespace std;

TEST(NodeTest) 
{
    SkipListNode<int> node(15, 2);

    CHECK_EQUAL(15, node.key);
    CHECK_EQUAL(2, node.height);
    CHECK(!node.next[0]);
    CHECK(!node.next[1]);
    CHECK(!node.next[2]);
}

int main(int, const char *[])
{
   return UnitTest::RunAllTests();
}

Now it says:

g++ -std=c++11 -Wall -g  skip_list_test.cpp
/tmp/cc2zCui4.o: In function `TestNodeTest::RunImpl() const':
/home/greg/study/data_structures/02_03_01_skip_list/skip_list_test.cpp:16: undefined reference to `UnitTest::CurrentTest::Details()'
2

There are 2 answers

6
Patrick Johnmeyer On BEST ANSWER

Just as you did in your answer, you need to tell g++ to include the library. There, you provide it with the full path to the libUnitTest++.a archive file that you compiled yourself as an input.

To use the library installed by your system package manager -- in this case, apt -- it would be more common to provide a linker directive. In this case, -l UnitTest++ instructs the linker to "search my system library paths for libUnitTest++.so"

$ g++ -std=c++11 -Wall -g  skip_list_test.cpp -l UnitTest++ -o skip_list_test.out
$ ./skip_list_test.out
Success: 1 tests passed.
Test time: 0.00 seconds.
1
Nick Legend On

As there's no answer, removed the UnitTest++ deb-packages:

$ sudo apt remove libunittest++2 libunittest++-dev

Cloned the Git project and compiled it according the instructions above. Now it works:

g++ -std=c++11 -Wall -g  -c -I/usr/local/include/ skip_list_test.cpp
g++ -std=c++11 -Wall -g  skip_list_test.o /usr/local/lib/libUnitTest++.a -o skip_list_test.out
./skip_list_test.out
Success: 2 tests passed.
Test time: 0.00 seconds.

And as for the installed artifacts:

$ sudo find / -iname "*UnitTest++.*" 2> /dev/null
/usr/local/include/UnitTest++/UnitTest++.h
/usr/local/lib/pkgconfig/UnitTest++.pc
/usr/local/lib/libUnitTest++.a

Anyway any other answers are welcome :)