For example I have following test case:
#include <MyClass.hpp>
#define BOOST_TEST_MODULE MyTest
#include <boost/test/unit_test.hpp>
BOOST_AUTO_TEST_CASE( my_test )
{
MyClass o1(42), o2(21);
BOOST_CHECK( o1.is_valid() );
BOOST_CHECK_EQUAL( o1 == o2 * 2 );
BOOST_CHECK_EQUAL ...
...
}
There are several similar classes that had implemented same methods, would like to test them by the same logic, test cases might be like following:
BOOST_AUTO_TEST_CASE( my_test1 )
{
MyClass1 o1(42), o2(21);
BOOST_CHECK( o1.is_valid() );
BOOST_CHECK_EQUAL( o1 == o2 * 2 );
BOOST_CHECK_EQUAL ...
...
}
BOOST_AUTO_TEST_CASE( my_test2 )
{
MyClass2 o1(42), o2(21);
BOOST_CHECK( o1.is_valid() );
BOOST_CHECK_EQUAL( o1 == o2 * 2 );
BOOST_CHECK_EQUAL ...
...
}
BOOST_AUTO_TEST_CASE( my_test3 )
{
MyClass3 o1(42), o2(21);
BOOST_CHECK( o1.is_valid() );
BOOST_CHECK_EQUAL( o1 == o2 * 2 );
BOOST_CHECK_EQUAL ...
...
}
...
Is there a way to reuse logic in the test case?
Check out template test cases.
#include <MyClass.hpp>
#define BOOST_TEST_MODULE MyTest
#include <boost/test/included/unit_test.hpp>
#include <boost/mpl/list.hpp>
typedef boost::mpl::list<MyClass1,MyClass2,MyClass3> test_types;
BOOST_AUTO_TEST_CASE_TEMPLATE( my_test, T, test_types )
{
T o1(42), o2(21);
BOOST_CHECK( o1.is_valid() );
BOOST_CHECK_EQUAL( o1 == o2 * 2 );
BOOST_CHECK_EQUAL ...
...
}
Related
Here is the code.
// .
#include <stdio.h>
#include <memory>
#include <vector>
#define CAT_(a, b) a##b
#define CAT(a, b) CAT_(a, b)
#define REG(x) inline auto CAT(unused_name_, __LINE__ ) = tu_register(x)
#define LNE printf("%d", __LINE__ )
typedef void (*fp_required)( ) ;
using fvec_type = std::vector< fp_required > ;
static fvec_type fvec{};
inline bool tu_register( fp_required const & fp ){
printf("\nRegistered: %p" , fp ) ;
fvec.push_back( fp ) ;
return true ;
}
REG([]{ LNE; }) ; // 3 stateless lambdas
REG([]{ LNE; }) ;
REG([]{ LNE; }) ;
int main()
{
printf("\n\nRegistered %lu lambdas. %s\n", fvec.size(), (fvec.size() > 0 ? "Now calling them": ""));
for ( auto & fun : fvec ) {
printf("\nCalling lambda %p , rezult: " , fun);
fun() ;
}
return 0;
}
CLANG compiles with no warnings, but places exactly nothing in the vector?
https://wandbox.org/permlink/nkYjgqvr5QOprKEn
G++ compiles with no warnings, and works as expected.
https://wandbox.org/permlink/a6HB6xzavE8FOyOi
MSVC (the latest VS2019, all up to date) too, compiles and works no problems.
Who is right?
That code should work, and in Clang 8.0.0, it does. Looks like it was broken after that, and doesn't work in 9.0.0 nor 10.0.0 on Wandbox.
As the title says, I want to get all test suites or test cases (name) from a test application, ether in the console or as xml output.
Test framework is the boost test library.
Is there an option to achieve this? I did not found anything useful in the documentation.
There is simply the --list_content command line option, exactly for that purpose.
This can be done without much intrusion using a global fixture. Assuming you have a translation unit (cpp file) that contains main explicitly or auto generated, you can intercept test execution when a certain command line argument is provided. Then you can traverse the test tree using a customized visitor, which lists all available tests. Here is a small working example, which creates a test runner by compiling and linking files main_test.cpp, a.cpp and 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
Invoking the runner without any arguments results in
./somelib_testrunner1
Running 4 test cases...
*** No errors detected
Passing the argument list used in the fixture defined above results in
Suite: MyTestSuite
.Suite: TestA
..TestFoo
..TestBar
.Suite: TestB
..TestFoo
..TestBar
I am afraid there is no easy way to do this without actually running the tests.
And even then the only thing coming to my mind without touching the code would be --log_level=test_suite and some sort of parsing script that finds the "Entering test suite", "Leaving test suite" and "Entering test case" messages (or parsing the xml given when you also put --log_format=XML, if you have a xml parser at hand).
But even that spoils the log with your added BOOST_TEST_MESSAGEs and of course encountered errors and warnings.
Seeing how the boost test framework is not much more than a bunch of macros, defined in the header, you could of course add the functionality yourself, probably easiest by adding another log format just listing the suites and tests, assuming you are fine with running the tests while finding the list.
I'm trying to use boost.test on a remote system with boost 1.33.1. On my pc this little example from http://www.boost.org/doc/libs/1_42_0/libs/test/doc/html/tutorials/hello-the-testing-world.html works:
#define BOOST_TEST_MODULE MyTest
#include <boost/test/included/unit_test.hpp> // I've changed here
int add( int i, int j ) { return i+j; }
BOOST_AUTO_TEST_CASE( my_test ) // <--- line 7
{
// seven ways to detect and report the same error:
BOOST_CHECK( add( 2,2 ) == 4 ); // #1 continues on error
BOOST_REQUIRE( add( 2,2 ) == 4 ); // #2 throws on error
if( add( 2,2 ) != 4 )
BOOST_ERROR( "Ouch..." ); // #3 continues on error
if( add( 2,2 ) != 4 )
BOOST_FAIL( "Ouch..." ); // #4 throws on error
if( add( 2,2 ) != 4 ) throw "Ouch..."; // #5 throws on error
BOOST_CHECK_MESSAGE( add( 2,2 ) == 4, // #6 continues on error
"add(..) result: " << add( 2,2 ) );
BOOST_CHECK_EQUAL( add( 2,2 ), 4 ); // #7 continues on error
}
but on the remote system the file unit_test.hpp doesn't exist. On my pc the file unit_test_framework.hpp is simply:
// deprecated
#include <boost/test/included/unit_test.hpp>
and it is present on the main system. So I tried to change the include to:
#include <boost/test/included/unit_test_framework.hpp>
but the compiler says:
main.cpp:7: error: expected constructor, destructor, or type conversion before ‘(’ token
what's this? How to solve it?
On Boost 1.33 use:
#include <boost/test/auto_unit_test.hpp>
in place of:
#include <boost/test/unit_test.hpp>
and also before the #include add:
#define BOOST_AUTO_TEST_MAIN
or you'll get a linker error
If your version of boost is older than 1.33, you should try renaming BOOST_AUTO_TEST_CASE to BOOST_AUTO_UNIT_TEST, and it shouldn't break compilation on newer versions of boost.
See these Boost.Test 1.33 Release Notes :
BOOST_AUTO_UNIT_TEST renamed to
BOOST_AUTO_TEST_CASE. Old name still
provided but deprecated
What's the boost version on your target platform? Are you using an old version there?
Since you are using a header only version of boost.test (you include the boost/test/included/unit_test.hpp header and not boost/test/unit_test.hpp), can't you just copy the working boost installation from your PC to the target machine and instruct your compiler to use it?
Why this (copied from boost site ) gives me an error?! in VS 2010 Ultimate:
#include <boost\test\unit_test.hpp>
#define BOOST_TEST_MODULE MyTest
int add( int i, int j ) { return i+j; }
BOOST_AUTO_TEST_CASE( my_test )
{
// seven ways to detect and report the same error:
BOOST_CHECK( add( 2,2 ) == 4 ); // #1 continues on error
BOOST_REQUIRE( add( 2,2 ) == 4 ); // #2 throws on error
if( add( 2,2 ) != 4 )
BOOST_ERROR( "Ouch..." ); // #3 continues on error
if( add( 2,2 ) != 4 )
BOOST_FAIL( "Ouch..." ); // #4 throws on error
if( add( 2,2 ) != 4 ) throw "Ouch..."; // #5 throws on error
BOOST_CHECK_MESSAGE( add( 2,2 ) == 4, // #6 continues on error
"add(..) result: " << add( 2,2 ) );
BOOST_CHECK_EQUAL( add( 2,2 ), 4 ); // #7 continues on error
}
Error
Error 1 error LNK2019: unresolved external symbol "class boost::unit_test::test_suite * __cdecl init_unit_test_suite(int,char * * const)" (?init_unit_test_suite##YAPAVtest_suite#unit_test#boost##HQAPAD#Z) referenced in function _main
Try defining BOOST_TEST_MODULE before you include the header.
Does anyone know of a library which will assist in decoding cron style timings, i.e.
30 7 * * 1-5
Which is 7:30am every Monday, Tuesday, Wednesday, Thursday, Friday.
M.
There is a library for PHP, Perl but I never saw one for C++.
The good thing is that Cron's source is freely available and you can reuse its code to parse entries in the cron format.
The entry data structure is defined in "cron.h" file:
typedef struct _entry {
struct _entry *next;
uid_t uid;
gid_t gid;
char **envp;
char *cmd;
bitstr_t bit_decl(minute, MINUTE_COUNT);
bitstr_t bit_decl(hour, HOUR_COUNT);
bitstr_t bit_decl(dom, DOM_COUNT);
bitstr_t bit_decl(month, MONTH_COUNT);
bitstr_t bit_decl(dow, DOW_COUNT);
int flags;
#define DOM_STAR 0x01
#define DOW_STAR 0x02
#define WHEN_REBOOT 0x04
#define MIN_STAR 0x08
#define HR_STAR 0x10
} entry;
And there are two functions you need from "entry.c" file (too large to post code here):
void free_entry (e);
entry *load_entry (file, error_func, pw, envp);
You can compile those files into a shared library or object files and use directly in your project.
This is an example of getting cron source code in Debian (Ubuntu):
apt-get source cron
You can also download it from http://cron.sourcearchive.com/
For those that wish to achieve the same goal as #ScaryAardvark
Dependency:
http://cron.sourcearchive.com/downloads/3.0pl1/cron_3.0pl1.orig.tar.gz
Build:
gcc -o main main.c cron-3.0pl1.orig/entry.c cron-3.0pl1.orig/env.c
cron-3.0pl1.orig/misc.c -I cron-3.0pl1.orig
Source:
#include <pwd.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <uuid/uuid.h>
#define MAIN_PROGRAM 1
#include "cron-3.0pl1.orig/cron.h"
void error_handler( char* message )
{
fprintf( stderr, "Error: %s\n", message );
}
void print_entry( const entry* e )
{
fprintf( stdout, "uid: %i\n", e->uid );
fprintf( stdout, "gid: %i\n", e->gid );
fprintf( stdout, "command: %s\n", e->cmd);
//etc...
}
int main( int argc, char** argv, char** envp )
{
const char* filename = "crontab";
const char* username = "bcrowhurst";
//Retreive Crontab File
FILE *file = fopen( filename, "r" );
if ( file == NULL )
{
error_handler( strerror( errno ) );
return EXIT_FAILURE;
}
//Retreive Password Entry
struct passwd *pw = getpwnam( username );
if ( pw == NULL )
{
error_handler( strerror( errno ) );
return EXIT_FAILURE;
}
//Read Entry
entry *e = load_entry( file, &error_handler, pw, envp );
if ( e == NULL )
{
error_handler( "No entry found!" );
return EXIT_FAILURE;
}
print_entry( e );
//Clean-up
fclose( file );
free_entry( e );
return EXIT_SUCCESS;
}
Example Crontab
#yearly /home/bcrowhurst/annual-process
*/10 * * * * /home/bcrowhurst/fschk