internal static void Main(string[] args) { // Check the parameter. string assembly = String.Join(" ", args); if (!File.Exists(assembly)) { Console.Error.WriteLine("Assembly not found: " + assembly); Environment.Exit(-1); } // Run the unit tester. try { Environment.ExitCode = UnitTester.Run(assembly); } catch (ReflectionTypeLoadException ex) { Console.Error.WriteLine("Failed to load types:"); foreach (Exception e in ex.LoaderExceptions) { Console.Error.WriteLine(); Console.Error.WriteLine(" " + e.Message); } Environment.Exit(-2); } }
/// <summary> /// Creates a unit tester for the specified assembly. /// </summary> /// <returns>Number of failed unit tests.</returns> public static int Run(string testAssembly) { string tempFolder = Path.Combine(Path.GetTempPath(), "UnitTest" + testAssembly.GetHashCode()); while (Directory.Exists(tempFolder)) { tempFolder += "_"; } Directory.CreateDirectory(tempFolder); try { // Copy over all assemblies. Utils.CopyFiles(Path.GetDirectoryName(testAssembly), tempFolder); // Copy over the nunit dlls. foreach (string dll in (from a in AppDomain.CurrentDomain.GetAssemblies() where a.GetName().Name.StartsWith("nunit.") || a == typeof(UnitTester).Assembly select new Uri(a.CodeBase).LocalPath)) { File.Copy(dll, Path.Combine(tempFolder, Path.GetFileName(dll))); } // Create and run the unit tester using (UnitTester tester = new UnitTester(Path.Combine(tempFolder, Path.GetFileName(testAssembly)))) { Results results = tester.Run(); foreach (var msg in results.Messages) { (msg.IsError ? Console.Error : Console.Out).WriteLine(msg.Text); } if (results.Total > 0) { Console.WriteLine("PASSED: " + results.Passed); if (results.Failed > 0) { Console.Error.WriteLine("FAILED: " + results.Failed); } } return(results.Failed); } } finally { Directory.Delete(tempFolder, true); } }
/// <summary> /// Creates a unit tester for the specified assembly. /// </summary> /// <returns>Number of failed unit tests.</returns> public static int Run(string testAssembly) { string tempFolder = Path.Combine(Path.GetTempPath(), "UnitTest" + testAssembly.GetHashCode()); while (Directory.Exists(tempFolder)) { tempFolder += "_"; } Directory.CreateDirectory(tempFolder); try { // Copy over all assemblies. Utils.CopyFiles(Path.GetDirectoryName(testAssembly), tempFolder); // Copy over the nunit dlls. foreach (string dll in (from a in AppDomain.CurrentDomain.GetAssemblies() where a.GetName().Name.StartsWith("nunit.") || a == typeof(UnitTester).Assembly select new Uri(a.CodeBase).LocalPath)) { File.Copy(dll, Path.Combine(tempFolder, Path.GetFileName(dll))); } // Create and run the unit tester using (UnitTester tester = new UnitTester(Path.Combine(tempFolder, Path.GetFileName(testAssembly)))) { Results results = tester.Run(); foreach (var msg in results.Messages) { (msg.IsError ? Console.Error : Console.Out).WriteLine(msg.Text); } if (results.Total > 0) { Console.WriteLine("PASSED: " + results.Passed); if (results.Failed > 0) { Console.Error.WriteLine("FAILED: " + results.Failed); } } return results.Failed; } } finally { Directory.Delete(tempFolder, true); } }