Search code examples
c++unit-testingboost

c++ Using boost test


How can I convert the following code to use boost unit test framework:

#include <iostream>
#include <fstream>

#include "graph.hh"

int main(int argc, char **argv) {
  const char* ifile = argv[1];

  Graph gp;
  gp.read_xml(ifile);

  std::cout << "Checking number of nodes and edges..."  << std::endl;
  int nodes_expected = 16;
  if(nodes_expected != gp.nodes()) {
    std::cout << "Test Failed." << std::endl;
    std::cout << "Expected: " << nodes_expected << std::endl;
    std::cout << "Result: " << gp.nodes() << std::endl;
  }
  int edges_expected = 15;
  if(edges_expected != gp.edges()) {
    std::cout << "Test Failed." << std::endl;
    std::cout << "Expected: " << edges_expected << std::endl;
    std::cout << "Result: " << gp.edges() << std::endl;
  }
  return 0;
}

I've read the documentation at (Boost Test), but it doesn't tell me how to ingest arguments from the command line. Otherwise, I could probably use BOOST_CHECK_EQUAL.


Solution

  • #include <boost/test/included/unit_test.hpp>
    #include <fstream>
    #include "graph.hh"
    using namespace boost::unit_test;
    
    BOOST_AUTO_TEST_CASE( test_num_of_nodes )
    {
      Graph gp;
      gp.read_xml( framework::master_test_suite().argv[1] );
    
      BOOST_MESSAGE( "Checking number of nodes and edges..." );
    
      BOOST_CHECK_EQUAL(16, gp.nodes());
      BOOST_CHECK_EQUAL(15, gp.edges());
    }