Announcement

Collapse
No announcement yet.

How to use GoogleTest properly

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    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:
    Code:
    #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
    Code:
    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!

    #2
    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:


    Code:
    #pragma once
    #include "GoogleTest/include/gtest/gtest.h"
    
    GTEST_TEST(MyTest, ShouldWork) {
        EXPECT_TRUE(true);
    }
    
    GTEST_TEST(MyTest, ShouldNotWork) {
        EXPECT_TRUE(false);
    }

    Comment


      #3
      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://github.com/NansPellicari/UE4WithGoogle
      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!

      Comment


        #4
        Hi Malo88,

        If you prefer a ready made solution, you can take a look at this repo: https://github.com/NansPellicari/UE4-TPL-CppWithTestEnv
        It uses this light thirdparty lib I've created: https://github.com/NansPellicari/UE4-GoogleTest
        Hope it helps !

        Regards,

        Comment


          #5
          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 https://github.com/NansPellicari/UE4-GoogleTest 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.

          Comment


            #6
            You're Welcome Malo88!
            I'm very glad it's helping you !! 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 ?
            Last edited by nansP; 06-02-2020, 07:08 AM.

            Comment


              #7
              nansP that would be great, thanks, please do!

              Comment


                #8
                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...

                Comment


                  #9
                  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 (https://refactoring.guru/design-patterns/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 Specs 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: https://github.com/NansPellicari/UE4-TPL-CppWithTestEnv
                  It allows to have 2 series of tests for each lib and you can use the same process to launch tests for each.
                  Last edited by nansP; 06-12-2020, 11:35 AM.

                  Comment


                    #10
                    Thanks nansP ! Sure, no worries, take your time 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

                    Comment


                      #11
                      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!
                      Last edited by nansP; 06-13-2020, 08:42 PM.

                      Comment


                        #12
                        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?

                        Comment


                          #13
                          Originally posted by nansP View Post
                          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!
                          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
                          Originally posted by nansP View Post
                          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?
                          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!

                          Comment


                            #14
                            nansP There you go man! I've finally finished it and it works. I've uploaded everything in here:

                            https://github.com/malo88/UnrealGoogleTestExample

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

                            Comment


                              #15
                              Hi Malo88,

                              I was busier than I thought this last weekend, but here they are:
                              https://github.com/NansPellicari/UE4-TimelineSystem
                              https://github.com/NansPellicari/UE4-FactorsFactory

                              They also use depencencies as tests helpers:
                              https://github.com/NansPellicari/UE4-TestsHelpers
                              https://github.com/NansPellicari/UE4-CoreHelpers

                              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!

                              Comment

                              Working...
                              X