自动测试多参数方法的功能;使用委托和泛型

Function for automated testing of multi-parameter methods; using delegates and generics

我最近开始学习 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 个参数。但是,您必须自己编写测试方法的不同重载才能使用它们。


但还有另一种解决方案:将 lambda 表达式传递给只有 return 值 (Func<TReturn>) 的测试方法,并且只传递并记录预期结果。

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);

如果您仍想记录输入值,可以通过两种方式进行:

  1. 使用 params 参数。它允许您将任意数量的参数传递给函数:

    public static void FunctionTest<TReturn>(
        Func<TReturn> functionName,
        TReturn expectedValue,
        params object[] args)
    { .. }
    
  2. 使用反射来分析 lambda 表达式并直接从那里提取参数。见 .

您可以通过以下方式覆盖它:

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);