When do we need to use static test method?

Consider the following test class.
public class MyTest
{
[Fact]
public static void Test_1() => Assert.True(true);
[Fact]
public void Test_2() => Assert.True(true);
}
public class MyTest
{
[Fact]
public static void Test_1() => Assert.True(true);
[Fact]
public void Test_2() => Assert.True(true);
}
Results:
Test Run Successful.
Total tests: 2
Passed: 2
Total time: 1.2190 Seconds
Test Run Successful.
Total tests: 2
Passed: 2
Total time: 1.2190 Seconds
Thus both static and instance test methods are invoked. From this results, I am wondering when we really need static test methods. I am not 100% sure with my opinion. Please correct me if I am wrong. Use static test methods to explicitly express our intent that the test methods don't want to use injected stuffs such as ITestOutputHelper because injected stuffs cannot be static. The chained constraints that prevent static methods from participating in DI: - Static methods can only access static members (other static methods, static fields, static properties, etc). - Static constructor must be parameterless so it cannot be injected. - Instance constructor cannot initialize static members.
1 Reply
Jimmacle
Jimmacle10mo ago
i'm not sure it particularly matters, they may just support static methods for the sake of completeness
Want results from more Discord servers?
Add your server