How to use GoogleTest properly

I see that GoogleTest library has been included in the latest engine version (it’s in …\UE_4.24\Engine\Source\ThirdParty\GoogleTest). That’s great because I don’t have to make a separate module to integrate myself!
I created a test file in an empty C++ Unreal project and then tried to write a simple test in it, but it doesn’t work. So in this cpp test file, I’m including the library in the Engine’s ThirdParty folder like this:


#include "GoogleTest/include/gmock/gmock.h"
#include "GoogleTest/include/gtest/gtest.h"

Intellisense does not complain about it, but it then complains about any other gtest related macro or declaration that I try to use in the test file.
More specifically, when I then try to build, I have a lot of errors complaining that it cannot find the headers included in the library, for example


Cannot open include file: 'gmock/gmock-actions.h': No such file or directory

Now, that file is there and it exists.
What do I need to change or add or include in order to see the other headers?
Thanks!

@Malo88 Did you find how to integrate google test properly? I’m also kinda stuck, and I can’t find any documentation on how to use it…

I’m trying to make this code works:


#pragma once
#include "GoogleTest/include/gtest/gtest.h"

GTEST_TEST(MyTest, ShouldWork) {
    EXPECT_TRUE(true);
}

GTEST_TEST(MyTest, ShouldNotWork) {
    EXPECT_TRUE(false);
}

@Herobrine20XX Unfortunately not, still don’t understand how to use those libraries directly from the engine.
But I found these two extremely helpful guides that I think will do the trick:

https://ericlemes.com/2018/12/13/uni…in-unreal-pt-2
I’m going to try them as soon as possible, they both require to build your own unreal from source code though.
Let me know if you have any luck!

Hi @Malo88,

If you prefer a ready made solution, you can take a look at this repo: GitHub - NansPellicari/UE4-TPL-CppWithTestEnv: Launch your tests and code coverage in UE4 project with
It uses this light thirdparty lib I’ve created: GitHub - NansPellicari/UE4-GoogleTest: A simple third-party library for UE4 to use the Google test lib
Hope it helps !

Regards,

Thanks @nansP your solution worked perfectly! Basically my problem with the guide provided by ericlemes was that it wasn’t building properly the solution but with your method GitHub - NansPellicari/UE4-GoogleTest: A simple third-party library for UE4 to use the Google test it worked like a charm.
Also, great work with reports! That could be extremely useful with continuous integration. Do you have perhaps also some game code with tdd to show? It would be nice to see how someone else is mocking and implementing tdd with Unreal.

You’re Welcome @Malo88!
I’m very glad it’s helping you :smiley: !! Next week I will push 2 new plugins with a bunch of tests in both ( with Unreal and GG tests included). They will be open source so you’ll be able to dig in.
I could noticed you here if you want ?

@nansP that would be great, thanks, please do!

@nansP a bit offtopic maybe, but were you able to include AActor in your ExampleGameCore module without having “Engine” in Build.cs but only “Core” (I also added "CoreUObject)? Cause in ericleme’s guide he’s not adding Engine to avoid increasing build times and apparently it works but mine it’s not compiling…

Hi @Malo88, the thing is you shouldn’t use UE4 objects (I mean by that all UObject class derivations) in your *Core plugins.

As Eric Lemes says in this post: https://ericlemes.com/2018/12/17/uni…n-unreal-pt-3/, you should work with c++ interfaces & adapters (Adapter) or decorators, proxys,… to give UE4 objects to your Core library.
So you should create 2 plugins (or modules) **MyLibCore **& **MyLibUE4 **with 2 differents goals:

  1. Core: creates your full functionnalities, very lightweight and UE4 agnostic
  2. UE4: creates the bridge from your Core lib to the Unreal Engine, by that I mean it extends your lib to create blueprint functionnalities, UE4 project settings, creating lightweight UObject classes which embed your Core objects, etc…

It is a bit tricky to do, but it allows you to:

  1. stick to a lightweight Core library you can launch very very fast and often
  2. already create an architecture which respects most of the SOLID principles (thanks interfaces :-))
  3. reduce the amount of UE4 S**pecs or UnitTest **which are cool features but too long to launch (in my taste at least :-)) https://docs.unrealengine.com/en-US/…ion/index.html
  4. making your Core lib UE4 agnostic (or at least with the less dependencies you can), in case you want to export you lib in an another engine.

If you can wait a bit more, I hope tomorow I will finish 2 repos to show you some full examples of these design concepts, I will noticed you here as promised.

Note: This is for this purpose I’ve created this template: GitHub - NansPellicari/UE4-TPL-CppWithTestEnv: Launch your tests and code coverage in UE4 project
It allows to have 2 series of tests for each lib and you can use the same process to launch tests for each.

Thanks @nansP ! Sure, no worries, take your time :slight_smile: I was asking just because I wanted to complete that tutorial but was stuck by the fact that it wasn’t compiling saying AActor not found. Since ericlemes is creating UnrealActorAdapter into the ExampleGameCore module and then using AActor in its cop…so I really wondered how the hell was that compiling :smiley:

Ah ok, yeah I think I see, it was hard to me too to not having a full code example of it’s tutorial. The architecture details should be clearer having code samples.
https://ericlemes.com/2018/12/17/uni…n-unreal-pt-3/ If you take a closer look at the chapter “Using your code in your game”, he talking about ExampleGame, **ExampleGameCore **and the ExampleGame.Build.cs. The ExampleGame build is the UE4 module i’ve talking about above, and this is the module where the AActor is created. This build embeds the “Engine” and the “ExampleGameCore” as dependencies, so it should build correctly and found the AActor class.
I hope it could help!

Oh and I wonder, if you are ok with that, it could be great to create a github public repository of your implementation of this tuto.
This way I can take a look if you want and when it is done you can post it to the Eric’s page as code sample to helps other people.
What do you think?

Ah yes, makes sense! ■■■■, I should stop doing this late at night during the week and start doing it on the weekend, maybe it’ll make me focus more :smiley:

Yeah, sounds good, it would be very useful for people like me who get stuck easily! I’m going to maybe ask Eric for confirmation and let you know when it’s uploaded.

Thanks!

@nansP There you go man! I’ve finally finished it and it works. I’ve uploaded everything in here:

Let me know anyway when you have your other repo you mentioned last time. Cheers

Hi @Malo88,

I was busier than I thought this last weekend, but here they are:

They also use depencencies as tests helpers:

You can directly jump to the dev docs where i explain how to launch tests:
For the UE4-TimelineSystem repo**:**
https://github.com/NansPellicari/UE4…s.md#5-testing
For the UE4-FactorsFactory repo**:**
https://github.com/NansPellicari/UE4…s.md#6-testing

Tests implementations:

As you will see in the previous links, I use **GG tests **for unit testing my **Core **module and **UE4 automation tests **more as **functionnal tests **for the **UE4 **module.

For the **UE4-TimelineSystem **repo :

For the **UE4-FactorsFactory **repo :

Let me know if there is some lacks of clarity or explanations. I will try to fix that quick! Enjoy!

Nice, well done! I will test it soon. Thanks for the quote I appreciate that!

Hey guys,

Just found this post. I got working UObjects (with the GC) in my tests but I couldnt make anything inside the Engine to work. So I have to use a proxy module if I want to test something that inherits from let’s say UActorComponent. It’s a bit hacky but it does not add too much friction because it’s like using the Engine types but would be even better to be able to use those. It should be possible somehow, if anyone finds a workaround to that let me know!