in Sources/Common/Test.Psi.Common/TestRunner.cs [49:197]
public static void RunAll(string nameSubstring = "")
{
int passed = 0;
int skipped = 0;
var failed = new List<string>();
var testClasses = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(a => a.GetTypes())
.Where(t => t.GetCustomAttributes().Any(a => a.GetType() == typeof(TestClassAttribute)));
// allow running methods not marked with TestMethod attribute if the search string starts with "!"
var isDefinitiveName = nameSubstring.StartsWith("!");
if (isDefinitiveName)
{
nameSubstring = nameSubstring.Substring(1);
}
foreach (var testClass in testClasses)
{
var t = Activator.CreateInstance(testClass);
IEnumerable<MethodInfo> allTestMethods = testClass.GetMethods();
// allow running methods not marked with TestMethod attribute if the search string starts with "!"
if (!isDefinitiveName)
{
// by default, run only methods marked with TestMethod and not marked with Ignore.
allTestMethods = allTestMethods
.Where(m => m.GetCustomAttribute<TestMethodAttribute>() != null)
.Where(m => m.GetCustomAttribute<IgnoreAttribute>() == null);
}
var testMethods = allTestMethods
.Where(m => m.Name.Contains(nameSubstring) || m.DeclaringType.FullName.Contains(nameSubstring));
skipped += allTestMethods.Count() - testMethods.Count();
if (testMethods.Count() == 0)
{
continue;
}
Console.WriteLine("------------------------------------------------------");
Console.WriteLine("Test suite {0} ({1} test cases).", testClass.Name, testMethods.Count());
Console.WriteLine("------------------------------------------------------");
// run the test setup
var setup = testClass.GetMethods().Where(m => m.GetCustomAttribute<TestInitializeAttribute>() != null).FirstOrDefault();
if (setup != null)
{
Console.Write("Test case init... ");
setup.Invoke(t, null);
Console.WriteLine("Done.");
Console.WriteLine();
}
// run all tests
foreach (var m in testMethods)
{
Console.WriteLine("Test case " + m.Name + ":");
Exception exception = null;
string error = null;
try
{
var result = m.Invoke(t, null) as Task;
if (result != null)
{
result.Wait();
}
}
catch (Exception e)
{
exception = e.InnerException;
error = exception.ToString();
}
// if the method expects this exception, consider the test passed
var expectedExceptionAttribute = m.GetCustomAttribute<ExpectedExceptionAttribute>();
if (expectedExceptionAttribute != null)
{
if (exception == null)
{
error = "Expected exception of type " + expectedExceptionAttribute.ExceptionType.Name;
}
else if (m.GetCustomAttribute<ExpectedExceptionAttribute>().ExceptionType == exception.GetType())
{
error = null;
}
}
if (error == null)
{
GC.Collect();
Console.WriteLine("Passed.");
passed++;
}
else
{
var color = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(exception);
Console.ForegroundColor = color;
Console.WriteLine("Failed.");
failed.Add(testClass.Name + "." + m.Name);
}
Console.WriteLine();
}
// run the test setup
var cleanup = testClass.GetMethods().Where(m => m.GetCustomAttribute<TestCleanupAttribute>() != null).FirstOrDefault();
if (cleanup != null)
{
Console.Write("Test case cleanup... ");
cleanup.Invoke(t, null);
Console.WriteLine("Done.");
Console.WriteLine();
}
}
if (passed >= 0)
{
var color = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("{0} tests passed.", passed);
Console.ForegroundColor = color;
}
if (skipped > 0)
{
var color = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("{0} tests skipped.", skipped);
Console.ForegroundColor = color;
}
if (failed.Count > 0)
{
var color = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("{0} tests failed.", failed.Count);
foreach (var n in failed)
{
Console.WriteLine("\t" + n);
}
Console.ForegroundColor = color;
}
}