跨测试共享AutoFixture

use*_*252 6 c# autofixture

共享Fixture多个测试方法的实例是一个好习惯吗?

或者Fixture为每个测试方法创建一个新实例是否更好?

什么是最佳做法?如果你能为我提供一个反模式的来源,那将是一件好事.

Mar*_*ann 7

AutoFixture的名称来自Fixture模式:

"测试夹具是我们为了进行测试并期望得到特定结果而需要的所有东西.[......]设置测试夹具是四相测试的第一阶段."

While a Shared Fixture is a conceptual possibility, it comes with lots of disadvantages, because it makes it harder to make tests independent of each other.

AutoFixture was explicitly designed to provide a reusable library for creating Fixtures, instead of having to manually code Fixture Objects for every new kind of test context you'd need to create.

There are people who create a single (AutoFixture) Fixture object and share it across multiple test methods, but I never understood why they do that; it almost defeats the purpose of AutoFixture.

Still, if you find such a set-up useful, who am I to tell you to stop doing it? Whatever floats your boat... However, AutoFixture was designed with the explicit use case of one Fixture instance per test method in mind, and I haven't seen any advantages to doing it the other way.


Nik*_*nis 6

最好的做法是利用AutoFixture.XunitAutoFixture.NUnit2,避免创建Fixture实例,内或测试方法或功能之外.

如果您不能使用上述任何胶水库,那么在每个测试中使用该类的实例时,这被认为是一种很好的做法.Fixture

使用新的实例Fixture类,可以控制AutoFixture的行为在每个特定的测试,你可以申请自定义它,他们是不会影响到其他所有的测试.