Arya Pourtabatabaie
Arya Pourtabatabaie

Reputation: 734

Using Catch2 in Visual C++ 2015

I'm trying to make a Unit Testing project using the Catch framework, but am faced with Link errors.

I've set up the project as follows:

  1. Create a Native Unit Test project
  2. Add Catch to the include directories
  3. Add #include <catch.hpp> to stdafx.h
  4. Write the following simple source file

unittest.cpp:

#include "stdafx.h"
namespace Catch2_Test
{
  TEST_CASE("Y U no work")
  {
    REQUIRE(1);
  }
}

Upvotes: 0

Views: 5669

Answers (2)

caoanan
caoanan

Reputation: 585

For integrating Catch into Visual Studio
refer the ACCU article Integrating the Catch Test Framework into Visual Studio, by Malcolm Noyes

For using Catch2 in pre-compiled headers,
refer Catch2 issue 1061, where horenmar gave an example. The changes have been released as part of v2.1.0

In summary, the solution given is:

// stdafx.h
#pragma once

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>

#define CATCH_CONFIG_ALL_PARTS
#include "catch.hpp"

// PCH-test.cpp:
#include "stdafx.h"

#undef TWOBLUECUBES_SINGLE_INCLUDE_CATCH_HPP_INCLUDED
#define CATCH_CONFIG_IMPL_ONLY
#define CATCH_CONFIG_MAIN
#include "catch.hpp"

// tests1.cpp:
#include "stdafx.h"

TEST_CASE("FooBarBaz") {
    REQUIRE(1 == 2);
}

Upvotes: 2

Arya Pourtabatabaie
Arya Pourtabatabaie

Reputation: 734

The problem was that I had cloned Catch2 from the master branch, while the VS integration worked on a branch off Catch.

Upvotes: 1

Related Questions