Yats Static Method Test Cases

By | February 18, 2017

In version 0.9.9 I have implemented support for very simple test cases – a static method can be a test case. Until now test cases had to be classes. All neat things like cancelling, parameter configuration and result passing are supported.

Here is one example of a minimal test case method:

[TestMethod]
public static ITestResult Increment(int input, out int result)
{
    result = input + 1;
    return new TestResult(ResultEnum.PASS);
}

In order to be discovered as a test case, the method has to meet two conditions: have [TestMethod] attribute and return a ITestResult. Passing arguments by ref is not supported – we either have parameters or results. Out arguments are treated as results.

For more convenience you can add various attributes to the method and its arguments. Another example test case:

[TestMethod]
[Name("Read int")]
[Description("Read an integer from a port")]
        
public static ITestResult ReadIntFromPort(
    [Description("Port settings")]
    SerialPortSettingsParameter portSettings,
    [Description("Receive timeout")]
    TimeSpan timeout,
    [Description("Result. Received string parsed to an integer")]
    out int N,
    [CancelHandler]
    WaitHandle cancelHandler
    )
{
    AbstractPort port = null;
    N = 0;
    try
    {
        port = PortOpenHelper.Open(portSettings, cancelHandler);

        var parser = new TextLineParser();

        ReadOperationResult result = port.Execute(timeout, parser, cancelHandler);

        if (result.PortException != null)
        {
            Logger.Warn("Exception", result.PortException);
            return new TestResult(ResultEnum.INCONCLUSIVE);
        }

        if (result.SerialPortError.HasValue)
        {
            Logger.Warn("Serial port error: " + result.SerialPortError.Value);
            return new TestResult( ResultEnum.INCONCLUSIVE);
        }

        return new TestResult(int.TryParse(parser.GetReceivedLine(), out N));
    }
    catch
    {
        throw;
    }
    finally
    {
        PortOpenHelper.Close(port, portSettings);
    }
}
}

A special case is an argument of WaitHandle type, marked with [CancelHandler]. If such argument is found, the running test case can be cancelled as shown in the example above.

Leave a Reply

Your email address will not be published.