C# 使用Nunit 3.x的参数化测试
我在测试时遇到了以下场景,我想问你们是否有测试这个的捷径C# 使用Nunit 3.x的参数化测试,c#,unit-testing,testing,nunit,nunit-3.0,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 PostBetSl
[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));
}
}
[测试]
[TestCaseSource(名称(MlTestCases))]
[TestCaseSource(名称(QaTestCases))]
public void EditBetSlip\u shouldConvertODDSF罗马十进制(字符串选择ID)
{
//安排
var betSlipRequest=新的PostBetSlipRequest
{
OddStyle=OddStyle.American.ToString(),
选择=新列表
{
新PostOneSelectionRequest
{
DisplayLobbits=$“+{Fixture.Create()}”,
Id=selectionId.Replace(“#”,“%23”),
},
},
下注=新名单
{
新PostOneBetRequest
{
OddStyle=OddStyle.American.ToString(),
Id=0,
桩=10,
},
},
};
//表演
_client.EditBetslip(betSlipRequest);
var response=_client.RefreshBetslip(新的GetBetSlipRequest{OddStyle=OddStyle.European.ToString()});
var betslip=response.DeserializedBody;
//断言
Assert.IsTrue(response.StatusCode==HttpStatusCode.OK);
foreach(betslip.Selections中的变量选择)
{
Assert.DoesNotThrow(()=>decimal.Parse(selection.displayLoverts));
}
}
现在我需要再次进行相同的测试,但只需翻转PostBetSlipRequest
和GetBetSlipRequest
的OddStyle
。我尝试了[Values]
属性,但它没有按我想要的方式工作
我想用American-European
执行所有这两个测试用例源一次,再用European-American
执行另一次,这可能吗?每个场景(US-->Eur&Eur-->US)都是测试方法的新测试用例吗
按原样,您有n个测试用例,其中n=QaTestCases的总数+MlTestCases的总数
实际上,您需要测试2n个测试用例(每个[Eur-->US,US-->Eur]置换的每个现有测试用例)。因此,我建议这应该是一个新的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";
}
[测试]
[TestCaseSource(名称(TestCaseSourceA))]
[TestCaseSource(名称(TestCaseSourceB))]
public void GivenX\u ShouldReturnOk(字符串输入)
{
//一些测试
Assert.Pass();
}
公共静态IEnumerable TestCaseSourceA()
{
收益率回报率“a1”;
收益率回报率“a2”;
}
公共静态IEnumerable TestCaseSourceB()
{
收益率回报率“b1”;
收益率回报率“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";
}
[测试]
[TestCaseSource(名称(TestCaseSourceMaster))]
public void GivenX_ShouldReturnOk(字符串输入,字符串fromOddsStyle,字符串到DddsStyle)
{
//一些测试
Assert.Pass();
}
公共静态IEnumerable TestCaseSourceMaster()
{
返回TestCaseSourceA()
.Concat(TestCaseSourceB())
.SelectMany(t=>newstring[]
{
新字符串[]{t,“US”,“Eur”},
新字符串[]{t,“Eur”,“Us”}
});
}
公共静态IEnumerable TestCaseSourceA()
{
收益率回报率“a1”;
收益率回报率“a2”;
}
公共静态IEnumerable TestCaseSourceB()
{
收益率回报率“b1”;
收益率回报率“b2”;
}
嗯,是的,在我发布问题之后,我实际上是在做这件事,但现在我发现这有点麻烦,因为我需要编写所有那些会污染我的测试夹具的静态“助手”方法(当然我可以在另一个类中提取它们,但仍然如此)