0

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);
  }
}
Arya Pourtabatabaie
  • 705
  • 2
  • 7
  • 22
  • Some things: 1. Go read the catch tutorial. It shows you how to set things up. Somewhere you need to `#include ` with a macro defined (I think it's `CATCH_CONFIG_MAIN`). This file would need to not use the precompiled header. 2. Don't use a Native Unit Test project for this; those projects are designed to be used with Visual Studio's unit testing library – Justin Nov 06 '17 at 21:34
  • @Justin I'm aware of that. I was following the Catch tutorial itself on how to integrate it into VC++ so that the Test Explorer could be used rather than the console. – Arya Pourtabatabaie Nov 06 '17 at 21:37
  • 1
    @AryaPourtabatabaie AFAIK, that can't be done at the moment. [It's on the radar for the Visual Studio team, though](https://blogs.msdn.microsoft.com/vcblog/2017/05/10/unit-testing-and-the-future-announcing-the-test-adapter-for-google-test/). – Justin Nov 06 '17 at 21:39
  • You've somehow mixed two issues up: using Catch in pre-compiled header, and, integrate Catch into Visual Studio. – caoanan Nov 16 '17 at 10:51

2 Answers2

2

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);
}
Xarn
  • 3,460
  • 1
  • 21
  • 43
caoanan
  • 554
  • 5
  • 20
1

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

Arya Pourtabatabaie
  • 705
  • 2
  • 7
  • 22