Unit Test, TestInitialize alternative in xUnit
Hello guys, I read that [TestInitialize] is used in MSTest to initialize an object. My first question is why should we initialize it? I mean, it seems that we build an entire method to initialize it, why not initializing it in the test cases?
Second thing, when each test cases is executed, the TestInitialize attribute creates a new instance for each case? The idea is we don't want shared data to affect our result?
I read a bit and came across how we can mimic TestInitialize in xUnit:
I write something like this. Can anyone confirm whether the idea is correct please.
Also, notice that we implement the IDisposable, why is it necessary here?
2 Replies
https://thomhurst.github.io/TUnit/docs/comparison/attributes has a comparison table, lifecycle hook attributes
if you have lots of common things to initialize, you can mark a method and you don't have to call it yourself in each test
yep I see