Skip to content

Commit

Permalink
Merge pull request #10 from sportyturks/feature/refactor-code-cleanup
Browse files Browse the repository at this point in the history
Feature/refactor code cleanup
  • Loading branch information
sportyturks authored Jun 17, 2024
2 parents 415f2ee + e80ba75 commit dbcf684
Show file tree
Hide file tree
Showing 45 changed files with 398 additions and 510 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ public class AssemblyInitialise
[AssemblyInitialize]
public static void Initialise(TestContext testContext)
{
WebTestManager.Instance().OnInitialiseAssemblyDependencies(testContext);
WebTestManager.Instance.OnInitialiseAssemblyDependencies(testContext);
}

[AssemblyCleanup]
public static void Cleanup()
{
WebTestManager.Instance().OnDisposeAssemblyDependencies();
WebTestManager.Instance.OnDisposeAssemblyDependencies();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.4.3" />
<PackageReference Include="MSTest.TestFramework" Version="3.4.3" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public void GetAppConfigConfigurationValues()
Assert.IsTrue(configurationReader.GetConfigurationValue("ForceKillProcess") == "false");
Assert.IsTrue(configurationReader.GetConfigurationValue("Headless") == "false");
Assert.IsTrue(configurationReader.GetConfigurationValue("BrowserOs").ToLower() == "linux");

}

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
using System;
using AutoTestMate.MsTest.Infrastructure.Core.MethodManager;
using AutoTestMate.MsTest.Web.Core;
using AutoTestMate.MsTest.Web.Core.MethodManager;
using AutoTestMate.MsTest.Web.Extensions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AutoTestMate.MsTest.Infrastructure.IntegrationTests
{
[TestClass]
public class LoggingUtilityTests
public class LoggingUtilityTests: WebTestBase
{
[TestMethod]
public void TestMethod1()
public void EnsureLoggingFileCreated()
{
LoggingUtility.Info("This is an info message");
LoggingUtility.Error("This is an info message");
}
[TestMethod]
public void EnsureScreenshotCreated()
{
LoggingUtility.Info("This is an info message");
LoggingUtility.Error("This is an info message");

CaptureScreenshot();
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.4.3" />
<PackageReference Include="MSTest.TestFramework" Version="3.4.3" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using AutoTestMate.MsTest.Infrastructure.Core;
using AutoTestMate.MsTest.Infrastructure.Helpers;
Expand All @@ -21,9 +22,9 @@ public virtual void CustomAttributesInitialise(string testMethod)
var method = GetType().GetMethod(TestContext.TestName);

_actionTestDataAttributes = method.GetCustomAttributes(typeof(ITestDataAttribute), true).OfType<ITestDataAttribute>();
foreach (var tesdataAttribute in _actionTestDataAttributes)
foreach (var testDataAttribute in _actionTestDataAttributes)
{
tesdataAttribute.BeforeTest(testMethod, TestContext, TestManager);
testDataAttribute.BeforeTest(testMethod, TestContext, TestManager);
}

if (classAttributes.Any())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
<Prefer32bit>false</Prefer32bit>
</PropertyGroup>

<PropertyGroup>
<RepositoryUrl>https://github.com/sportyturks/AutoTestMate.MsTest</RepositoryUrl>
</PropertyGroup>

<ItemGroup>
<Compile Remove="Helpers\MQ\**" />
<EmbeddedResource Remove="Helpers\MQ\**" />
Expand Down Expand Up @@ -49,8 +53,8 @@
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
<PackageReference Include="NLog" Version="4.7.10" />
<PackageReference Include="MSTest.TestFramework" Version="3.4.3" />
<PackageReference Include="NLog" Version="5.3.2" />
<PackageReference Include="System.Data.Common" Version="4.3.0" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class AppConfiguration : IConfiguration
{
public AppConfiguration()
{
Settings = new NameValueCollection();
Settings = [];

var configBuilder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
Expand Down
4 changes: 2 additions & 2 deletions AutoTestMate.MsTest.Infrastructure/Core/AssemblyInitialise.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ public static class AssemblyInitialise
[AssemblyInitialize]
public static void Initialise(TestContext testContext)
{
TestManager.Instance().OnInitialiseAssemblyDependencies(testContext);
TestManager.Instance.OnInitialiseAssemblyDependencies(testContext);
}

[AssemblyCleanup]
public static void Cleanup()
{
TestManager.Instance().OnDisposeAssemblyDependencies();
TestManager.Instance.OnDisposeAssemblyDependencies();
}
}
}
54 changes: 22 additions & 32 deletions AutoTestMate.MsTest.Infrastructure/Core/ConfigurationReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,11 @@ namespace AutoTestMate.MsTest.Infrastructure.Core
/// </summary>
public class ConfigurationReader : IConfigurationReader
{
#region Private Variables

private readonly IDictionary<string, string> _settings;
private readonly IConfiguration _appConfiguration;
private readonly TestContext _testContext;

#endregion

#region Constructor

public ConfigurationReader(TestContext testContext, IConfiguration appConfiguration)
{
_settings = new Dictionary<string, string>();
_testContext = testContext;
_appConfiguration = appConfiguration;
Settings = new Dictionary<string, string>();
TestContext = testContext;
AppConfiguration = appConfiguration;

if (testContext != null)
{
Expand All @@ -34,33 +24,31 @@ public ConfigurationReader(TestContext testContext, IConfiguration appConfigurat

public ConfigurationReader(TestContext testContext, IConfiguration appConfiguration, IDictionary<string, string> settings)
{
_testContext = testContext;
_appConfiguration = appConfiguration;
_settings = settings;
TestContext = testContext;
AppConfiguration = appConfiguration;
Settings = settings;
}

#endregion

#region Public Methods

/// <summary>
/// Gets a value from the configuration file.
/// </summary>
public string GetConfigurationValue(string key, bool required = false)
{
if (_settings.Count == 0 && _appConfiguration.Settings.Count == 0)
if (Settings.Count == 0 && AppConfiguration.Settings.Count == 0)
{
throw new KeyNotFoundException($"{key} was not found in the test parameters. Please make sure that the solution has an active .runsettings file and that the parameter is valid.");
}

var testSettingsValue = _settings.ContainsKey(key) ? _settings[key] : string.Empty;
var testSettingsValue = Settings.TryGetValue(key, out var setting) ? setting : string.Empty;
if (!string.IsNullOrWhiteSpace(testSettingsValue))
{
return testSettingsValue;
}

var appSettingsDict = _appConfiguration.Settings.AllKeys.ToDictionary(k => k, k => _appConfiguration.Settings[k]);
var appSettingsValue = appSettingsDict.ContainsKey(key) ? appSettingsDict[key] : string.Empty;
var appSettingsDict = AppConfiguration.Settings.AllKeys.ToDictionary(k => k, k => AppConfiguration.Settings[k]);
var appSettingsValue = appSettingsDict.TryGetValue(key, out var value) ? value : string.Empty;
if (!string.IsNullOrWhiteSpace(appSettingsValue))
{
return string.Equals(appSettingsValue, Constants.Configuration.NullValue) ? null : appSettingsValue;
Expand All @@ -80,13 +68,13 @@ public void SetTestContext(TestContext testContext)
foreach (var key in keys)
{
var value = testContext.Properties[key.ToString()].ToString();
if (!_settings.TryGetValue(key.ToString(), out _))
if (!Settings.TryGetValue(key.ToString(), out _))
{
_settings.Add(key.ToString(), value);
Settings.Add(key.ToString(), value);
}
else
{
_settings[key.ToString()] = value;
Settings[key.ToString()] = value;
}
}

Expand All @@ -111,17 +99,17 @@ public void SetTestContext(TestContext testContext)
}
public void AddSetting(string key, string value)
{
if (!_settings.ContainsKey(key))
if (!Settings.ContainsKey(key))
{
_settings.Add(key, value);
Settings.Add(key, value);
}
}

public bool UpdateSetting(string key, string value)
{
if (!_settings.ContainsKey(key)) return false;
if (!Settings.ContainsKey(key)) return false;

_settings[key] = value;
Settings[key] = value;

return true;
}
Expand All @@ -130,9 +118,11 @@ public bool UpdateSetting(string key, string value)
#region Public Properties
public string LogLevel => GetConfigurationValue(Constants.Configuration.LogLevelKey);
public string LogName => GetConfigurationValue(Constants.Configuration.LogNameKey);
public IDictionary<string, string> Settings => _settings;
public IConfiguration AppConfiguration => _appConfiguration;
public TestContext TestContext => _testContext;
public IDictionary<string, string> Settings { get; }

public IConfiguration AppConfiguration { get; }

public TestContext TestContext { get; }

#endregion
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ namespace AutoTestMate.MsTest.Infrastructure.Core
{
public class EmptyConfiguration : IConfiguration
{
public EmptyConfiguration()
{
Settings = new NameValueCollection();
}
public NameValueCollection Settings { get; set; }
public NameValueCollection Settings { get; set; } = [];
}
}
28 changes: 14 additions & 14 deletions AutoTestMate.MsTest.Infrastructure/Core/ITestManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@

namespace AutoTestMate.MsTest.Infrastructure.Core
{
public interface ITestManager
{
public interface ITestManager
{
WindsorContainer Container { get; }
TestContext TestContext { get; }
IConfigurationReader ConfigurationReader { get; }
ITestMethodManager TestMethodManager { get; }
TestContext TestContext { get; }
IConfigurationReader ConfigurationReader { get; }
ITestMethodManager TestMethodManager { get; }
IConfiguration AppConfiguration { get; }
ILoggingUtility LoggingUtility { get; }
void OnInitialiseAssemblyDependencies(TestContext testContext = null);
void OnDisposeAssemblyDependencies();
ILoggingUtility LoggingUtility { get; }
void OnInitialiseAssemblyDependencies(TestContext testContext = null);
void OnDisposeAssemblyDependencies();
void OnTestMethodInitialise(string testMethod, TestContext testContext = null);
void OnTestCleanup(string testMethod);
void InitialiseIoc();
void InitialiseTestContext(TestContext testContext = null);
void InitialiseTestContextDependencies();
void OnTestCleanup(string testMethod);
void InitialiseIoc();
void InitialiseTestContext(TestContext testContext = null);
void InitialiseTestContextDependencies();
void Dispose(string testMethod);
void UpdateConfigurationReader(string testMethod, IConfigurationReader configurationReader);
void SetTextContext(TestContext testContext);
}
void SetTestContext(TestContext testContext);
}
}
15 changes: 11 additions & 4 deletions AutoTestMate.MsTest.Infrastructure/Core/LoggingUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,15 @@ private void SetupLogger()

var outputDirectory = _configurationReader.GetConfigurationValue(Constants.Configuration.OutputFileDirectory);
string outputFile;

if (!string.IsNullOrWhiteSpace(outputDirectory) && outputDirectory.Contains("/")) //handle relative paths

if (!string.IsNullOrWhiteSpace(outputDirectory) && outputDirectory.Contains("~")) //handle relative paths
{
var homeDirRoot = Environment.GetEnvironmentVariable("HOME") + "/";
var outputDir = outputDirectory.Trim('~');
var homeDir = Path.GetDirectoryName(homeDirRoot) + outputDir;
outputFile = $"{homeDir}/{_defaultFileName}";
}
else if (!string.IsNullOrWhiteSpace(outputDirectory) && outputDirectory.Contains("/")) //handle relative paths
{
outputFile = $"{outputDirectory}/{_defaultFileName}";
}
Expand Down Expand Up @@ -105,8 +112,8 @@ public void Debug(string message, bool logTestContext = false)
}

private void TestContextWriteLine(string message, bool logTestContext)
{
if (logTestContext)
{
if (logTestContext)
{
_testContext.WriteLine(message);
}
Expand Down
2 changes: 1 addition & 1 deletion AutoTestMate.MsTest.Infrastructure/Core/MemoryCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace AutoTestMate.MsTest.Infrastructure.Core
{
public class MemoryCache : IMemoryCache
{
public const int DefaultCacheMinutes = 5;
private const int DefaultCacheMinutes = 5;

private readonly Microsoft.Extensions.Caching.Memory.MemoryCache _memoryCache;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,16 @@

namespace AutoTestMate.MsTest.Infrastructure.Core.MethodManager
{
public abstract class TestMethodBase : ITestMethodBase, IDisposable
public abstract class TestMethodBase(
ILoggingUtility loggingUtility,
IConfigurationReader configurationReader,
string testMethod)
: ITestMethodBase, IDisposable
{
protected TestMethodBase(ILoggingUtility loggingUtility, IConfigurationReader configurationReader, string testMethod)
{
IsInitialised = false;
LoggingUtility = loggingUtility;
ConfigurationReader = configurationReader;
TestMethod = testMethod;
}

public string TestMethod { get; set; }
public bool IsInitialised { get; set; }
public IConfigurationReader ConfigurationReader { get; set; }
public ILoggingUtility LoggingUtility { get; set; }
public string TestMethod { get; set; } = testMethod;
public bool IsInitialised { get; set; } = false;
public IConfigurationReader ConfigurationReader { get; set; } = configurationReader;
public ILoggingUtility LoggingUtility { get; set; } = loggingUtility;

public virtual void Dispose()
{
Expand Down
Loading

0 comments on commit dbcf684

Please sign in to comment.