Skip to content

Instantly share code, notes, and snippets.

@exklamationmark
Last active May 6, 2018 05:13
Show Gist options
  • Save exklamationmark/3b83f80721e57c153daa250d93e5c1c2 to your computer and use it in GitHub Desktop.
Save exklamationmark/3b83f80721e57c153daa250d93e5c1c2 to your computer and use it in GitHub Desktop.
Create and run C++ unit tests with Boost
#include "hello.h"
#include <string>
namespace mine {
std::string hello() {
return "aha";
}
}
#include <string>
namespace mine {
std::string hello();
}
#define BOOST_TEST_MAIN
#include "hello.h"
#include <boost/test/included/unit_test.hpp>
BOOST_AUTO_TEST_CASE(test_hello)
{
BOOST_REQUIRE_EQUAL("Hello, World!", mine::hello());
}
#!/bin/bash
# This is how I managed to setup Boost and C++ on Ubuntu 16.04.
# It's meant for people who are trying to setup for the first time.
# I assume you are somewhat familar with Linux, so if any steps seems like magic, please ask for explanation.
# Let's work within /tmp/
cd /tmp
# This download the file `/tmp/boost_1_67_0.tar.bz2`
curl -sLO https://dl.bintray.com/boostorg/release/1.67.0/source/boost_1_67_0.tar.bz2
# Unpack boost's source code
tar -xvf boost_1_67_0.tar.bz2
# Move the code to /usr/local/boost_1_67_0.
# You will now refer to it when asked for $BOOST_ROOT
sudo mv boost_1_67_0 /usr/local/
# Let's build Boost
cd /usr/local/boost_1_67_0
./bootstrap.h --with-libraries=all --prefix=/usr/local
sudo ./b2 install
# and wait for a long time
# If you are successful, checkout your /usr/local/lib folder,
# You should find a lot of `.a` and `.so` files prefixed with `libboost_`
#!/bin/bash
# compile and generate the test program
g++ -I /usr/local/boost_1_67_0/ -L /usr/local/boost_1_67_0/stage/lib/ -o test hello.h hello.cpp hello_test.cpp
# run it
./test
# Running 1 test case...
# hello_test.cpp(7): fatal error: in "test_hello": critical check "Hello, World!" == mine::hello() has failed [Hello, World! != aha]
#
# *** 1 failure is detected in the test module "Master Test Suite"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment