Skip to content

Instantly share code, notes, and snippets.

@juanpabloaj
Last active October 21, 2016 17:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juanpabloaj/b7cf73e781616a62d8332918a070e6bb to your computer and use it in GitHub Desktop.
Save juanpabloaj/b7cf73e781616a62d8332918a070e6bb to your computer and use it in GitHub Desktop.
catch_unittest_example

Usage

make

Output

g++ to_test.cpp pass_tests.cpp -o pass_tests && ./pass_tests && rm pass_tests
===============================================================================
All tests passed (2 assertions in 1 test case)

g++ to_test.cpp fail_tests.cpp -o fail_tests && ./fail_tests && rm fail_tests

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fail_tests is a Catch v1.5.7 host application.
Run with -? for options

-------------------------------------------------------------------------------
Factorials are computed
-------------------------------------------------------------------------------
fail_tests.cpp:6
...............................................................................

fail_tests.cpp:8: FAILED:
  REQUIRE( Factorial(3) == 10 )
with expansion:
  6 == 10

===============================================================================
test cases: 1 | 1 failed
assertions: 2 | 1 passed | 1 failed
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
#include "to_test.hpp"
TEST_CASE( "Factorials are computed", "[factorial]" ){
REQUIRE( Factorial(1) == 1 );
REQUIRE( Factorial(3) == 10 );
}
CC = g++
PASSTEST=pass_tests
FAILTEST=fail_tests
test:
[ -f catch.hpp ] || wget https://raw.githubusercontent.com/philsquared/Catch/master/single_include/catch.hpp
$(CC) to_test.cpp $(PASSTEST).cpp -o $(PASSTEST) && ./$(PASSTEST) && rm $(PASSTEST)
$(CC) to_test.cpp $(FAILTEST).cpp -o $(FAILTEST) && ./$(FAILTEST) && rm $(FAILTEST)
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
#include "to_test.hpp"
TEST_CASE( "Factorials are computed", "[factorial]" ){
REQUIRE( Factorial(1) == 1 );
REQUIRE( Factorial(3) == 6 );
}
// https://github.com/philsquared/Catch/blob/master/docs/tutorial.md
#include "to_test.hpp"
unsigned int Factorial( unsigned int number) {
return number <= 1 ? number: Factorial(number-1)*number;
}
unsigned int Factorial( unsigned int number);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment