Getting all test suites / test cases

As the name says, I want to get all test suites or test cases (name) from a test application, broadcast in the console, or as XML output. Test environment is a test testing library.

Is there any way to achieve this? I did not find anything useful in the documentation.

+4
source share
3 answers

This can be done without much intervention using a global device . Assuming you have a translation unit (cpp file) that contains mainexplicitly or automatically generated, you can intercept the test execution when a specific command line argument is provided. You can then go through the test tree with a personalized visitor listing all available tests. Here is a small working example that creates a test runner by compiling and linking files main_test.cpp, a.cppand b.cpp:

main_test.cpp

#include <string>
#include <iostream>

// --- Boost Includes ---
#define BOOST_TEST_MODULE MyTestSuite
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>

using namespace boost::unit_test;

struct Visitor : test_tree_visitor
{
  size_t level = 0;

  void visit( test_case const& test )
  {
    std::string indentation( level, '.' );

    std::cout << indentation << test.p_name << std::endl;
  }

  bool test_suite_start( test_suite const& suite )
  {
    std::string indentation( level, '.' );
    level++;

    std::cout << indentation << "Suite: " << suite.p_name << std::endl;
    return true;
  }

  void test_suite_finish( test_suite const& suite )
  {
    level--;
  }
};

struct GlobalFixture
{
  GlobalFixture( )
  {
    int argc = framework::master_test_suite( ).argc;
    for ( int i = 0; i < argc; i++ )
    {
      std::string argument( framework::master_test_suite( ).argv[i] );

      if ( argument == "list" )
      {
        Visitor visitor;

        traverse_test_tree( framework::master_test_suite( ), visitor );

        exit( EXIT_SUCCESS );
      }
    }
  }

};

BOOST_GLOBAL_FIXTURE( GlobalFixture )

a.cpp

#include <boost/test/unit_test.hpp>

BOOST_AUTO_TEST_SUITE ( TestA )

BOOST_AUTO_TEST_CASE ( TestFoo )
{
  BOOST_CHECK(true);
}

BOOST_AUTO_TEST_CASE ( TestBar )
{
  BOOST_CHECK(true);
}

BOOST_AUTO_TEST_SUITE_END() // TestA

b.cpp

#include <boost/test/unit_test.hpp>

BOOST_AUTO_TEST_SUITE ( TestB )

BOOST_AUTO_TEST_CASE ( TestFoo )
{
  BOOST_CHECK(true);
}

BOOST_AUTO_TEST_CASE ( TestBar )
{
  BOOST_CHECK(true);
}

BOOST_AUTO_TEST_SUITE_END() // TestA

Calling a runner without any arguments results in

./somelib_testrunner1 
Running 4 test cases...

*** No errors detected

Passing the argument listused in the above device results in

Suite: MyTestSuite
.Suite: TestA
..TestFoo
..TestBar
.Suite: TestB
..TestFoo
..TestBar
+2
source

, .

, , , --log_level=test_suite - script, " ", " " " ", ( xml, --log_format=XML, xml).

BOOST_TEST_MESSAGE , , .

, - , , , , , , , , , , , .

+1
+1

All Articles