AutoFixture sharing in tests

Is sharing a Fixture instance for multiple test methods a good practice?

Or is it better to create a new Fixture instance for each test method?

What is the best practice? It will be good if you can provide me with the source on which it is anti-pattern.

+6
source share
2 answers

AutoFixture takes its name from Fixture :

β€œtest equipment is all we need to conduct a test and expect a certain result. [...] Setting up a test device is the first phase of a four-phase test .

While Shared Fixture is a conceptual feature, it has many drawbacks as it makes it difficult to run tests independently of each other.

AutoFixture has been explicitly designed to provide a reusable library for creating Fixtures instead of manually encoding Fixture Objects for each new type of test context you need to create.

There are people who create one Fixture object (AutoFixture) and share it according to several testing methods, but I never understood why they do it; he almost defeats the goal of AutoFixture.

However, if you find this setting useful, by whom should I tell you to stop doing this? No matter what your boat is sailing ... However, AutoFixture was developed with the explicit option of using one Fixture instance for each test method, and I did not see any advantages for this in another way.

+7
source

Best practice is to use AutoFixture.Xunit or AutoFixture.NUnit2 and not create a Fixture instance inside or outside of the testing methods or functions.

If you cannot use any of the above glue libraries, it is considered good practice to use a new instance of the Fixture class in each test.

Using a new instance of the Fixture class allows you to control how AutoFixture behaves in each particular test, since you can apply settings to it and they will not affect all other tests.

+6
source

Source: https://habr.com/ru/post/981194/


All Articles