我最近开始学习C#泛型和委托。在学习了一些之后,我创建了这个函数,作为自动测试任何单参数方法输出的一种方法:使用泛型和委托,它接受一个具有提供的类型、测试值和期望值的函数。
public static class Testing
{
public static void FunctionTest<FunctionArgType, FunctionReturnType>(Func<FunctionArgType, FunctionReturnType> functionName, FunctionArgType testValue, FunctionReturnType expectedValue)
{
string passFail;
var returnedValue = functionName(testValue);
if (returnedValue.Equals(expectedValue))
{
passFail = "Pass";
}
else
{
passFail = "Fail";
}
ConsoleLogger(functionName.Method.ToString(), passFail, testValue, expectedValue, returnedValue);
}
}
这里有两种非常简单的测试方法:
public static double SimpleSquare(double num)
{
return num * num;
}
public static char FirstLetter(string value)
{
return value[0];
}
以下是我的测试功能的实现:
Testing.FunctionTest<double, double>(SimpleSquare, 5, 25);
Testing.FunctionTest<double, double>(SimpleSquare, 4, 20);
Testing.FunctionTest<string, char>(FirstLetter, "Ryan", 'R');
Testing.FunctionTest<string, char>(FirstLetter, "Brian", 'n');
控制台输出:
Double SimpleSquare(Double) Pass
Input: 5; Expected: 25; Returned: 25
-------------------------------------------------
Double SimpleSquare(Double) Fail
Input: 4; Expected: 20; Returned: 16
-------------------------------------------------
Char FirstLetter(System.String) Pass
Input: Ryan; Expected: R; Returned: R
-------------------------------------------------
Char FirstLetter(System.String) Fail
Input: Brian; Expected: n; Returned: B
-------------------------------------------------
我的问题:我的测试功能是否可以扩展到包括具有多个参数的测试方法?例如:
public static double SimpleSum(double num1, double num2)
{
return num1 + num2;
}
此外,作为C#这方面的新手,以及一般的自动化测试,这是一种不错的方法吗?我是否朝着一个好的方向前进?
Func<>
有许多重载,最多有16个参数。然而,为了使用它们,您将不得不为自己编写测试方法的不同重载。
但还有另一种解决方案:将一个只有返回值(Func<TReturn>
)的lambda表达式传递给测试方法,然后只传递并记录预期结果。
public static void FunctionTest<TReturn>(
Func<TReturn> functionName,
TReturn expectedValue)
{
string passFail;
var returnedValue = functionName();
if (returnedValue.Equals(expectedValue)) {
passFail = "Pass";
} else {
passFail = "Fail";
}
ConsoleLogger(functionName.Method.ToString(), passFail, expectedValue, returnedValue);
}
使用此附加功能:
public static double Mult(double a, double b)
{
return a * b;
}
测试如下:
Testing.FunctionTest<double>(() => SimpleSquare(5), 25);
Testing.FunctionTest<double>(() => Mult(3, 4), 12);
C#可以推断函数类型。因此,您也可以在调用时删除通用参数
Testing.FunctionTest(() => SimpleSquare(5), 25);
Testing.FunctionTest(() => Mult(3, 4), 12);
如果你仍然想记录输入值,你可以用两种方法:
使用
params
参数。它允许您将任意数量的参数传递给函数:public static void FunctionTest<TReturn>( Func<TReturn> functionName, TReturn expectedValue, params object[] args) { .. }
使用反射来分析lambda表达式并直接从中提取参数。看见https://stackoverflow.com/a/3766713/880990.
您可以通过以下方式覆盖它:
public static void FunctionTest<FunctionReturnType>(Func<FunctionReturnType> function, FunctionReturnType expectedValue) string passFail;
var returnedValue = function();
if (returnedValue.Equals(expectedValue))
{
passFail = "Pass";
}
else
{
passFail = "Fail";
}
ConsoleLogger(function.Method.ToString(), passFail, expectedValue, returnedValue);
}
Testing.FunctionTest(() => SimpleSum(5,5), 10);