gpt4 book ai didi

c# - 如何对该方法进行单元测试

转载 作者:行者123 更新时间:2023-12-02 04:31:25 24 4
gpt4 key购买 nike

我正在学习单元测试。下面的方法如何在 VS 2010 上进行单元测试?

private string[] lstStrCalculatePoints()
{
string[] lstStrPoints = new string[2];
if (lblKPIAResult.Text.Contains("Exceeds"))
{
lstStrPoints[0] = "AHT";
}

if (lblKPIBResult.Text.Contains("Exceeds"))
{
lstStrPoints[1] = "QA";
}

return lstStrPoints;
}

请提供示例。谢谢!

最佳答案

我会创建两个新方法:

public static string GetKpiPoint(string kpiResult, string ifExceeds)
{
return kpiResult.Contains("Exceeds") ? ifExceeds : null;
}

public static string[] CalculatePoints(string kpiAResult, string kpiBResult)
{
return new string[] { GetKpiPoint(kpiAResult, "AHT"), GetKpiPoint(kpiBResult, "QA") };
}

然后从现有的调用它:

private string[] lstStrCalculatePoints()
{
return CalcualtePoints(lblKPIAResult.Text, lblKPIBResult.Text);
}

编辑:测试此方法的方式取决于您的测试框架,但如果您使用 NUnit,您可以执行以下操作:

[Test]
[TestCase("Exceeds", "Exceeds", new string[] { "AHT", "QA" })]
[TestCase("NoMatch", "NoMatch", new string[] { null, null })]
[TestCase("Exceeds", "NoMatch", new string[] { "AHT", null })]
[TestCase("NoMatch", "Exceeds", new string[] { null, "QA" })]
public void CalculatePointsTest(string kpiA, string kpiB, string[] expected)
{
CollectionAssert.AreEqual(expected, CalculatePoints(kpiA, kpiB));
}

关于c# - 如何对该方法进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23021062/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com