OneTimeSetUp before TestCaseDataSource? #5036
-
|
I've got a [SetUpFixture]
internal static class TestSetup
{
public static MyService MyService { get; private set; }
[OneTimeSetUp]
public static async Task OneTimeSetUp()
{
MyService = /* ... */;
}
}and I wish I could use it in a TestCaseDataSource but that throws an exception because I suppose that makes sense. Is there a work-around for that use-case? |
Beta Was this translation helpful? Give feedback.
Answered by
manfred-brands
Sep 26, 2025
Replies: 1 comment 1 reply
-
|
@verdie-g You could convert the MyService property into a lazy method so that it is evaluated on first use: internal static class TestSetup
{
private static MyService _myService;
public static async Task<MyService> GetMyService()
{
If (_myService is null)
_myService = await CreateService()
return _myService;
}
}With your use case converted to: [TestCaseSource(nameof(TestCases))]
public async Task Test(string x)
{
}
private static IAsyncEnumerable<string> TestCases()
{
return (await TestSetup.GetMyService()).EnumerateTestCases();
}Note I'm not behind a PC and haven't tried the above. |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
verdie-g
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@verdie-g You could convert the MyService property into a lazy method so that it is evaluated on first use:
With your use case converted to:
Note I'm not behind a PC and haven't tried the above.