Hi everyone,
We are trying out some work with test steps. We know that when we face or throw an exception during a TestStep.Run(), the execution will stop. Is there a way we can know how what exception was thrown? or handle it?
Would appreciate some inputs, thanks!
Hi @antonio.quizon, The best way to know which exceptions are thrown is by looking in the log file. The best way to handle exceptions is using try/catch.
Since you ask, maybe thats because you cannot use either of those options, but there might be other things you can do. Can you elaborate a bit more on the specific situation?
Our step’s run looks like this:
public class MyTest: CustomTestStep
{
...
public override void Run()
{
try
{
}
catch (Exception ex)
{
UpgradeVerdictToError($"Step '{Name}': {ex.Message}", ex);
}
}
...
and we use this in our derived class off of TestStep so that we have an easy way to invoke it and a single place to change it in the future.
public class CustomTestStep : TestStelp
{
...
/// <summary>
/// Sets the Verdict to Error and logs the error message and exception if provided.
/// </summary>
/// <param name="errorMessage">Message to log, will use exception if null or empty</param>
/// <param name="exception">Exception if any</param>
/// <param name="rethrow">Rethrow exception if any</param>
/// <param name="caller">Caller of this method</param>
public void UpgradeVerdictToError(string errorMessage, Exception exception = null, bool rethrow = false, [CallerMemberName] string caller = "")
{
if (exception != null)
{
Log.Debug($"{caller}: {exception}");
if (errorMessage == null || errorMessage.Length == 0)
errorMessage = exception.ToString();
}
Log.Error(errorMessage);
UpgradeVerdict(Verdict.Error);
if (rethrow)
throw exception;
}
...
}