Skip to content

[Before(Test)] executes before [BeforeEvery(Test)] #2606

@lizalc

Description

@lizalc

Maybe this is intentional but to me it feels wrong. Since [BeforeEvery(Test)] is "global" I'd expect that hook to be executed first and then the [Before(Test)] hook which is "test specific".

What I'd like to do is have a [BeforeEvery(Test)] hook which prints some test details to the beginning of the test output. But since [Before(Test)] executes first and may write to the test output the test details may not be displayed first.

Example:

public class Tests
{
    [Before(Test)]
    public void Setup()
    {
        Console.WriteLine("Before test setup");
    }

    [Test]
    public void Basic()
    {
        Console.WriteLine("This is a basic test");
    }
}

public sealed class Hooks
{
    [BeforeEvery(Test)]
    public static void BeforeTest(TestContext testContext)
    {
        Console.WriteLine("Before every test");
    }
}

dotnet run output:

████████╗██╗   ██╗███╗   ██╗██╗████████╗
╚══██╔══╝██║   ██║████╗  ██║██║╚══██╔══╝
   ██║   ██║   ██║██╔██╗ ██║██║   ██║   
   ██║   ██║   ██║██║╚██╗██║██║   ██║   
   ██║   ╚██████╔╝██║ ╚████║██║   ██║   
   ╚═╝    ╚═════╝ ╚═╝  ╚═══╝╚═╝   ╚═╝   
   
   TUnit v0.24.0.0 | 64-bit | Darwin 24.5.0 Darwin Kernel Version 24.5.0: Tue Apr 22 19:53:27 PDT 2025; root:xnu-11417.121.6~2/RELEASE_ARM64_T6041 | osx-arm64 | .NET 9.0.6 | Microsoft Testing Platform v1.7.1
   
   Engine Mode: SourceGenerated
   
Before test setup
Before every test
This is a basic test

Test run summary: Passed! - /Users/lizalc/Programming/source/tunit-constructor-runs-before-skipattribute/bin/Debug/net9.0/tunit-constructor-runs-before-skipattribute.dll (net9.0|arm64)
  total: 1
  failed: 0
  succeeded: 1
  skipped: 0
  duration: 46ms

I'd expect the following test output instead:

Before every test
Before test setup
This is a basic test

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions