The goal is that we have a test class with several test methods, and we have some initialization code that is common to all of the test methods. We want the initialization code to run only once for the entire set of tests, not once per test.
One way to do this is to put the initialization code in the constructor, like this:
public MyTestClass()
{
var x = 5;
x.Should().Be(4); // Fluent Assertions
}
Another way is to create a static method with the ClassInitialize attribute, like this:
[ClassInitialize]
public static void Init(TestContext testContext)
{
var x = 5;
x.Should().Be(4); // Fluent Assertions
}
So what's the difference?