Rai*_*tef 6 c# testing nunit unit-testing autofixture
简明扼要:
class AutoMoqDataAttribute : AutoDataAttribute
{
public AutoMoqDataAttribute() : base(new Fixture().Customize(new AutoMoqCustomization()))
{
}
}
public interface IWeapon { bool LaunchAtEarth(double probabilityOfHitting); }
public class DeathStar
{
readonly IWeapon _weapon;
public DeathStar(IWeapon weapon) // guard clause omitted for brevity
{
this._weapon = weapon;
}
public bool FireFromOrbit(double probabilityOfHitting)
{
return this._weapon.LaunchAtEarth(probabilityOfHitting);
}
}
// Make me pass, pretty please with a cherry on the top
[Test, AutoMoqData]
[TestCase(0.1), TestCase(0.5), TestCase(1)]
public void AutoMoqData_should_fill_rest_of_arguments_that_are_not_filled_by_TestCase(
double probabilityOfHitting,
[Frozen] Mock<IWeapon> weapon,
DeathStar platform)
{
Assert.That(weapon, Is.Not.Null);
Assert.That(platform, Is.Not.Null);
} // Ignored with error: Wrong number of parameters specified.
// Make me pass, too!
[Test, AutoMoqData]
[TestCaseSource("GetTestCases")]
public void AutoMoqData_should_fill_rest_method_arguments_that_are_not_filled_by_TestCaseSource(
double probabilityOfHitting,
[Frozen] Mock<IWeapon> weapon,
DeathStar platform)
{
Assert.That(weapon, Is.Not.Null);
Assert.That(platform, Is.Not.Null);
} // Ignored with error: Wrong number of parameters specified.
IEnumerable<TestCaseData> GetTestCases()
{
yield return new TestCaseData(0.1);
yield return new TestCaseData(0.5);
yield return new TestCaseData(1);
}
Run Code Online (Sandbox Code Playgroud)
如果我使用 Xunit,这似乎可以解决问题:http ://nikosbaxevanis.com/blog/2011/08/25/combining-data-theories-in-autofixture-dot-xunit-extension/ 我找不到任何等效的东西单位。
这个:http : //gertjvr.wordpress.com/2013/08/29/my-first-open-source-contribution/ 似乎已经在当前版本的 AutoFixture.NUnit2(AutoData 属性)中工作,但它没有'当我想让它采用一个 params object[] 时,不处理这种情况,这样测试方法中的第一个参数使用属性参数填充,其余参数传递给 AutoData 属性。
有人可以引导我走向正确的方向吗?似乎缺少一些我无法理解的东西。
我已经成功了。
不幸的是,由于 2.6 中 NUnit 的可扩展性 API 中的一些糟糕的设计选择(无法覆盖或删除任何内置测试用例提供程序),我被迫求助于一些反射来获取“ TestCaseProviders”类。
TL;DR:这应该仅适用于 NUnit 2.6.x。NUnit 3.0 不兼容。
只需在已经使用常规 [TestCase] 和 [TestCaseSource] 属性的测试上添加提供的[AutoMoqData]即可。测试用例参数将首先被填充,其余的测试方法参数将通过 AutoFixture 处理。您可以根据需要更改 AutoMoqDataAttribute 以使用任何不同的夹具自定义(例如:AutoRhinoMockCustomization)。
如果将其添加到外部程序集并在测试项目中引用它,NUnit 将看不到您的加载项(因为它只在正在加载的直接测试程序集或当前运行的“addins”子文件夹中的 DLL 中查找)测试运行程序可执行文件。
在这种情况下,只需在当前测试项目中创建一个空类并使其继承自 AutoMoqDataAddIn即可。使用 ReSharper 单元测试运行程序进行测试,它可以正确查看测试用例,并仅使用“真实”测试参数自动生成测试用例名称。
GitHub:https ://gist.github.com/rwasef1830/ab6353b43bfb6549b396