© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
1 reply
Only in .NET stacks do we trust

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
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.
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

✅ When will we use overload method?
C#CC# / help
3y ago
why do we declare overloaded operators as `static` methods?
C#CC# / help
16mo ago
When to use a static helper class vs extension methods
C#CC# / help
16mo ago
✅ When can we call a non-static method without creating an object?
C#CC# / help
8mo ago