# BeforeAll and AfterAll in Automation Spec?

**URL:** <https://forums.unrealengine.com/t/beforeall-and-afterall-in-automation-spec/560571>\
**Category:** C++\
**Tags:** Automation-Spec, unreal-engine, UE5-0, CPP, question\
**Created:** [May 18, 2022, 9:02pm UTC](https://forums.unrealengine.com/t/beforeall-and-afterall-in-automation-spec/560571 "2022-05-18T21:02:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![PEYMAAN](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@PEYMAAN](https://forums.unrealengine.com/u/PEYMAAN)\
**Post date:** [May 18, 2022, 9:02pm UTC](https://forums.unrealengine.com/t/beforeall-and-afterall-in-automation-spec/560571/1 "2022-05-18T21:02:41Z")

</div>

I have an automation spec which tests a component.  
To test the component, I create a new world, spawn an actor, add the component to the actor in `BeforeEach` method.

All that happens for every test which slows tests down.

I wanted to know if there are functions that run before all tests and after all tests.

That way, I can create the world, use that world for all tests, and then destroy the world when all tests are done.

---

<div class="post-metadata">

**Author:** ![xi57](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/xi57/32/1194660_2.png) [@xi57](https://forums.unrealengine.com/u/xi57)\
**Post date:** [May 18, 2022, 9:59pm UTC](https://forums.unrealengine.com/t/beforeall-and-afterall-in-automation-spec/560571/2 "2022-05-18T21:59:58Z")

</div>

Try this, it shows how to group unit tests into a container so you can do setup/takedown tasks in each container rather than for every test:

> **[Automation Spec](https://docs.unrealengine.com/4.27/en-US/TestingAndOptimization/Automation/AutomationSpec/)**
>
> Overview covering a new type of automation test, known as a 'Spec', which has been added to our existing automation testing framework.

---

<div class="post-metadata">

**Author:** ![PEYMAAN](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@PEYMAAN](https://forums.unrealengine.com/u/PEYMAAN)\
**Post date:** [May 18, 2022, 10:16pm UTC](https://forums.unrealengine.com/t/beforeall-and-afterall-in-automation-spec/560571/3 "2022-05-18T22:16:34Z")

</div>

I’m already using it (I’m even mentioning it in the title).  
It doesn’t have a function that runs before and after all tests.

---

<div class="post-metadata">

**Author:** ![xi57](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/xi57/32/1194660_2.png) [@xi57](https://forums.unrealengine.com/u/xi57)\
**Post date:** [May 18, 2022, 10:29pm UTC](https://forums.unrealengine.com/t/beforeall-and-afterall-in-automation-spec/560571/4 "2022-05-18T22:29:41Z")

</div>

_Pretty sure my comment here was incorrect. Removed to avoid confusing others._

---

<div class="post-metadata">

**Author:** ![cpage\_opex](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@cpage\_opex](https://forums.unrealengine.com/u/cpage_opex)\
**Post date:** [November 20, 2023, 7:59pm UTC](https://forums.unrealengine.com/t/beforeall-and-afterall-in-automation-spec/560571/5 "2023-11-20T19:59:31Z")

</div>

I’m also looking for a way to do this. My thought was to force BeforeEach and AfterEach to act like BeforeAll and AfterAll by only sometimes running their code blocks.

For BeforeAll, you can check whether it’s already been run by keeping a list of corresponding FString names, for example, so you only run it the first time it is encountered.

AfterAll is harder, because I don’t know how many tests to wait for until it should be called. I thought about having an It(“should clean up”) test as the very last test in that Describe() scope that calls the AfterAll code, but I don’t think the spec format guarantees the order of test execution.

---

<div class="post-metadata">

**Author:** ![ZRogers-US](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ZRogers-US](https://forums.unrealengine.com/u/ZRogers-US)\
**Post date:** [July 23, 2024, 2:56pm UTC](https://forums.unrealengine.com/t/beforeall-and-afterall-in-automation-spec/560571/6 "2024-07-23T14:56:44Z")

</div>

So I’m fairly new to automation so I might be incorrect, but I believe a Before All could be done with a BeforeEach in a nested describe eg:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/c/7/c/c7cf00ad982cd739704ad05c3f664bbfe2647862.png)  
This seems to work as a before all for my tests, BeforeEach is run once for my first It function then not run again for the second It function. For a AfterAll, I read that latent Its will run after the AfterEach so, assuming you don’t have any latent tests, I’d have though you could use a Latent It (or possibly LatentAfterEach) to run after everything else. You could probably also nest a AfterEach with the last It function to run at the end again not 100%.  
Don’t quote me I’m a noob 🙂 I had hoped putting the Before and After Each outside of the describe would work but doesn’t seem too ☹

---

<div class="post-metadata">

**Author:** ![tenpn](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@tenpn](https://forums.unrealengine.com/u/tenpn)\
**Post date:** [April 2, 2025, 7:24pm UTC](https://forums.unrealengine.com/t/beforeall-and-afterall-in-automation-spec/560571/7 "2025-04-02T19:24:00Z")

</div>

this approach may work, but if you try to run “Should have loalded after the FPSMap” as a standalone test from the frontend, it won’t run your BeforeEach too! I’d avoid this approach.

---

<div class="post-metadata">

**Author:** ![swiznitzer](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@swiznitzer](https://forums.unrealengine.com/u/swiznitzer)\
**Post date:** [July 23, 2025, 4:19pm UTC](https://forums.unrealengine.com/t/beforeall-and-afterall-in-automation-spec/560571/8 "2025-07-23T16:19:36Z")

</div>

For BeforeAll, you could do something like this.

```auto
BEGIN_DEFINE_SPEC(FMyExampleSpec, "MyProject.MyFeature.MyTest", EAutomationTestFlags::ApplicationContextMask | EAutomationTestFlags::ProductFilter);
   bool bHasSetupRun = false; 
END_DEFINE_SPEC();

void FMyExampleSpec::Define()
{
   Describe("My Feature Test", [this]()
   {
      BeforeEach([this]()
      {
         if (!bHasSetupRun)
         {
            // Perform setup tasks
            bHasSetupRun = true;
         }
      });

      It("should do something after setup", [this]()
      {
         // Test logic
      });

   });
}

```

Regarding an AfterAll block, I imagine you could do something similar where you make an AfterEach block on the same level as your tests. Within the BEGIN\_DEFINE\_SPEC and END\_DEFINE\_SPEC, add `int NumTestsRun = 0;` Then in your AfterEach block, increment that by one and check if it equals the total number of tests in your spec. If it does, run your cleanup code.
