public static void RunTests()
        {
            ITestAssemblyRunner testRunner = new NUnitLiteTestAssemblyRunner(new NUnitLiteTestAssemblyBuilder());

            Assembly assembly = Assembly.GetCallingAssembly();

            bool hasTestLoaded = testRunner.Load(assembly: assembly, settings: new Hashtable());

            if (!hasTestLoaded)
            {
                Debug.Log(string.Format("No tests found in assembly {0}", assembly.GetName().Name));
                return;
            }

            ITestResult rootTestResult = testRunner.Run(TestListener.NULL, TestFilter.Empty);

            ResultSummary summary = new ResultSummary(rootTestResult);

            Debug.Log(ToSummryString(summary));

            List <ITestResult> testResultList = EnumerateTestResult(rootTestResult).Where(it => !it.HasChildren).ToList();

            if (summary.FailureCount > 0 || summary.ErrorCount > 0)
            {
                DebugLogErrorResults(testResultList);
            }

            if (summary.NotRunCount > 0)
            {
                DebugLogNotRunResults(testResultList);
            }
        }
Exemple #2
0
        public override void Run(IList <TestAssemblyInfo> testAssemblies)
        {
            if (testAssemblies == null)
            {
                throw new ArgumentNullException(nameof(testAssemblies));
            }

            var builder   = new NUnitLiteTestAssemblyBuilder();
            var runner    = new NUnitLiteTestAssemblyRunner(builder);
            var testSuite = new TestSuite(global::Android.App.Application.Context.PackageName);

            results = new TestSuiteResult(testSuite);

            foreach (TestAssemblyInfo assemblyInfo in testAssemblies)
            {
                if (assemblyInfo == null || assemblyInfo.Assembly == null)
                {
                    continue;
                }

                if (!runner.Load(assemblyInfo.Assembly, builderSettings))
                {
                    OnWarning($"Failed to load tests from assembly '{assemblyInfo.Assembly}");
                    continue;
                }
                if (runner.LoadedTest is NUnitTest tests)
                {
                    testSuite.Add(tests);
                }

                // Messy API. .Run returns ITestResult which is, in reality, an instance of TestResult since that's
                // what WorkItem returns and we need an instance of TestResult to add it to TestSuiteResult. So, cast
                // the return to TestResult and hope for the best.
                ITestResult result = null;
                try {
                    OnAssemblyStart(assemblyInfo.Assembly);
                    result = runner.Run(this, Filter);
                } finally {
                    OnAssemblyFinish(assemblyInfo.Assembly);
                }

                if (result == null)
                {
                    continue;
                }

                var testResult = result as TestResult;
                if (testResult == null)
                {
                    throw new InvalidOperationException($"Unexpected test result type '{result.GetType ()}'");
                }
                results.AddResult(testResult);
            }

            LogFailureSummary();
        }
        public override void Run(IList <TestAssemblyInfo> testAssemblies)
        {
            if (testAssemblies == null)
            {
                throw new ArgumentNullException(nameof(testAssemblies));
            }

            if (AssemblyFilters == null || AssemblyFilters.Count == 0)
            {
                runAssemblyByDefault = true;
            }
            else
            {
                runAssemblyByDefault = AssemblyFilters.Values.Any(v => !v);
            }

            var builder   = new NUnitLiteTestAssemblyBuilder();
            var runner    = new NUnitLiteTestAssemblyRunner(builder, new FinallyDelegate());
            var testSuite = new TestSuite(NSBundle.MainBundle.BundleIdentifier);

            results = new TestSuiteResult(testSuite);

            TotalTests = 0;
            foreach (TestAssemblyInfo assemblyInfo in testAssemblies)
            {
                if (assemblyInfo == null || assemblyInfo.Assembly == null || !ShouldRunAssembly(assemblyInfo))
                {
                    continue;
                }

                if (!runner.Load(assemblyInfo.Assembly, builderSettings))
                {
                    OnWarning($"Failed to load tests from assembly '{assemblyInfo.Assembly}");
                    continue;
                }
                if (runner.LoadedTest is NUnitTest tests)
                {
                    TotalTests += tests.TestCaseCount;
                    testSuite.Add(tests);
                }

                // Messy API. .Run returns ITestResult which is, in reality, an instance of TestResult since that's
                // what WorkItem returns and we need an instance of TestResult to add it to TestSuiteResult. So, cast
                // the return to TestResult and hope for the best.
                ITestResult result = null;
                try {
                    OnAssemblyStart(assemblyInfo.Assembly);
                    result = runner.Run(this, Filter);
                } finally {
                    OnAssemblyFinish(assemblyInfo.Assembly);
                }

                if (result == null)
                {
                    continue;
                }

                var testResult = result as TestResult;
                if (testResult == null)
                {
                    throw new InvalidOperationException($"Unexpected test result type '{result.GetType ()}'");
                }
                results.AddResult(testResult);
            }

            // NUnitLite doesn't report filtered tests at all, but we can calculate here
            FilteredTests = TotalTests - ExecutedTests;
            LogFailureSummary();
        }
Exemple #4
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);
            }
        }