//Unit testing by category added by Joseph public void Execute(Assembly assembly, String category) { try { IDictionary loadOptions = new Hashtable(); if (!runner.Load(assembly, loadOptions)) { writer.WriteLine("No tests found in assembly {0}", assembly.GetName().Name); return; } //create test filter for CategoryAttribute, based on /include flag ITestFilter filter = TestFilter.Empty; TestFilter includeFilter = new SimpleCategoryExpression(category).Filter; filter = includeFilter; writer.Write(assembly.GetName().Name + ": "); RunTests(filter); } #pragma warning disable 168 catch (NullReferenceException ex) #pragma warning restore 168 { } catch (Exception ex) { writer.WriteLine(ex.Message); } finally { writer.Close(); } }
/// <summary> /// Execute a test run based on the aruments passed /// from Main. /// </summary> /// <param name="args">An array of arguments</param> public void Execute(string[] args) { // NOTE: Execute must be directly called from the // test assembly in order for the mechanism to work. Assembly callingAssembly = Assembly.GetCallingAssembly(); this.commandLineOptions = new CommandLineOptions(); commandLineOptions.Parse(args); if (commandLineOptions.OutFile != null) this.writer = new StreamWriter(commandLineOptions.OutFile); if (!commandLineOptions.NoHeader) WriteHeader(this.writer); if (commandLineOptions.ShowHelp) writer.Write(commandLineOptions.HelpText); else if (commandLineOptions.Error) { writer.WriteLine(commandLineOptions.ErrorMessage); writer.WriteLine(commandLineOptions.HelpText); } else { WriteRuntimeEnvironment(this.writer); if (commandLineOptions.Wait && commandLineOptions.OutFile != null) writer.WriteLine("Ignoring /wait option - only valid for Console"); // We only have one commandline option that has to be passed // to the runner, so we do it here for convenience. var runnerSettings = new Dictionary<string, object>(); Randomizer.InitialSeed = commandLineOptions.InitialSeed; TestFilter filter = commandLineOptions.TestCount > 0 ? new SimpleNameFilter(commandLineOptions.Tests) : TestFilter.Empty; try { foreach (string name in commandLineOptions.Parameters) assemblies.Add(Assembly.Load(name)); if (assemblies.Count == 0) assemblies.Add(callingAssembly); // TODO: For now, ignore all but first assembly Assembly assembly = assemblies[0]; //Randomizer.InitialSeed = commandLineOptions.InitialSeed; if (runner.Load(assembly, runnerSettings) == null) { var assemblyName = AssemblyHelper.GetAssemblyName(assembly); Console.WriteLine("No tests found in assembly {0}", assemblyName.Name); return; } if (commandLineOptions.Explore) ExploreTests(); else { if (commandLineOptions.Include != null && commandLineOptions.Include != string.Empty) { TestFilter includeFilter = new SimpleCategoryExpression(commandLineOptions.Include).Filter; if (filter.IsEmpty) filter = includeFilter; else filter = new AndFilter(filter, includeFilter); } if (commandLineOptions.Exclude != null && commandLineOptions.Exclude != string.Empty) { TestFilter excludeFilter = new NotFilter(new SimpleCategoryExpression(commandLineOptions.Exclude).Filter); if (filter.IsEmpty) filter = excludeFilter; else if (filter is AndFilter) ((AndFilter)filter).Add(excludeFilter); else filter = new AndFilter(filter, excludeFilter); } RunTests(filter); } } catch (FileNotFoundException ex) { writer.WriteLine(ex.Message); } catch (Exception ex) { writer.WriteLine(ex.ToString()); } finally { if (commandLineOptions.OutFile == null) { if (commandLineOptions.Wait) { Console.WriteLine("Press Enter key to continue . . ."); Console.ReadLine(); } } else { writer.Close(); } } } }
/// <summary> /// Execute a test run based on the aruments passed /// from Main. /// </summary> /// <param name="args">An array of arguments</param> public void Execute(string[] args) { // NOTE: Execute must be directly called from the // test assembly in order for the mechanism to work. Assembly callingAssembly = Assembly.GetCallingAssembly(); this.commandLineOptions = new CommandLineOptions(); commandLineOptions.Parse(args); if (commandLineOptions.OutFile != null) this.writer = new StreamWriter(commandLineOptions.OutFile); if (!commandLineOptions.NoHeader) WriteHeader(this.writer); if (commandLineOptions.ShowHelp) writer.Write(commandLineOptions.HelpText); else if (commandLineOptions.Error) { writer.WriteLine(commandLineOptions.ErrorMessage); writer.WriteLine(commandLineOptions.HelpText); } else { WriteRuntimeEnvironment(this.writer); if (commandLineOptions.Wait && commandLineOptions.OutFile != null) writer.WriteLine("Ignoring /wait option - only valid for Console"); #if SILVERLIGHT IDictionary loadOptions = new System.Collections.Generic.Dictionary<string, string>(); #else IDictionary loadOptions = new Hashtable(); #endif //if (options.Load.Count > 0) // loadOptions["LOAD"] = options.Load; //IDictionary runOptions = new Hashtable(); //if (commandLineOptions.TestCount > 0) // runOptions["RUN"] = commandLineOptions.Tests; ITestFilter filter = commandLineOptions.TestCount > 0 ? new SimpleNameFilter(commandLineOptions.Tests) : TestFilter.Empty; try { foreach (string name in commandLineOptions.Parameters) assemblies.Add(Assembly.Load(name)); if (assemblies.Count == 0) assemblies.Add(callingAssembly); // TODO: For now, ignore all but first assembly Assembly assembly = assemblies[0] as Assembly; Randomizer.InitialSeed = commandLineOptions.InitialSeed; if (!runner.Load(assembly, loadOptions)) { AssemblyName assemblyName = AssemblyHelper.GetAssemblyName(assembly); Console.WriteLine("No tests found in assembly {0}", assemblyName.Name); return; } if (commandLineOptions.Explore) ExploreTests(); else { if (commandLineOptions.Include != null && commandLineOptions.Include != string.Empty) { TestFilter includeFilter = new SimpleCategoryExpression(commandLineOptions.Include).Filter; if (filter.IsEmpty) filter = includeFilter; else filter = new AndFilter(filter, includeFilter); } if (commandLineOptions.Exclude != null && commandLineOptions.Exclude != string.Empty) { TestFilter excludeFilter = new NotFilter(new SimpleCategoryExpression(commandLineOptions.Exclude).Filter); if (filter.IsEmpty) filter = excludeFilter; else if (filter is AndFilter) ((AndFilter)filter).Add(excludeFilter); else filter = new AndFilter(filter, excludeFilter); } RunTests(filter); } } catch (FileNotFoundException ex) { writer.WriteLine(ex.Message); } catch (Exception ex) { writer.WriteLine(ex.ToString()); } finally { if (commandLineOptions.OutFile == null) { if (commandLineOptions.Wait) { Console.WriteLine("Press Enter key to continue . . ."); Console.ReadLine(); } } else { writer.Close(); } } } }