C# 多参数方法的自动测试功能;使用委托和泛型
我最近开始学习C#泛型和委托。在学习了一些之后,我创建了这个函数,作为自动测试任何单参数方法输出的一种手段:使用泛型和委托,它接受一个具有提供的类型、测试值和期望值的函数C# 多参数方法的自动测试功能;使用委托和泛型,c#,unit-testing,generics,delegates,automated-tests,C#,Unit Testing,Generics,Delegates,Automated Tests,我最近开始学习C#泛型和委托。在学习了一些之后,我创建了这个函数,作为自动测试任何单参数方法输出的一种手段:使用泛型和委托,它接受一个具有提供的类型、测试值和期望值的函数 public static class Testing { public static void FunctionTest<FunctionArgType, FunctionReturnType>(Func<FunctionArgType, FunctionReturnType> functio
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);
}
}
以下是我的测试功能的实现:
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');
public static double Mult(double a, double b)
{
return a * b;
}
public static void FunctionTest<TReturn>(
Func<TReturn> functionName,
TReturn expectedValue,
params object[] args)
{ .. }
我的问题:我的测试功能是否可以扩展到包括多参数测试方法?例如:
public static double SimpleSum(double num1, double num2)
{
return num1 + num2;
}
另外,作为C#这方面的新手,以及一般的自动化测试,这是一种体面的方法吗?我朝着一个好的方向前进了吗 有许多
Func
的重载,最多有16个参数。然而,为了使用测试方法,您必须为自己编写不同的重载
但是还有另一种解决方案:将lambda表达式传递给您的测试方法,该方法只有一个返回值(
Func
),并且只传递并记录预期的结果
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);
}
这样测试:
Testing.FunctionTest<double>(() => SimpleSquare(5), 25);
Testing.FunctionTest<double>(() => Mult(3, 4), 12);
如果仍要记录输入值,可以通过两种方式进行记录:
params
参数。它允许您向函数传递任意数量的参数:
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');
public static double Mult(double a, double b)
{
return a * b;
}
public static void FunctionTest<TReturn>(
Func<TReturn> functionName,
TReturn expectedValue,
params object[] args)
{ .. }
公共静态无效功能测试(
Func函数名,
TReturn期望值,
参数对象[]args)
{ .. }
您可以通过以下方式覆盖它:
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);
publicstaticvoid函数测试(Func函数,FunctionReturnType expectedValue)字符串passFail;
var returnedValue=function();
if(returnedValue.Equals(expectedValue))
{
passFail=“Pass”;
}
其他的
{
passFail=“Fail”;
}
ConsoleLogger(function.Method.ToString(),passFail,expectedValue,returnedValue);
}
功能测试(()=>SimpleSum(5,5,10);