public static async Task<List<TestResult>> RunTests()
{
List<TestResult> res = new List<TestResult>();
// Consider the namespace of the tests:
IEnumerable<Type> typelist = GetTypesInNamespace(Assembly.GetExecutingAssembly(), "LiquidTextUWP.Tests");
foreach (Type testClass in typelist)
{
// Get all the test methods in the class:
foreach (MethodInfo method in testClass.GetMethods())
{
LTTestAttribute testAttr = method.GetCustomAttributes<LTTestAttribute>().FirstOrDefault();
if (testAttr == null)
continue;
object instance = Activator.CreateInstance(testClass);
try
{
if (testAttr.IsAsync)
await (Task) method.Invoke(instance, null);
else
method.Invoke(instance, null);
res.Add(new TestResult(true));
}
catch (Exception e)
{
res.Add(new TestResult(false, string.Format("{0} - {1}", e.Message, e.InnerException?.Message)));
}
}
}
return res;
}
private static IEnumerable<Type> GetTypesInNamespace(Assembly assembly, string nameSpace)
{
return assembly.GetTypes().Where(t => (t.Namespace != null) && t.Namespace.StartsWith(nameSpace));
}
public struct TestResult
{
public bool Success { get; set; }
public string ErrorMsg { get; set; }
public TestResult(bool success, string msg = "")
{
Success = success;
ErrorMsg = msg;
}
}
[AttributeUsage(AttributeTargets.Method)]
public class LTTestAttribute : Attribute
{
public bool IsAsync { get; set; } = false;
}
public static async Task<List<TestResult>> RunTests()
{
List<TestResult> res = new List<TestResult>();
// Consider the namespace of the tests:
IEnumerable<Type> typelist = GetTypesInNamespace(Assembly.GetExecutingAssembly(), "LiquidTextUWP.Tests");
foreach (Type testClass in typelist)
{
// Get all the test methods in the class:
foreach (MethodInfo method in testClass.GetMethods())
{
LTTestAttribute testAttr = method.GetCustomAttributes<LTTestAttribute>().FirstOrDefault();
if (testAttr == null)
continue;
object instance = Activator.CreateInstance(testClass);
try
{
if (testAttr.IsAsync)
await (Task) method.Invoke(instance, null);
else
method.Invoke(instance, null);
res.Add(new TestResult(true));
}
catch (Exception e)
{
res.Add(new TestResult(false, string.Format("{0} - {1}", e.Message, e.InnerException?.Message)));
}
}
}
return res;
}
private static IEnumerable<Type> GetTypesInNamespace(Assembly assembly, string nameSpace)
{
return assembly.GetTypes().Where(t => (t.Namespace != null) && t.Namespace.StartsWith(nameSpace));
}
public struct TestResult
{
public bool Success { get; set; }
public string ErrorMsg { get; set; }
public TestResult(bool success, string msg = "")
{
Success = success;
ErrorMsg = msg;
}
}
[AttributeUsage(AttributeTargets.Method)]
public class LTTestAttribute : Attribute
{
public bool IsAsync { get; set; } = false;
}