使用Nunit 3.x进行参数化测试
问题描述:
我在测试时遇到以下情况,我想问问大家是否有快捷方式来测试它。使用Nunit 3.x进行参数化测试
[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));
}
}
现在我需要再次进行相同的测试,但只需翻转的PostBetSlipRequest
和GetBetSlipRequest
的OddStyle
。我尝试了[Values]
属性,但它不能按我想要的方式工作。
我想要的是执行所有这两个测试用例源与American - European
和另一个时间与European - American
是否有可能?
答
当然,每种情况(美国 - >欧洲&欧元 - >美国)是测试方法的新测试用例吗?
由于您有n个测试用例,其中n = QaTestCases的总数+ MlTestCases的总数。
您实际上想要测试2n个测试用例(每个[Eur - > US,US - > 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";
}
嗯嗯,我真的有点儿doi以后我发布了这个问题,但现在我觉得有点麻烦,因为我需要编写所有那些会污染我的测试夹具的静态“助手”方法(当然,我可以在另一个类中提取它们,但仍然是) – kuskmen