2023-04-12 11:53:55 +00:00
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
using Utils;
|
|
|
|
|
|
|
|
|
|
namespace Logging
|
|
|
|
|
{
|
2023-04-14 12:53:39 +00:00
|
|
|
|
public class TestLog : BaseLog
|
2023-04-12 11:53:55 +00:00
|
|
|
|
{
|
|
|
|
|
private readonly NumberSource subfileNumberSource = new NumberSource(0);
|
2023-04-14 12:53:39 +00:00
|
|
|
|
private readonly string methodName;
|
|
|
|
|
private readonly string fullName;
|
2023-04-12 11:53:55 +00:00
|
|
|
|
|
2023-06-21 08:06:54 +00:00
|
|
|
|
public TestLog(string folder, bool debug, string name = "")
|
2023-04-25 09:31:15 +00:00
|
|
|
|
: base(debug)
|
2023-04-12 11:53:55 +00:00
|
|
|
|
{
|
2023-06-21 08:06:54 +00:00
|
|
|
|
methodName = GetMethodName(name);
|
2023-04-14 12:53:39 +00:00
|
|
|
|
fullName = Path.Combine(folder, methodName);
|
2023-04-12 11:53:55 +00:00
|
|
|
|
|
2023-04-19 08:42:08 +00:00
|
|
|
|
Log($"*** Begin: {methodName}");
|
2023-04-12 11:53:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-14 12:53:39 +00:00
|
|
|
|
public LogFile CreateSubfile(string ext = "log")
|
2023-04-12 11:53:55 +00:00
|
|
|
|
{
|
2023-04-14 12:53:39 +00:00
|
|
|
|
return new LogFile($"{fullName}_{GetSubfileNumber()}", ext);
|
2023-04-12 11:53:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void EndTest()
|
|
|
|
|
{
|
|
|
|
|
var result = TestContext.CurrentContext.Result;
|
|
|
|
|
|
2023-04-19 08:42:08 +00:00
|
|
|
|
Log($"*** Finished: {methodName} = {result.Outcome.Status}");
|
2023-04-12 11:53:55 +00:00
|
|
|
|
if (!string.IsNullOrEmpty(result.Message))
|
|
|
|
|
{
|
|
|
|
|
Log(result.Message);
|
|
|
|
|
Log($"{result.StackTrace}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (result.Outcome.Status == NUnit.Framework.Interfaces.TestStatus.Failed)
|
|
|
|
|
{
|
2023-04-14 12:53:39 +00:00
|
|
|
|
MarkAsFailed();
|
2023-04-12 11:53:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-06-21 08:06:54 +00:00
|
|
|
|
|
2023-04-14 12:53:39 +00:00
|
|
|
|
protected override LogFile CreateLogFile()
|
2023-04-12 11:53:55 +00:00
|
|
|
|
{
|
2023-04-14 12:53:39 +00:00
|
|
|
|
return new LogFile(fullName, "log");
|
2023-04-12 11:53:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-06-21 08:06:54 +00:00
|
|
|
|
private string GetMethodName(string name)
|
2023-04-12 11:53:55 +00:00
|
|
|
|
{
|
2023-06-21 08:06:54 +00:00
|
|
|
|
if (!string.IsNullOrEmpty(name)) return name;
|
2023-04-14 12:53:39 +00:00
|
|
|
|
var test = TestContext.CurrentContext.Test;
|
|
|
|
|
var args = FormatArguments(test);
|
|
|
|
|
return $"{test.MethodName}{args}";
|
2023-04-12 11:53:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-14 12:53:39 +00:00
|
|
|
|
private string GetSubfileNumber()
|
2023-04-12 11:53:55 +00:00
|
|
|
|
{
|
2023-04-14 12:53:39 +00:00
|
|
|
|
return subfileNumberSource.GetNextNumber().ToString().PadLeft(6, '0');
|
2023-04-12 11:53:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string FormatArguments(TestContext.TestAdapter test)
|
|
|
|
|
{
|
|
|
|
|
if (test.Arguments == null || !test.Arguments.Any()) return "";
|
|
|
|
|
return $"[{string.Join(',', test.Arguments)}]";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|