Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ClassDataSource doesn't instantiate class on every MethodDataSource test #1187

Open
theodelrieu opened this issue Nov 12, 2024 · 1 comment · May be fixed by #1205
Open

ClassDataSource doesn't instantiate class on every MethodDataSource test #1187

theodelrieu opened this issue Nov 12, 2024 · 1 comment · May be fixed by #1205

Comments

@theodelrieu
Copy link

theodelrieu commented Nov 12, 2024

Hello, here is a reproducible example of the issue.
The MethodDataSource test shares the same Context instance across the 4 tests, whereas the one using Arguments works as intended.

using TUnit.Core;
using TUnit.Core.Interfaces;

namespace Tests;

public record TestRecord(Guid AppId, int x, int y);

public static class TestData
{
    public static Guid AppId1 { get; } = Guid.NewGuid();
    public static Guid AppId2 { get; } = Guid.NewGuid();
    public static Guid AppId3 { get; } = Guid.NewGuid();

    public static IEnumerable<TestRecord> App1Data()
    {
        yield return new TestRecord(AppId1, 7, 2);
        yield return new TestRecord(AppId1, 2, 1);
        yield return new TestRecord(AppId1, 1, 0);
        yield return new TestRecord(Guid.NewGuid(), 0, 1);
    }

    public static IEnumerable<TestRecord> App2Data()
    {
        yield return new TestRecord(AppId2, 7, 2);
        yield return new TestRecord(AppId2, 2, 1);
        yield return new TestRecord(AppId2, 1, 0);
        yield return new TestRecord(Guid.NewGuid(), 0, 1);
    }

    public static IEnumerable<TestRecord> App3Data()
    {
        yield return new TestRecord(AppId3, 7, 2);
        yield return new TestRecord(AppId3, 2, 1);
        yield return new TestRecord(AppId3, 1, 0);
        yield return new TestRecord(Guid.NewGuid(), 0, 1);
    }
}

public class Fixture : IAsyncInitializer
{
    public async Task InitializeAsync()
    {
        Console.WriteLine("in fixture init async");
        await Task.Delay(2);
        Console.WriteLine("fixture init async done");
    }
}

public class Context : IAsyncInitializer
{
    public Guid Id { get; } = Guid.NewGuid();

    public async Task InitializeAsync()
    {
        Console.WriteLine("in context init async");
        await Task.Delay(2);
        Console.WriteLine("context init async done");
    }
}

[ClassDataSource<Fixture, Context>(Shared = [SharedType.PerTestSession, SharedType.None])]
public class Tests(Fixture fixture, Context ctx)
{
    [Test]
    [MethodDataSource(typeof(TestData), nameof(TestData.App1Data))]
    public async Task Test1(TestRecord r)
    {
        Console.WriteLine($"in test1, id = {ctx.Id}");
        await Task.Delay(2);
    }

    [Test]
    [Arguments(1)]
    [Arguments(2)]
    public async Task Test2(int a)
    {
        Console.WriteLine($"in test2, iteration {a}, id = {ctx.Id}");
        await Task.Delay(2);
    }
}
@thomhurst thomhurst linked a pull request Nov 12, 2024 that will close this issue
@thomhurst
Copy link
Owner

Thanks for the report @theodelrieu !

This is a problem with nested loops;data, and I'm not entirely sure the best way to solve it without performing multiple enumeration...

@thomhurst thomhurst linked a pull request Nov 13, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants