Inheritance: ITestListener
示例#1
0
        async void RunTests(CategoryFilter filter = null)
        {
            if (!startButton.Enabled)
            {
                return;
            }
            startButton.Enabled = false;
            var keywords = search.Text;

            Log.Write(null, "Starting tests...");
            var testPlatform   = useTestPlatform.Checked == true ? new TestPlatform() : Platform;
            var runManualTests = includeManualTests.Checked == true;

            try
            {
                await Task.Run(() =>
                {
                    using (Platform.ThreadStart())
                    {
                        try
                        {
                            var listener = new TestListener {
                                Application = Application.Instance
                            };                                                                                                  // use running application for logging

                            var builder  = new DefaultTestAssemblyBuilder();
                            var runner   = new NUnitTestAssemblyRunner(builder);
                            var settings = new Dictionary <string, object>();
                            var result   = new MultipleTestResult();
                            if (filter != null)
                            {
                                filter.SkipCount = 0;
                            }
                            foreach (var assembly in ((TestApplication)TestApplication.Instance).TestAssemblies)
                            {
                                runner.Load(assembly, settings);

                                filter                   = filter ?? new CategoryFilter();
                                filter.Application       = Application.Instance;
                                filter.ExecutingAssembly = assembly;
                                if (!runManualTests)
                                {
                                    filter.ExcludeCategories.Add(UnitTests.TestBase.ManualTestCategory);
                                }
                                if (testPlatform is TestPlatform)
                                {
                                    filter.IncludeCategories.Add(UnitTests.TestBase.TestPlatformCategory);
                                }
                                else
                                {
                                    filter.IncludeCategories.RemoveAll(r => r == UnitTests.TestBase.TestPlatformCategory);
                                }
                                filter.Keyword = keywords;
                                using (testPlatform.Context)
                                {
                                    result.Results.Add(runner.Run(listener, filter));
                                }
                            }
                            var writer = new StringWriter();
                            writer.WriteLine(result.FailCount > 0 ? "FAILED" : "PASSED");
                            writer.WriteLine("\tPass: {0}, Fail: {1}, Skipped: {2}, Inconclusive: {3}", result.PassCount, result.FailCount, result.SkipCount + filter.SkipCount, result.InconclusiveCount);
                            writer.Write("\tDuration: {0}", result.Duration);
                            Application.Instance.Invoke(() => Log.Write(null, writer.ToString()));
                        }
                        catch (Exception ex)
                        {
                            Application.Instance.Invoke(() => Log.Write(null, "Error running tests: {0}", ex));
                        }
                        finally
                        {
                            Application.Instance.Invoke(() => startButton.Enabled = true);
                        }
                    }
                });
            }
            catch (Exception ex)
            {
                Log.Write(null, "Error running tests\n{0}", ex);
            }
        }
示例#2
0
        async void RunTests(CategoryFilter filter = null)
        {
            if (!startButton.Enabled)
            {
                return;
            }
            startButton.Enabled = false;
            Log.Write(null, "Starting tests...");
            var testPlatform = useTestPlatform.Checked == true ? new TestPlatform() : Platform;

            try
            {
                await Task.Run(() =>
                {
                    using (Platform.ThreadStart())
                    {
                        try
                        {
                            var assembly = GetType().GetTypeInfo().Assembly;
                            var runner   = new NUnitLiteTestAssemblyRunner(new NUnitLiteTestAssemblyBuilder());
                            if (!runner.Load(assembly, new Dictionary <string, object>()))
                            {
                                Log.Write(null, "Failed to load test assembly");
                                return;
                            }
                            ITestResult result;
                            var listener = new TestListener {
                                Application = Application.Instance
                            };                                                                                                  // use running application for logging
                            filter             = filter ?? new CategoryFilter();
                            filter.Application = Application.Instance;
                            if (testPlatform is TestPlatform)
                            {
                                filter.ExcludeCategories.Add(UnitTests.TestUtils.NoTestPlatformCategory);
                            }
                            using (testPlatform.Context)
                            {
                                result = runner.Run(listener, filter);
                            }
                            var writer = new StringWriter();
                            writer.WriteLine(result.FailCount > 0 ? "FAILED" : "PASSED");
                            writer.WriteLine("\tPass: {0}, Fail: {1}, Skipped: {2}, Inconclusive: {3}", result.PassCount, result.FailCount, result.SkipCount, result.InconclusiveCount);
                            writer.Write("\tDuration: {0}", result.Duration);
                            Application.Instance.Invoke(() => Log.Write(null, writer.ToString()));
                        }
                        catch (Exception ex)
                        {
                            Application.Instance.Invoke(() => Log.Write(null, "Error running tests: {0}", ex));
                        }
                        finally
                        {
                            Application.Instance.Invoke(() => startButton.Enabled = true);
                        }
                    }
                });
            }
            catch (Exception ex)
            {
                Log.Write(null, "Error running tests\n{0}", ex);
            }
        }