public static List <TestSuiteMethod> GetTestRunOptions(ApplicationEnvironment applicationEnvironment) { List <TestSuiteMethod> testRunOptions = new List <TestSuiteMethod>(); List <string> testSuiteClasses = AppDomain.CurrentDomain.GetAssemblies().SelectMany(x => x.GetTypes()) .Where(x => typeof(ITestSuite).IsAssignableFrom(x) && !x.IsInterface && !x.IsAbstract) .Select(x => x.FullName).ToList(); int methodOrder = 1; foreach (string testSuiteClassName in testSuiteClasses) { Type testSuiteType = Type.GetType(testSuiteClassName); ConstructorInfo constructor = testSuiteType.GetConstructor(Type.EmptyTypes); object magicClassObject = constructor.Invoke(new object[] { }); List <MethodInfo> methodInfos = testSuiteType.GetMethods(BindingFlags.Public | BindingFlags.Instance) .Where(m => m.DeclaringType != typeof(object)).ToList(); foreach (MethodInfo methodInfo in methodInfos) { TestSuiteMethod testSuiteMethod = new TestSuiteMethod(); testSuiteMethod.Title = $"{testSuiteType.Name} => {methodInfo.Name}"; testSuiteMethod.Order = methodOrder; testSuiteMethod.TaskToRun = delegate { object[] parameters = { applicationEnvironment }; methodInfo.Invoke(magicClassObject, parameters); }; testRunOptions.Add(testSuiteMethod); methodOrder++; } } return(testRunOptions); }
// ReSharper disable once FunctionRecursiveOnAllPaths public static void Main(string[] args) { Console.WriteLine(@"Environment Settings:"); Console.WriteLine($"Application Environment: {applicationEnvironment}"); Console.WriteLine(); Console.WriteLine(@"Choose an option below:"); Console.WriteLine(); // list options List <TestSuiteMethod> testRunOptions = TestRunManager.GetTestRunOptions(applicationEnvironment); foreach (TestSuiteMethod testRunOption in testRunOptions) { Console.WriteLine($"{testRunOption.Order}- {testRunOption.Title} ({testRunOption.Order})"); } // pick an option string option = Console.ReadLine(); int optionInt; int.TryParse(option, out optionInt); TestSuiteMethod actionToRun = testRunOptions.FirstOrDefault(q => q.Order.Equals(optionInt)); if (actionToRun == null) { Console.WriteLine(); Console.WriteLine(@"Invalid option: {0}", option); Console.WriteLine(); } else { Console.WriteLine(); TestUtilities.ConsoleWriteJson(new { Message = $"Executing: {actionToRun.Title}", }); Console.WriteLine(); try { actionToRun.TaskToRun.Invoke(); } catch (Exception e) { Console.WriteLine(e); } } Console.WriteLine(); Console.WriteLine(); Main(null); Console.ReadLine(); }