xUnit basics: reminding of how IClassFixture works
xUnit has a fantastic feature in which you can share some context for all tests inside a collection (say class here).
Class Fixture is for: 'when you want to create a single test context and share it among all the tests in the class, and have it cleaned up after all the tests in the class have finished.'
In a normal way without using ClassFixture, xUnit will create a class instance per test which makes sense for the sake of test isolation but it's a common use case that you want to instantiate a class/service only once to be reused for all tests, then ClassFixture comes to the picture.
You need to know how xUnit runs the tests otherwise it may cause writing inefficient tests!
Ok, in this example, let's say you have a UserServiceTest containing 3 tests and it makes sense to spin up only 1 datasource (db) for them. but still, the UserServiceTest ctor will be called 3 times.
So what's the point here?
I can see a misunderstanding between using IClassFixture and Class instantiating, this is the rule to be reminded of:
For one collection the IClassFixture runs once and class ctor runs per test.
Saeed Esmaeelinejad
xUnit basics: reminding of how IClassFixture works
xUnit has a fantastic feature in which you can share some context for all tests inside a collection (say class here).
Class Fixture is for: 'when you want to create a single test context and share it among all the tests in the class, and have it cleaned up after all the tests in the class have finished.'
In a normal way without using ClassFixture, xUnit will create a class instance per test which makes sense for the sake of test isolation but it's a common use case that you want to instantiate a class/service only once to be reused for all tests, then ClassFixture comes to the picture.
You need to know how xUnit runs the tests otherwise it may cause writing inefficient tests!
Ok, in this example, let's say you have a UserServiceTest containing 3 tests and it makes sense to spin up only 1 datasource (db) for them. but still, the UserServiceTest ctor will be called 3 times.
So what's the point here?
I can see a misunderstanding between using IClassFixture and Class instantiating, this is the rule to be reminded of:
For one collection the IClassFixture runs once and class ctor runs per test.
#dotnet #xunit #unittesting #iclassfixture
1 year ago | [YT] | 12