Skip to content

Latest commit

 

History

History
153 lines (112 loc) · 5.8 KB

UsingCatch.md

File metadata and controls

153 lines (112 loc) · 5.8 KB

Using Approval Tests With Catch

Contents

Introduction

The Catch2 test framework works well with Approval Tests.

This section describes the various ways of using Approval Tests with Catch2.

Notes pre-v.10.8.0:

Earlier versions of Approval Tests had issues with Ninja. Read more at Troubleshooting Misconfigured Build.

Requirements

Approval Tests requires that a file called the following is found:

#include <catch2/catch.hpp>

snippet source | anchor

(Before v7.0.0, it required Catch.hpp)

Getting Started With Catch2

Starter Project

The quickest way to start experimenting with Approval Tests is to:

  1. Download the project ApprovalTests.cpp.StarterProject - via the green "Clone or Download" button at the top-right of the project site.
  2. Opening the project in the C++ IDE of your choice.

Each time we release a new version of Approval Tests, we update this project, so it always has the latest features.

New Project

Create a file main.cpp and add just the following two lines:

// main.cpp:
#define APPROVALS_CATCH // This tells Approval Tests to provide a main() - only do this in one cpp file
#include "ApprovalTests.hpp"

snippet source | anchor

Existing Project - with CATCH_CONFIG_MAIN

If you have a Catch2 project with your own main.cpp that contains the following lines, you will need to replace them with the code in the previous section.

#define CATCH_CONFIG_MAIN // remove these lines, and replace with Approval Tests lines
#include "catch2/catch.hpp"

Existing Project - with your main()

If you have supplied your own main() for Catch, you will need to teach it how to supply test names to Approval Tests.

You should make the following additions to your own source file that contains main().

// Add these two lines to the top of your main.cpp file:
#define APPROVALS_CATCH_EXISTING_MAIN
#include "ApprovalTests.hpp"

snippet source | anchor

Code to copy for your first Catch2 Approvals test

Here is sample code to create your main() function, to set up Approval Tests' Catch2 integration.

We called this file catch2_starter_main.cpp:

#define APPROVALS_CATCH
#include "ApprovalTests.hpp"

// This puts "received" and "approved" files in approval_tests/ sub-directory,
// keeping the test source directory tidy:
auto directoryDisposer =
    ApprovalTests::Approvals::useApprovalsSubdirectory("approval_tests");

snippet source | anchor

Here is sample code to create your first test. We called this file catch2_starter_test.cpp:

#include "catch2/catch.hpp"
#include "ApprovalTests.hpp"

TEST_CASE("catch2_starter sample")
{
    // TODO Replace 42 with the value or object whose contents you are verifying.
    // For help, see:
    // https://approvaltestscpp.readthedocs.io/en/latest/generated_docs/ToString.html
    ApprovalTests::Approvals::verify(42);
}

snippet source | anchor

And finally, here is sample code to put in your CMakeLists.txt file:

set(EXE_NAME catch2_starter)
set(CMAKE_CXX_STANDARD 11)
add_executable(${EXE_NAME}
        catch2_starter_main.cpp
        catch2_starter_test.cpp
        )
target_link_libraries(${EXE_NAME} ApprovalTests::ApprovalTests Catch2::Catch2)

add_test(NAME ${EXE_NAME} COMMAND ${EXE_NAME})

snippet source | anchor


Back to User Guide