简体   繁体   English

使用 Nunit 3.x 的参数化测试

[英]Parametrized tests using Nunit 3.x

I am having following scenario while testing and I want to ask you guys if there is shortcut for testing this.我在测试时遇到了以下场景,我想问你们是否有测试这个的捷径。

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

Now I need to make the same test again but simply flip the OddStyle of PostBetSlipRequest and GetBetSlipRequest .现在我需要再次进行相同的测试,但只需翻转OddStylePostBetSlipRequestGetBetSlipRequest I tried with [Values] attribute but it doesn't work the way I want.我尝试使用[Values]属性,但它不能按我想要的方式工作。

What I want is to execute all these two test case sources once with American - European and another time with European - American is it possible?我想要的是一次与American - European和另一次与European - American一起执行所有这两个测试用例源,这可能吗?

Surely each scenario (US --> Eur & Eur --> US) is a new test case for the test method?肯定每个场景(US --> Eur & Eur --> US)都是测试方法的新测试用例?

As is you have n test cases where n = the total number of QaTestCases + the total number of MlTestCases.你有 n 个测试用例,其中 n = QaTestCases 的总数 + MlTestCases 的总数。

You actually want to test 2n test cases (each existing test case for each [Eur --> US, US --> Eur] permuatation).您实际上想要测试 2n 个测试用例(每个 [Eur --> US, US --> Eur] 排列的每个现有测试用例)。 Id suggest that this therefore should just be a new TestCaseSource using the existing ones and adding in the Eur/US permutations.因此我建议这应该只是一个新的TestCaseSource使用现有的TestCaseSource并添加 Eur/US 排列。

Stripping it right back, you have something vaguely like this just now:把它剥离回来,你刚才有一些模糊的东西:

[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";
}

giving this set of results:给出这组结果: 测试输出

Whereas you really want something more like this:而你真的想要更像这样的东西:

[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";
}

测试屏幕截图新

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM