Skip to content

Regression: TestBuilderContext.ObjectBag no longer works, again #3723

@aradalvand

Description

@aradalvand

A regression — basically identical to #3079

ClassConstructorMetadata.TestBuilderContext.ObjectBag once again doesn't flow through to TestContext.StateBag.

You might also want to rename TestBuilderContext.ObjectBag to StateBag, for consistency's sake. I think this property may have been overlooked in general.

Minimal repro (nearly identical to the one in #3079):
Using TUnit version 1.0.0:

using System.Diagnostics.CodeAnalysis;
using TUnit.Core.Interfaces;

public sealed class ExampleConstructor : IClassConstructor
{
    public async Task<object> Create([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type type, ClassConstructorMetadata classConstructorMetadata)
    {
        classConstructorMetadata.TestBuilderContext.ObjectBag["FOO"] = "BAR";
        return new ExampleTestClass();
    }
}

public sealed class CustomListenersAttribute : Attribute, ITestStartEventReceiver
{
    public async ValueTask OnTestStart(TestContext context)
    {
        Console.WriteLine($"FOO: {context.StateBag["FOO"]}"); // THROWS
    }
}

[CustomListeners]
[ClassConstructor<ExampleConstructor>]
public sealed class ExampleTestClass()
{
    [Test]
    public async Task ExampleTest()
    {
        await Task.Delay(1000);
    }
}

This time, please consider adding a test for this so that such regressions are prevented in the future. Thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions