簡體   English   中英

InlineAutoDataAttribute但適用於NUnit(適用於TestCase和TestCaseSource)

[英]InlineAutoDataAttribute but for NUnit (for TestCase and TestCaseSource)

要被嘲笑:

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);
}

如果我使用Xunit,這似乎可以解決問題: http ://nikosbaxevanis.com/blog/2011/08/25/combining-data-theories-in-autofixture-dot-xunit-extension/我找不到任何等效的東西NUnit的。

這個: http ://gertjvr.wordpress.com/2013/08/29/my-first-open-source-contribution/似乎已經在當前版本的AutoFixture.NUnit2(AutoData屬性)中工作,但它沒有當我想使用params對象[]時,處理這種情況,以便使用屬性參數填充測試方法中的第一個參數,其余參數傳遞給AutoData屬性。

有人能引導我走向正確的方向嗎? 似乎有些東西缺失我無法掌握。

OP代碼 這里不nunit2.6.3為我工作。 實際上它甚至沒有編譯,也不確定nunit基礎設施是否已經改變。 事實證明我錯過了nunit.core.dll引用,在添加它之后,它編譯了。 但我使用了自己的實現。

所以我推出了自己的實現。 實現如下:我們將詢問AutoFixture的參數,它將給出Test方法所需的所有參數,然后我們將只重寫我們需要的參數值。

[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class InlineAutoDataAttribute : AutoDataAttribute
{
    private readonly object[] values;
    public InlineAutoDataAttribute(params object[] values)
        : base(new Fixture().Customize(new AutoMoqCustomization()))
    {
        this.values = values;
    }

    public override IEnumerable<object[]> GetData(MethodInfo method)
    {
        var testCaseData = base.GetData(method);

        foreach (object[] caseValues in testCaseData)
        {
            if (values.Length > caseValues.Length)
            {
                throw new InvalidOperationException("Number of parameters provided is more than expected parameter count");
            }
            Array.Copy(values, caseValues, values.Length);

            yield return caseValues;
        }
    }
}

如何使用:

[Test]
[InlineAutoData(1, 2)]
[InlineAutoData(3, 4)]
public void Whatever_Test_Name(int param1, int param2, TemplateOrder sut)
{
    //Your test here
    Assert.That(sut, Is.Not.Null);
}

測試將被調用

param1 = 1, param2 = 2, sut = given by auto fixture
param1 = 3, param2 = 4, sut = given by auto fixture

您可能會認為此實現效率不高,但至少可以按預期工作。 我也聯系了Mark Seemann (AutoFixture的創造者),但似乎他也無法幫助解決這個問題。 所以現在我可以忍受這個。

我有它的工作。

不幸的是,由於NUnit的2.6中的可擴展性API中的一些糟糕的設計選擇(無法覆蓋或刪除任何內置的測試用例提供程序), 我不得不采取一些反思來獲取“內部的ExtensionCollection實例” TestCaseProviders“類

TL; DR:這應該僅適用於NUnit 2.6.x. NUnit 3.0不兼容。

如何使用

只需使用常規[TestCase]和[TestCaseSource]屬性在測試中添加提供的[AutoMoqData] 首先填充測試用例參數,然后通過AutoFixture處理其余的測試方法參數。 您可以根據需要更改AutoMoqDataAttribute以使用任何不同的夾具自定義(例如:AutoRhinoMockCustomization)。

如何使用ReSharper的測試運行器

如果將其添加到外部程序集並在測試項目中引用它,NUnit將不會看到您的加載項(因為它只查看正在加載的即時測試程序集或在當前運行的“addins”子文件夾中的DLL中)測試跑步者可執行

在這種情況下,只需在當前測試項目中創建一個空類,並使其繼承自AutoMoqDataAddIn 使用ReSharper單元測試運行器進行測試,它可以正確查看測試用例,並僅使用“真實”測試參數自動生成測試用例名稱。

告訴我代碼!

GitHub: https//gist.github.com/rwasef1830/ab6353b43bfb6549b396

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM