Updated Supplying your own main() (markdown)

philsquared
2012-06-02 09:43:13 -07:00
parent 88ec3e0c9c
commit 644a95d3aa

@@ -1,18 +1,18 @@
The easiest way to use Catch is to let it supply main() for you and handle configuring itself from the command line. The easiest way to use CATCH is to let it supply main() for you and handle configuring itself from the command line.
This is achieved by #defining CATCH_CONFIG_MAIN before the #include for catch.hpp in exactly one source file. This is achieved by #defining CATCH_CONFIG_MAIN before the #include for catch.hpp in exactly one source file.
Sometimes, though, you'd like to write your own version of main(). You can do this by #defining CATCH_CONFIG_RUNNER instead. Sometimes, though, you'd like to write your own version of main(). You can do this by #defining CATCH_CONFIG_RUNNER instead.
Catch then supports this in three ways: CATCH then supports this in three ways:
1. Forward onto Catch's main but write code before and/ or after for global setup/ cleanup purposes. Catch will still configure itself from the command line as before. 1. Forward onto CATCH's main but write code before and/ or after for global setup/ cleanup purposes. CATCH will still configure itself from the command line as before.
2. As above but your can programatically set parts, or all, of Catch's configuration. 2. As above but your can programatically set parts, or all, of CATCH's configuration.
3. Set configuration and run specific tests directly from your own code. 3. Set configuration and run specific tests directly from your own code.
Let's look at each of these in a little more detail. Let's look at each of these in a little more detail.
# Wrapping Catch's main() # Wrapping CATCH's main()
```c++ ```c++
#define CATCH_CONFIG_RUNNER #define CATCH_CONFIG_RUNNER
@@ -30,7 +30,7 @@ int main (int argc, char* const argv[])
} }
``` ```
# Configuring Catch # Configuring CATCH
```c++ ```c++
#define CATCH_CONFIG_RUNNER #define CATCH_CONFIG_RUNNER
@@ -41,12 +41,12 @@ int main (int argc, char* const argv[])
// Create a default config object // Create a default config object
Catch::Config config; Catch::Config config;
Configure Catch to send all its output to a stringstream // Configure CATCH to send all its output to a stringstream
std::ostringstream oss; std::ostringstream oss;
config.setStreamBuf( oss.rdbuf() ); config.setStreamBuf( oss.rdbuf() );
// Forward on to Catch's main, but using our custom config. // Forward on to CATCH's main, but using our custom config.
// Catch will still parse the command line and set the config // CATCH will still parse the command line and set the config
// object up further // object up further
int result = Catch::Main( argc, argv, config ); int result = Catch::Main( argc, argv, config );
@@ -67,10 +67,10 @@ int main (int argc, char* const argv[])
// Create a default config object // Create a default config object
Catch::Config config; Catch::Config config;
// Configure Catch to run all tests starting with "mytests" // Configure CATCH to run all tests starting with "mytests"
config.addTestSpec( "mytests/*" ); config.addTestSpec( "mytests/*" );
// Forward on to Catch's main using our custom config. // Forward on to CATCH's main using our custom config.
// This overload doesn't take command line arguments // This overload doesn't take command line arguments
// So the config object must be fully set up // So the config object must be fully set up
return Catch::Main( config ); return Catch::Main( config );