Skip to content

Commit

Permalink
Add support for BEFORE_EACH/AFTER_EACH
Browse files Browse the repository at this point in the history
  • Loading branch information
slonopotamus committed Sep 19, 2024
1 parent 8671fd6 commit a66ca30
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 7 deletions.
23 changes: 22 additions & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,28 @@ TEST_CLASS(MyFancyTestClass)
}
----

If you want to execute a common piece of logic before and after each test method in a test class, you can do that using `BEFORE_EACH`/`AFTER_EACH` macros:

[source,cpp]
----
#include "UEST.h"
TEST_CLASS(MyFancyTestClass)
{
BEFORE_EACH()
{
// Place code that will be executed before each test method of this class
}
AFTER_EACH()
{
// Place code that will be executed after each test method of this class
}
...
}
----

=== Assertions

All UEST assertions are done through `ASSERT_THAT(Expression, Matcher)`.
Expand Down Expand Up @@ -154,7 +176,6 @@ TEST(MyGame, SimpleMultiplayerTest)

== Further development plans

* `BEFORE_EACH`/`AFTER_EACH` to `TEST_CLASS`
* More matchers
* Add `ASSERT_MULTIPLE` that allows performing multiple assertions without interrupting execution between them, also known as "soft assertions".
* Add API to disable tests (with `EAutomationTestFlags::Disabled` under the hood)
Expand Down
10 changes: 8 additions & 2 deletions Source/UEST/Private/UEST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,14 @@ bool FUESTTestBase::RunTest(const FString& InTestName)
{
if (const auto* TestInfo = TestMethods.Find(InTestName); ensure(TestInfo))
{
// TODO: Add things like BEFORE_EACH() {...}, AFTER_EACH() {...}?
TestInfo->Delegate.Execute();
Setup();

if (!HasAnyErrors())
{
TestInfo->Delegate.Execute();
}

TearDown();
}
}

Expand Down
28 changes: 28 additions & 0 deletions Source/UEST/Private/UESTTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,32 @@ TEST_CLASS(UEST, TestClass, With, Deep, Naming)
ASSERT_THAT(true, Is::True);
}
};

TEST_CLASS(UEST, BeforeAfter)
{
int32 BeforeCalled = 0;
int32 AfterCalled = 0;

BEFORE_EACH()
{
BeforeCalled++;
}

AFTER_EACH()
{
AfterCalled++;
}

TEST_METHOD(Test1)
{
ASSERT_THAT(BeforeCalled, Is::EqualTo<int>(1));
ASSERT_THAT(AfterCalled, Is::EqualTo<int>(0));
}

TEST_METHOD(Test2)
{
ASSERT_THAT(BeforeCalled, Is::EqualTo<int>(2));
ASSERT_THAT(AfterCalled, Is::EqualTo<int>(1));
}
};
// clang-format on
20 changes: 16 additions & 4 deletions Source/UEST/Public/UEST.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ namespace UEST
{
return Value == nullptr;
}

FString Describe() const
{
return TEXT("be nullptr");
Expand All @@ -62,6 +63,7 @@ namespace UEST
{
return Value;
}

FString Describe() const
{
return TEXT("be true");
Expand All @@ -76,6 +78,7 @@ namespace UEST
{
return !Value;
}

FString Describe() const
{
return TEXT("be false");
Expand Down Expand Up @@ -366,9 +369,9 @@ namespace Is
template<typename T>
using AtLeast = GreaterThanOrEqualTo<T>;

const auto Zero = EqualTo<int64>(0);
const auto Positive = GreaterThan<int64>(0);
const auto Negative = LessThan<int64>(0);
const inline auto Zero = EqualTo<int64>(0);
const inline auto Positive = GreaterThan<int64>(0);
const inline auto Negative = LessThan<int64>(0);

template<typename T>
using InRange = UEST::Matchers::InRange<T>;
Expand Down Expand Up @@ -413,7 +416,9 @@ namespace Is
do \
{ \
const auto& _M = Matcher; \
if (!ensureAlwaysMsgf(_M.Matches<decltype(Value)>(Value), TEXT("%s: %s must %s"), TEXT(#Value), *CQTestConvert::ToString(Value), *_M.Describe())) \
/* TODO: Can we make this const auto& ? */ \
const auto _V = Value; \
if (!ensureAlwaysMsgf(_M.Matches<decltype(_V)>(_V), TEXT("%s: %s must %s"), TEXT(#Value), *CQTestConvert::ToString(_V), *_M.Describe())) \
{ \
return; \
} \
Expand All @@ -438,6 +443,10 @@ class UEST_API FUESTTestBase : public FAutomationTestBase

virtual bool RunTest(const FString& InTestName) override;

virtual void Setup() {}

virtual void TearDown() {}

public:
struct FTestMethodInfo final
{
Expand Down Expand Up @@ -575,3 +584,6 @@ struct TUESTInstantiator
#define TEST_METHOD(MethodName) \
FUESTMethodRegistrar reg##MethodName{*this, TEXT(#MethodName), {FSimpleDelegate::CreateRaw(this, &ThisClass::MethodName), TEXT(__FILE__), __LINE__}}; \
void MethodName()

#define BEFORE_EACH() virtual void Setup() override
#define AFTER_EACH() virtual void TearDown() override

0 comments on commit a66ca30

Please sign in to comment.