使用Nunit 3.x进行参数化测试

时间:2017-10-16 14:37:21

标签: c# unit-testing testing nunit nunit-3.0

我在测试时遇到以下情况,我想问你们是否有测试这个的快捷方式。

[Test]
[TestCaseSource(nameof(MlTestCases))]
[TestCaseSource(nameof(QaTestCases))]
public void EditBetSlip_ShouldConvertOddsFromAmericanToDecimal(string selectionId)
{
    // Arrange
    var betSlipRequest = new PostBetSlipRequest
    {
        OddStyle = OddStyle.American.ToString(),
        Selections = new List<PostOneSelectionRequest>
        {
            new PostOneSelectionRequest
            {
                DisplayOdds = $"+{Fixture.Create<int>()}",
                Id = selectionId.Replace("#", "%23"),
            },
        },
        Bets = new List<PostOneBetRequest>
        {
            new PostOneBetRequest
            {
                OddStyle = OddStyle.American.ToString(),
                Id = 0,
                Stake = 10,
            },
        },
    };

    // Act
    _client.EditBetslip(betSlipRequest);
    var response = _client.RefreshBetslip(new GetBetSlipRequest { OddStyle = OddStyle.European.ToString() });
    var betslip = response.DeserializedBody;

    // Assert
    Assert.IsTrue(response.StatusCode == HttpStatusCode.OK);

    foreach (var selection in betslip.Selections)
    {
        Assert.DoesNotThrow(() => decimal.Parse(selection.DisplayOdds));
    }
}

现在我需要再次进行相同的测试,只需翻转OddStylePostBetSlipRequest GetBetSlipRequest。我尝试使用[Values]属性,但它没有按照我想要的方式工作。

我想要的是使用American - European执行所有这两个测试用例来源,并且可以再次使用European - American吗?

1 个答案:

答案 0 :(得分:1)

当然,每种情况(美国 - &gt; Eur&amp; Eur - &gt; US)都是测试方法的新测试案例?

因为你有n个测试用例,其中n = QaTestCases的总数+ MlTestCases的总数。

您实际上想要测试2n个测试用例(每个[Eur - &gt; US,US - &gt; Eur] permuatation的每个现有测试用例)。我建议这应该只是一个新的TestCaseSource使用现有的[Test] [TestCaseSource(nameof(TestCaseSourceA))] [TestCaseSource(nameof(TestCaseSourceB))] public void GivenX_ShouldReturnOk(string input) { //some test Assert.Pass(); } public static IEnumerable<string> TestCaseSourceA() { yield return "a1"; yield return "a2"; } public static IEnumerable<string> TestCaseSourceB() { yield return "b1"; yield return "b2"; } 并添加欧洲/美国的排列。

立即剥离它,你现在有一些模糊的东西:

[Test]
[TestCaseSource(nameof(TestCaseSourceMaster))]
public void GivenX_ShouldReturnOk(string input, string fromOddsStyle, string toOddsStyle)
{
    //some test
    Assert.Pass();
}

public static IEnumerable<string[]> TestCaseSourceMaster()
{
    return TestCaseSourceA()
        .Concat(TestCaseSourceB())
        .SelectMany(t => new string[][]
        {
            new string[]{t,"US","Eur"},
            new string[]{t,"Eur","Us"}
        });
}

public static IEnumerable<string> TestCaseSourceA()
{
    yield return "a1";
    yield return "a2";
}
public static IEnumerable<string> TestCaseSourceB()
{
    yield return "b1";
    yield return "b2";
}

给出这组结果:test output

而你真的想要更像这样的东西:

//Maximizing Chrome Window.
int x=1280; //according to your screen resolution
int y=860;  //according to your screen resolution
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("window.resizeTo(x, y);");

test screensnip new