Skip to content

Commit

Permalink
Initial take at test API
Browse files Browse the repository at this point in the history
  • Loading branch information
slonopotamus committed Aug 2, 2024
1 parent 47e4bd0 commit 4ee581f
Show file tree
Hide file tree
Showing 11 changed files with 326 additions and 0 deletions.
35 changes: 35 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
Language: Cpp
BasedOnStyle: LLVM
IndentWidth: 4
TabWidth: 4
UseTab: ForIndentation
Standard: Cpp11
FixNamespaceComments: true
NamespaceIndentation: All
AccessModifierOffset: -4
AlignAfterOpenBracket: Align
AlignEscapedNewlines: DontAlign
AlignTrailingComments: true
AllowShortCaseLabelsOnASingleLine: true
AllowShortFunctionsOnASingleLine: InlineOnly
BreakConstructorInitializersBeforeComma: true
RemoveSemicolon: true
InsertBraces: true
InsertNewlineAtEOF: true
BreakBeforeBraces: Allman

AllowShortLambdasOnASingleLine: Inline
ColumnLimit: 0
PointerAlignment: Left
SpaceAfterTemplateKeyword: false
SpacesInAngles: false
ContinuationIndentWidth: 4
IndentCaseLabels: true
IncludeBlocks: Regroup
IncludeCategories:
- Regex: '.*\.generated\.h'
Priority: 2
- Regex: '.*'
Priority: 1
...
8 changes: 8 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: CI
on: [push, pull_request]
jobs:
clang-format:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: DoozyX/[email protected]
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/.idea/
/Binaries/
/Intermediate/
9 changes: 9 additions & 0 deletions ClangFormat.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

# http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -euo pipefail
IFS=$'\n\t'

PROJECT_DIR="$(dirname "${BASH_SOURCE[0]}")"

find "${PROJECT_DIR}" \( -name "*.h" -o -name "*.cpp" \) -print0 | xargs -0 -P "$(nproc)" -n 100 clang-format -i
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Marat Radchenko

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
46 changes: 46 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,50 @@

This project aims to provide a testing framework for Unreal Engine that does not suck.

== Goals

* Simple things should be simple, complex things should be possible
* Extensibility
* Async execution support
* IDE and CI first-class support

== Usage

.Simple test
[source,cpp]
----
#include "UEST.h"
TEST(MyFancyTest)
{
// test body goes here
ASSERT_THAT(...);
}
----

.Test class with multiple methods
[source,cpp]
----
#include "UEST.h"
TEST_CLASS(MyFancyTestClass)
{
TEST_METHOD(Method1)
{
// test body goes here
ASSERT_THAT(...);
}
TEST_METHOD(Method2)
{
// test body goes here
ASSERT_THAT(...);
}
// put helper fields or methods here
}
----

== Analysis of existing Unreal Engine solutions

As of 5.4, Unreal Engine has 4 (FOUR, that's not a typo) APIs for writing tests and all are very far from being good for various reasons.
Expand Down Expand Up @@ -137,3 +181,5 @@ You need to use custom macros instead of `TEST` and `TEST_CLASS` because they ha
And this framework claims they are about composition instead of inheritance!
There was absolutely zero reason to tie test class to a _single_ asserter.
Asserter could easily be absolutely external class to the test itself, see NUnit for example.

// TODO: Write about AFunctionalTest, DaedalicTestAutomationPlugin, Gauntlet
32 changes: 32 additions & 0 deletions Source/UEST/Private/UEST.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "UEST.h"

#include "Modules/ModuleManager.h"

FUESTTestBase::FUESTTestBase(const FString& InName)
: FAutomationTestBase(InName, false)
{
}

uint32 FUESTTestBase::GetRequiredDeviceNum() const
{
return 1;
}

uint32 FUESTTestBase::GetTestFlags() const
{
return EAutomationTestFlags::ApplicationContextMask | EAutomationTestFlags::ProductFilter;
}

void FUESTTestBase::GetTests(TArray<FString>& OutBeautifiedNames, TArray<FString>& OutTestCommands) const
{
OutBeautifiedNames.Add(GetBeautifiedTestName());
OutTestCommands.Add(FString());
}

bool FUESTTestBase::RunTest(const FString& Parameters)
{
DoTest(Parameters);
return true;
}

IMPLEMENT_MODULE(FDefaultModuleImpl, UEST)
19 changes: 19 additions & 0 deletions Source/UEST/Private/UESTTests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "UEST.h"

TEST_CLASS(UEST_TestClass)
{
TEST_METHOD(Test1)
{
ASSERT_THAT(true);
}

TEST_METHOD(Test2)
{
ASSERT_THAT(true);
}
};

TEST(UEST_Test)
{
ASSERT_THAT(true);
}
117 changes: 117 additions & 0 deletions Source/UEST/Public/UEST.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#pragma once

// These asserts are just a stub

#define AssertThatMsgf(Expression, Message, ...) \
do \
{ \
if (!ensureAlwaysMsgf((Expression), TEXT(Message), ##__VA_ARGS__)) \
{ \
return; \
} \
} while (false)

#define ASSERT_THAT(Expression) AssertThatMsgf(Expression, "expected expression to be true")

class UEST_API FUESTTestBase : public FAutomationTestBase
{
protected:
FUESTTestBase(const FString& InName);

virtual uint32 GetRequiredDeviceNum() const override;

virtual uint32 GetTestFlags() const override;

virtual void GetTests(TArray<FString>& OutBeautifiedNames, TArray<FString>& OutTestCommands) const override;

virtual bool RunTest(const FString& Parameters) override;

virtual void DoTest(const FString& Parameters)
{
}
};

template<typename TClass>
struct TUESTInstantiator
{
TUESTInstantiator()
{
Instance = MakeUnique<TClass>();
}
TUniquePtr<TClass> Instance;
};

#define TEST_CLASS_WITH_BASE(ClassName, BaseClass, PrettyName) \
struct F##ClassName##Impl; \
struct F##ClassName : public BaseClass \
{ \
typedef BaseClass Super; \
F##ClassName() \
: Super(TEXT(#PrettyName)) \
{ \
} \
/* This using is needed so Rider understands that we are a runnable test */ \
using Super::RunTest; \
virtual FString GetBeautifiedTestName() const override \
{ \
return TEXT(#PrettyName); \
} \
virtual FString GetTestSourceFileName() const override \
{ \
return TEXT(__FILE__); \
} \
virtual int32 GetTestSourceFileLine() const override \
{ \
return __LINE__; \
} \
}; \
static TUESTInstantiator<F##ClassName##Impl> ClassName##Instantiator; \
struct F##ClassName##Impl : public F##ClassName

#define TEST_WITH_BASE(TestName, BaseClass, PrettyName) \
TEST_CLASS_WITH_BASE(TestName, BaseClass, PrettyName) \
{ \
virtual void DoTest(const FString& Parameters) override; \
}; \
void F##TestName##Impl::DoTest(const FString& Parameters)

/**
* Simple macro for a test.
* Usage:
*
* TEST(MyFancyTest)
* {
* // Test body goes here
* ASSERT_THAT(...);
* }
*/
// TODO: Add proper support for variadics
#define TEST(...) TEST_WITH_BASE(__VA_ARGS__, FUESTTestBase, __VA_ARGS__)

/**
* Declares a test class
* Usage:
*
* TEST_CLASS(MyFancyTestClass)
* {
* TEST_METHOD(Method1)
* {
* // Test body goes here
* ASSERT_THAT(...);
* }
*
* TEST_METHOD(Method2)
* {
* // Test body goes here
* ASSERT_THAT(...);
* }
*
* // You can put helper fields or methods here
* }
*/
// TODO: Add proper support for variadics
#define TEST_CLASS(...) TEST_CLASS_WITH_BASE(__VA_ARGS__, FUESTTestBase, __VA_ARGS__)

// TODO: Add proper support for variadics
// TODO: This is just a stub, we need to register method in test class
#define TEST_METHOD(MethodName) void MethodName()
16 changes: 16 additions & 0 deletions Source/UEST/UEST.Build.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using UnrealBuildTool;

public class UEST : ModuleRules
{
public UEST(ReadOnlyTargetRules Target) : base(Target)
{
PublicDependencyModuleNames.AddRange(
new[]
{
"Core",
"CoreUObject",
"Engine",
}
);
}
}
20 changes: 20 additions & 0 deletions UEST.uplugin
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"FileVersion": 3,
"Version": 1,
"VersionName": "1.0",
"FriendlyName": "UEST",
"Description": "A testing framework that does not suck",
"Category": "",
"CreatedBy": "Marat Radchenko",
"CreatedByURL": "https://github.com/slonopotamus",
"DocsURL": "https://github.com/slonopotamus/UEST",
"MarketplaceURL": "",
"CanContainContent": false,
"Modules": [
{
"Name": "UEST",
"Type": "Runtime",
"LoadingPhase": "Default"
}
]
}

0 comments on commit 4ee581f

Please sign in to comment.