Skip to content

[Bug]: Sharing Data Between Discovery and Execution doesn't work #5267

@EmilSvenssonSonova

Description

@EmilSvenssonSonova

Description

When trying to share data between discovery and execution the data is not found in execution. The expected objects are simply not present in the testcontext statebag.

Expected Behavior

I would expect data added in the TestBuilderContext in a datasource to be able to be retrieved in the testcontext.StateBag.

Actual Behavior

The TestContext.StateBag doesn't contain the expected data.

Steps to Reproduce

// In your data generator
public static IEnumerable<object[]> TestData()
{
    var builderContext = TestBuilderContext.Current;
    if (builderContext != null)
    {
        builderContext.StateBag["DataGeneratedAt"] = DateTime.Now;
        builderContext.StateBag["GeneratorVersion"] = "1.0";
    }
    
    yield return new object[] { "test" };
}

// In your test
[Test]
[MethodDataSource(nameof(TestData))]
public void MyTest(string value)
{
    // Access the data stored during generation
    var generatedAt = TestContext.Current.StateBag["DataGeneratedAt"];
    var version = TestContext.Current.StateBag["GeneratorVersion"];
    
    Console.WriteLine($"Data was generated at: {generatedAt}");
}

TUnit Version

1.21.30

.NET Version

.NET 10

Operating System

Windows

IDE / Test Runner

Visual Studio

Error Output / Stack Trace

Message: 
[Test Failure] The given key 'DataGeneratedAt' was not present in the dictionary.

  Stack Trace: 
ConcurrentDictionary`2.ThrowKeyNotFoundException(TKey key)
ConcurrentDictionary`2.get_Item(TKey key)
ITestStateBag.get_Item(String key)
FittingDataFixture.MyTestA(String value) line 37

Additional Context

No response

IDE-Specific Issue?

  • I've confirmed this issue occurs when running via dotnet test or dotnet run, not just in my IDE

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions