Using Catch2 in Visual C++ 2015 - c++

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:
Create a Native Unit Test project
Add Catch to the include directories
Add #include <catch.hpp> to stdafx.h
Write the following simple source file
unittest.cpp:
#include "stdafx.h"
namespace Catch2_Test
{
TEST_CASE("Y U no work")
{
REQUIRE(1);
}
}

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);
}

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

Related

How to succesfully build a project using the MSBuild?

I have a 3-dr party project that I'd like to build. The author said to build it I have to use the MSBuild. Here is the exact citation from him:
Clone https://github.com/NovaRain/DXSDK_Collection.git to some
Copy \DXSDK_Aug2007\Lib\x86\dinput.lib to \DXSDK_Jun2010\Lib\x86\
Set environment variable DXSDK_DIR to "\DXSDK_Jun2010" (trailing slash is important)
In sfall directory (where ddraw.sln is), create empty PostBuild.cmd
Run VS installer and add "MSVC v140 - VS 2015 C++ build tools (v14.00)" if you don't have it already
Find MSBuild.exe and run: MSBuild.exe path\to\ddraw.sln -p:Configuration=ReleaseXP -p:Platform=Win32 -p:PlatformToolset=v140_xp
I've followed all the steps carefully but when i run the MSBuild command I'm getting followin error: "Cannot open include file: 'algorithm': No such file or directory"
here is the stdafx.h header content for a reference:
#pragma once
#pragma message("Compiling precompiled headers.\n")
#define WINVER _WIN32_WINNT_WINXP
#define _WIN32_WINNT _WIN32_WINNT_WINXP
#include <algorithm>
#include <functional>
#include <initializer_list>
#include <memory>
#include <vector>
#include <unordered_map>
#include <map>
#include <string>
//#define WIN32_LEAN_AND_MEAN
#define NOCRYPT
#define NOSERVICE
#define NOMCX
#define NOIME
#include <Windows.h>
#include <intrin.h>
How could it be that the Standard Library headers isn't available since I've done a standard VS c++ installation?

Project structure issue

My goal is to achieve a test project on VisualStudio 2019.
I think that my main problem is with How to structure my project. Because, at the end, I find many files and don't know how to manage them.
These are the test .cpp files :
TIStream.cpp :
#include <boost/test/unit_test.hpp>
#include <boost/test/data/test_case.hpp>
#include <boost/test/data/monomorphic.hpp>
#include "../ReemasCore/serialization.h"
#include <string.h>
BOOST_AUTO_TEST_SUITE(Serialization_InMemStream)
BOOST_AUTO_TEST_CASE(InMemStreamCtor_SizePositif_CorrectPointer)
{
auto in = std::make_unique<InMemStream>(10);
BOOST_TEST(in->ppStart != nullptr);
BOOST_TEST(in->ppCurrent == in->ppStart);
BOOST_TEST(in->ppEnd == in->ppStart + 10);
}
BOOST_AUTO_TEST_SUITE_END()
TTCPServer :
#include <boost/test/unit_test.hpp>
BOOST_AUTO_TEST_SUITE(TCP_SERVER)
BOOST_AUTO_TEST_CASE(Server_Case1)
{
BOOST_TEST(1 == 1);
BOOST_TEST(true);
}
BOOST_AUTO_TEST_SUITE_END()
Based on this mailing the dynamic version of Boost Test doesn't contain the main function from version 1.34.1 and up. So if you want to use the shared version of Boost Test you need to follow the instructions from Boost Test doc.
You have to put #define BOOST_TEST_MODULE test module name before the #include <boost/test/unit_test.hpp>. As the documentation says it must be defined exactly for one compilation unit of your test module. As you create different executables for every test, then it practically means you have to put it into every test source file.

Both DocTest and Catch 2 not running unit test

I'm starting a new win32 C++ project using Visual Studio 2019 (v. 16.0.4) and Resharper (v. 2019.1.1) and can't get either the Catch2 or Doctest unit testing framework to run a test. I prefer Doctest and used that first and when that didn't work, I tried Catch2 and had the same result. Both frameworks find the test, but give the status "Inconclusive Test not run". The Catch2 warning states: "2019.05.19 08:47:46.447 WARN Element CatchTest Test was left pending after its run completion.
". All the code below is for Catch2:
Engine.h
#pragma once
#include "pch.h";
#include <SDKDDKVer.h>;
int wWinMain( dv* ghInst, dv* ghPrevInst, dv* gupCmdLine, dsd gsdCmdShowFlag);
int test();
Engine.cpp
#include "pch.h"
//#include "..\DocTest_2_2_2.h"
#include "..\Catch_2_7_2.h"
#include "Engine.h"
int wWinMain( dv* ghInst, dv* ghPrevInst, dv* gupCmdLine, dsd gsdCmdShowFlag) {
return 0;
};
int test() { return 3; }
TEST_CASE("CatchTest Test") {
REQUIRE(test() == 3);
}
DocTest.cpp (Using Catch2 code, DocTest code is commented out)
#include "pch.h"
//#define DOCTEST_CONFIG_IMPLEMENT
//#include "..\DocTest_2_2_2.h"
#define CATCH_CONFIG_MAIN
#include "..\Catch_2_7_2.h"
I found that there was an issue like this on conversations about prior versions of Visual Studio and Resharper but I'm using the latest versions and still have a problem.
In Resharper options, I have "Enable Catch support" selected under "C++ Tests" and also have "Enable MSTest support" selected under "MsTest". I have the x64 architecture selected for both the unit tests and on all project configuration settings.
Any help with this is appreciated. Thank you.
Your wMinMain does not run the tests. See doctest docs on how to provide a correct main entry point.

Visual Studio doesn't find GL/glut.h

I'm using Microsoft Visual Studio Community 2017 on Windows 10 and I've been trying to install GLUT using this online guide that I found on this StackOverflow question. However, when I try to compile my code this error still shows up: fatal error C1083: Cannot open include file: 'GL\glut.h': No such file or directory. I've followed every single step line by line, but nothing seems to work. What could be causing this error?
This is how I'm importing the dependencies:
#include <iostream>
#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <GL/GL.h>
#include <GL/GLU.h>
#include <GL/glut.h>
#include <cstring>
#include "math.h"
#include <conio.h>
For this, I like to use the Nuget package manager. If you go to
Tools>NuGet Package Manager>Package Manager Console
Then, in the console type:
Install-Package nupengl.core
Your problem should be fixed.

Why does changing the order of including psapi.h gives compilation erros?(Indentifier BOOL is undefined)

I am using Visual Studio Community 2017 to code c++. When I run the following code everything works fine.
#include "pch.h"
#include<Windows.h>
#include<Psapi.h>
#include <iostream>
#include <conio.h>
int main()
{
std::cout << "Really!! How do you do it?";
_getch();
}
But if I change the order of #includes by including psapi.h before Windows.h, compiler goes badass and throws 198 errors at me, which surprisingly(maybe only to me) includes Identifier "BOOL" is undefined.
Why is this happening?
Since Psapi.h's include tree is trivial, I'm going to exemplify. Everything relies on VStudio 2015 (Community) (v14.0.25431.01 Update 3) and Windows Kits 8.1 (? funny, because v10 is there too) files (with default env vars and preprocessor definitions):
BOOL is defined in minwindef.h (#157: typedef int BOOL;)
Psapi.h only includes one file (#27: #include <winapifamily.h>)
winapifamily.h doesn't include any other file
So, when reaching Psapi.h (#87: BOOL WINAPI EnumProcesses (...), the compiler doesn't know anything about BOOL, so it complains.
Windows.h includes minwindef.h (indirectly, via windef.h), and that's why it works when you include it before Psapi.h.
Personally, I think it's a bug in Psapi.h, since it's not self contained, but there might be a good reason (that I'm not aware of) for that. Anyway, if this is indeed a bug, it wouldn't be MS's 1st one :)
#include <Windows.h>
#include <WinSock2.h>
// main present just for rigorosity's sake
int main() {
return 0;
}
to answer the question, I know this is DATED but the issues persist today. You need the following:
#include "stdafx.h"
#include <stdbool.h>
#include <Windows.h>
#include <stdlib.h>
#include <psapi.h>
After stdlib.h was included, the errors were gone.