JD2022-TU1/main/tools/framework/JD.Application.TeaBox/Tests/FunctionalTest/FunctionalTestBase.cs

73 lines
No EOL
2.2 KiB
C#

using System;
using JD.Application.Program;
using JD.Collections.Extensions;
using JD.LogService;
using NUnit.Framework;
namespace JD.Application.Tests.FunctionalTest
{
/// <summary>
/// All the application functional tests should derive from this class.
/// </summary>
public abstract class FunctionalTestBase
{
private GenericApplicationStartup _application;
private IDisposable _applicationDisposableHelper;
/// <summary>
/// Represents the logging context to fetch the application logs
/// </summary>
protected LoggingContext TestLogContext { get; private set; }
/// <summary>
/// Returns the startup type of the application to test.
/// </summary>
/// <returns></returns>
protected abstract GenericApplicationStartup CreateApplicationStartup();
protected FunctionalTestBase()
{
TestLogContext = new LoggingContext();
LogService.LogService.AddLogger(TestLogContext);
}
[OneTimeSetUp]
public void LaunchApplication()
{
_application = CreateApplicationStartup();
System.Windows.Forms.Application.EnableVisualStyles();
System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);
_applicationDisposableHelper = _application.StartEmbedded();
// After launching, we wait for the application to be ready before starting tests
System.Windows.Forms.Application.DoEvents();
}
[TearDown]
public void CheckErrorLog()
{
System.Windows.Forms.Application.DoEvents();
if(!TestLogContext.Errors.IsEmpty())
Assert.Fail(ConstrutErrorMessage());
}
private string ConstrutErrorMessage()
{
var errorMessage = String.Empty;
foreach (var error in TestLogContext.Errors)
{
errorMessage += error + "\r\n";
}
return errorMessage;
}
[OneTimeTearDown]
public void CloseApplication()
{
_applicationDisposableHelper?.Dispose();
}
}
}