Basics
Note: YOU CANNOT HAVE YOUR OWN MAIN(), SINCE BOOST CREATES ONE OF ITS OWN
if you want to take a different approach to the one showed here, refer to 2.2.2 / 2.2.1 https://now.ntu.ac.uk/d2l/le/content/793918/viewContent/6628975/View
INCLUDE THIS AT COMPILE TIME (the bit when you make object files)
-lboost_unit_test_frameworkInclude
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE MyModuleName
#include </usr/local/Cellar/boost/1.76.0/include/boost/test/included/unit_test.hpp>Testing
BOOST_AUTO_TEST_CASE( testName )
{
BOOST_CHECK_EQUAL( funcName(0) , 0 );
}
// funcName is the name of the c++ function you are testing
// testName is the name of the test that you have madeExample
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE MyModuleName
#include </usr/local/Cellar/boost/1.76.0/include/boost/test/included/unit_test.hpp>
int sqr(int x)
{
return x*x;
}
BOOST_AUTO_TEST_CASE( twoSquared )
{
BOOST_CHECK_EQUAL( sqr(2) , 4 );
}
BOOST_AUTO_TEST_CASE( fourSquared )
{
BOOST_CHECK_EQUAL( sqr(4) , 16 );
}More Realistic Example
Remember there can only be one Main function, therefore we cannot have boost in our main program
sqr.cpp
sqr.h
#include "sqr.h"
int sqr(int x)
{
return x*x;
}#ifndef SQR_H
#define SQR_H
int sqr(int);
#endifautomated-test.cpp
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE SqrTestModule
#include <boost/test/unit_test.hpp>
#include "sqr.h"
BOOST_AUTO_TEST_CASE( zeroSquared )
{
BOOST_CHECK_EQUAL( sqr(0) , 0 );
}