Example #1
0
        public void LoadCategories()
        {
            Test fixture = TestFixtureBuilder.BuildFrom(typeof(HasCategories));

            Assert.IsNotNull(fixture);
            Assert.AreEqual(2, fixture.Categories.Count);
        }
Example #2
0
        private IList GetFixtures(Assembly assembly, string ns)
        {
            ArrayList fixtures = new ArrayList();

            log.Debug("Examining assembly for test fixtures");

            IList testTypes = GetCandidateFixtureTypes(assembly, ns);

            log.Debug("Found {0} classes to examine", testTypes.Count);
#if NET_2_0
            System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();
            timer.Start();
#endif

            foreach (Type testType in testTypes)
            {
                if (TestFixtureBuilder.CanBuildFrom(testType))
                {
                    fixtures.Add(TestFixtureBuilder.BuildFrom(testType));
                }
            }

#if NET_2_0
            log.Debug("Found {0} fixtures in {1} seconds", fixtures.Count, timer.Elapsed);
#else
            log.Debug("Found {0} fixtures", fixtures.Count);
#endif

            return(fixtures);
        }
        public void RegisterAddins()
        {
            //Figure out the directory from which NUnit is executing
            string moduleName = TestFixtureBuilder.GetAssemblyPath(GetType().Assembly);
            //string moduleName = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
            string nunitDirPath   = Path.GetDirectoryName(moduleName);
            string coreExtensions = Path.Combine(nunitDirPath, "nunit.core.extensions.dll");
            string addinsDirPath  = Path.Combine(nunitDirPath, "addins");

            // Load nunit.core.extensions if available
            if (File.Exists(coreExtensions))
            {
                Register(coreExtensions);
            }

            // Load any extensions in the addins directory
            DirectoryInfo dir = new DirectoryInfo(addinsDirPath);

            if (dir.Exists)
            {
                foreach (FileInfo file in dir.GetFiles("*.dll"))
                {
                    Register(file.FullName);
                }
            }
        }
Example #4
0
        private TestResult RunTestOnFixture(object fixture)
        {
            Test suite = TestFixtureBuilder.BuildFrom(fixture);

            Assert.AreEqual(1, suite.Tests.Count, "Test case count");
            return(suite.Run(NullListener.NULL, TestFilter.Empty));
        }
    public void Should_Add_Range(string test1, string test2, string test3)
    {
        // Given, When
        TestFixture builder = new TestFixtureBuilder().WithTests(new[] { test1, test2, test3 });

        // Then
        Assert.Equal(new[] { test1, test2, test3 }, builder.Tests);
    }
Example #6
0
        private TestResult RunTestOnFixture(object fixture)
        {
            Test suite = TestFixtureBuilder.BuildFrom(fixture);

            Assert.AreEqual(1, suite.Tests.Count, "Test case count");
            Assert.AreEqual("NUnit.Core.Extensions.RepeatedTestCase", suite.Tests[0].GetType().FullName);
            return(suite.Run(NullListener.NULL));
        }
    public void Should_Add_To_List()
    {
        // Given, When
        TestFixture builder = new TestFixtureBuilder().WithTest("testing");

        // Then
        Assert.Equal(new[] { "testing" }, builder.Tests);
    }
    public void Should_Return_Count(int count)
    {
        // Given, When
        TestFixture builder = new TestFixtureBuilder().WithCount(count);

        // Then
        Assert.Equal(count, builder.Count);
    }
Example #9
0
        public void SetUp()
        {
            testSuite   = new TestSuite("MyTestSuite");
            testFixture = TestFixtureBuilder.BuildFrom(typeof(MockTestFixture));
            testSuite.Add(testFixture);

            testCase = TestFinder.Find("MockTest1", testFixture, false);
        }
    public void Should_Add_Key_Value_Pair(string key, string value)
    {
        // Given, When
        TestFixture builder = new TestFixtureBuilder().WithKeyValue(new KeyValuePair <string, string>(key, value));

        // Then
        Assert.Equal(value, builder.Variables[key]);
    }
    public void Should_Return_Name(string name)
    {
        // Given, When
        TestFixture builder = new TestFixtureBuilder().WithName(name);

        // Then
        Assert.Equal(name, builder.Name);
    }
Example #12
0
        public void GoodSignature()
        {
            string methodName = "TestVoid";
            Test   fixture    = TestFixtureBuilder.BuildFrom(typeof(SignatureTestFixture));
            Test   foundTest  = TestFinder.Find(methodName, fixture);

            Assert.IsNotNull(foundTest);
            Assert.AreEqual(RunState.Runnable, foundTest.RunState);
        }
        public void MaxTimeExceeded()
        {
            Test       test   = TestFixtureBuilder.BuildFrom(typeof(MaxTimeFixture));
            TestResult result = test.Run(NullListener.NULL, TestFilter.Empty);

            Assert.AreEqual(ResultState.Failure, result.ResultState);
            result = (TestResult)result.Results[0];
            StringAssert.IsMatch(@"Elapsed time of \d*ms exceeds maximum of 1ms", result.Message);
        }
Example #14
0
        public void CategoryWorksWithRepeatedTest()
        {
            Test suite = TestFixtureBuilder.BuildFrom(typeof(RepeatedTestWithCategory));
            Test test  = suite.Tests[0] as Test;

            Assert.IsNotNull(test.Categories);
            Assert.AreEqual(1, test.Categories.Count);
            Assert.AreEqual("SAMPLE", test.Categories[0]);
        }
        public void ErrorReportHasPriorityOverMaxTime()
        {
            Test       test   = TestFixtureBuilder.BuildFrom(typeof(MaxTimeFixtureWithError));
            TestResult result = test.Run(NullListener.NULL, TestFilter.Empty);

            Assert.AreEqual(ResultState.Failure, result.ResultState);
            result = (TestResult)result.Results[0];
            Assert.AreEqual(ResultState.Error, result.ResultState);
            StringAssert.IsMatch("Exception message", result.Message);
        }
Example #16
0
        private Test BuildSingleFixture(Type testType)
        {
            // The only place we currently allow legacy suites
            if (legacySuiteBuilder.CanBuildFrom(testType))
            {
                return(legacySuiteBuilder.BuildFrom(testType));
            }

            return(TestFixtureBuilder.BuildFrom(testType));
        }
        /// <summary>
        /// Build a TestSuite from type provided.
        /// </summary>
        /// <param name="type">The type of the fixture to be used</param>
        /// <returns>A TestSuite</returns>
        public Test BuildFrom(Type type)
        {
            TestSuite suite = new LegacySuite(type);

            if (suite.RunState == RunState.NotRunnable)
            {
                string reason = null;
                if (!IsValidFixtureType(type, ref reason))
                {
                    suite.RunState = RunState.NotRunnable;
                    suite.Properties.Set(PropertyNames.SkipReason, reason);
                }
            }

            PropertyInfo suiteProperty = GetSuiteProperty(type);
            MethodInfo   method        = suiteProperty.GetGetMethod(true);

            if (method.GetParameters().Length > 0)
            {
                suite.RunState = RunState.NotRunnable;
                suite.Properties.Set(PropertyNames.SkipReason, "Suite property may not be indexed");
            }
            // TODO: Stop checking for name
            else if (method.ReturnType.FullName == "NUnit.Framework.Internal.TestSuite")
            {
                TestSuite s = (TestSuite)suiteProperty.GetValue(null, new Object[0]);
                foreach (Test test in s.Tests)
                {
                    suite.Add(test);
                }
            }
            else if (typeof(IEnumerable).IsAssignableFrom(method.ReturnType))
            {
                foreach (object obj in (IEnumerable)suiteProperty.GetValue(null, new object[0]))
                {
                    Type objType = obj as Type;
                    if (objType != null && TestFixtureBuilder.CanBuildFrom(objType))
                    {
                        suite.Add(TestFixtureBuilder.BuildFrom(objType));
                    }
                    else
                    {
                        suite.Add(TestFixtureBuilder.BuildFrom(obj));
                    }
                }
            }
            else
            {
                suite.RunState = RunState.NotRunnable;
                suite.Properties.Set(PropertyNames.SkipReason, "Suite property must return either TestSuite or IEnumerable");
            }

            return(suite);
        }
        public void SetUp()
        {
            testSuite   = new TestSuite("MyTestSuite");
            testFixture = TestFixtureBuilder.BuildFrom(typeof(MockTestFixture));
            testSuite.Add(testFixture);

            suiteInfo   = new TestInfo(testSuite);
            fixtureInfo = new TestInfo(testFixture);

            testCase     = (NUnit.Core.Test)testFixture.Tests[0];
            testCaseInfo = new TestInfo(testCase);
        }
Example #19
0
        public void RepeatedTestIsBuiltCorrectly()
        {
            Test suite = TestFixtureBuilder.BuildFrom(typeof(RepeatSuccessFixture));

            Assert.IsNotNull(suite, "Unable to build suite");
            Assert.AreEqual(1, suite.Tests.Count);
            Assert.AreEqual("RepeatedTestCase", suite.Tests[0].GetType().Name);
            TestCase repeatedTestCase = suite.Tests[0] as TestCase;

            Assert.IsNotNull(repeatedTestCase, "Test case is not a RepeatedTestCase");
            Assert.AreSame(suite, repeatedTestCase.Parent);
//			Assert.AreEqual( "NUnit.TestData.RepeatedTestFixture.RepeatSuccessFixture", repeatedTestCase.FixtureType.FullName );
        }
    public void Should_Add_Dictionary()
    {
        // Given, When
        var dictionary = new Dictionary <string, string>
        {
            { "check", "one" },
            { "testing", "two" }
        };
        TestFixture builder =
            new TestFixtureBuilder()
            .WithDictionary(dictionary);

        // Then
        Assert.Equal(dictionary, builder.Variables);
    }
Example #21
0
 private Test Build(string assemblyName, Type testType, bool autoSuites)
 {
     // TODO: This is the only situation in which we currently
     // recognize and load legacy suites. We need to determine
     // whether to allow them in more places.
     if (legacySuiteBuilder.CanBuildFrom(testType))
     {
         return(legacySuiteBuilder.BuildFrom(testType));
     }
     else if (TestFixtureBuilder.CanBuildFrom(testType))
     {
         return(BuildTestAssembly(assemblyName,
                                  new Test[] { TestFixtureBuilder.BuildFrom(testType) }, autoSuites));
     }
     return(null);
 }
Example #22
0
        private IList GetFixtures(Assembly assembly, string ns)
        {
            ArrayList fixtures = new ArrayList();

            IList testTypes = GetCandidateFixtureTypes(assembly, ns);

            foreach (Type testType in testTypes)
            {
                if (TestFixtureBuilder.CanBuildFrom(testType))
                {
                    fixtures.Add(TestFixtureBuilder.BuildFrom(testType));
                }
            }

            return(fixtures);
        }
Example #23
0
        public TestResult RunAllUnitTests(Action <string> callback, Assembly testAssembly = null)
        {
            if (testAssembly == null)
            {
                testAssembly = Assembly.GetCallingAssembly();
            }

            CoreExtensions.Host.InitializeService();
            TestExecutionContext.CurrentContext.TestPackage = new TestPackage(string.Format("TestPackage for {0}", testAssembly.GetName().FullName));

            var builder  = new NamespaceTreeBuilder(new TestAssembly(testAssembly, testAssembly.GetName().FullName));
            var fixtures = testAssembly.GetTypes().Where(i => TestFixtureBuilder.CanBuildFrom(i)).Select(i => TestFixtureBuilder.BuildFrom(i)).ToList();

            builder.Add(fixtures);
            return(builder.RootSuite.Run(new ConsoleListener(callback), TestFilter.Empty));
        }
        public TestReport Run(string specificTestName = null, bool skipLocal = false)
        {
            var testSuiteTypes = GetTestSuiteTypesUsingReflection(skipLocal);

            if (testSuiteTypes == null || testSuiteTypes.Length == 0)
            {
                return(new TestReport()
                {
                    Tests = new List <TestReportItem>()
                });
            }

            var testPackage = new TestPackage("HelthMonitor test package");
            var testSuite   = new TestSuite("HelthMonitor test suite");

            TestExecutionContext.CurrentContext.TestPackage = testPackage;

            if (string.IsNullOrEmpty(specificTestName))
            {
                foreach (var testSuiteType in testSuiteTypes)
                {
                    var test = TestFixtureBuilder.BuildFrom(testSuiteType);
                    testSuite.Tests.Add(test);
                }
            }
            else
            {
                Test   specificTest       = null;
                string specificTestMethod = null;

                if (specificTestName.Contains("#"))
                {
                    var tokens = specificTestName.Split('#');

                    specificTestName   = tokens[0];
                    specificTestMethod = tokens[1];
                }

                var specificTestType = testSuiteTypes.FirstOrDefault(x => x.FullName == specificTestName);
                if (specificTestType != null)
                {
                    specificTest = TestFixtureBuilder.BuildFrom(specificTestType);
                }

                if (specificTest != null && !string.IsNullOrEmpty(specificTestMethod))
                {
                    var testsToRemove = new List <Test>();
                    foreach (Test test in specificTest.Tests)
                    {
                        var testMethod = test as TestMethod;
                        if (testMethod != null)
                        {
                            if (testMethod.Method.Name != specificTestMethod)
                            {
                                testsToRemove.Add(test);
                            }
                        }
                    }

                    foreach (var test in testsToRemove)
                    {
                        specificTest.Tests.Remove(test);
                    }
                }

                if (specificTest != null)
                {
                    testSuite.Tests.Add(specificTest);
                }
            }

            using (var listener = new NUnitTraceListener(_appInsightsInstrumentationKey))
            {
                var testResult = testSuite.Run(listener, new NUnitTestRunnerFilter());

                var testReport = GenerateTestReport(testResult);
                return(testReport);
            }
        }
        public void CreateResult()
        {
            Test testFixture = TestFixtureBuilder.BuildFrom(typeof(MockTestFixture));

            result = testFixture.Run(NullListener.NULL, TestFilter.Empty);
        }
Example #26
0
        /// <summary>
        /// Runs all of the tests defined in the specified assembly, specifically not running
        /// any of the tests in the calling type.
        /// </summary>
        /// <param name="assemblyToTest">
        /// The assembly to be tested.
        /// </param>
        /// <param name="callingType">
        /// No tests will be run that are defined in this type.
        /// </param>
        /// <returns>
        /// The result of running the suite of tests.
        /// </returns>
        private TestResult RunTestsInParallelImpl(Assembly assemblyToTest, Type callingType)
        {
            // NUnit requires this initialization step before any tests can be run
            CoreExtensions.Host.InitializeService();

            this.Listener = new ParallelListener();
            this.Filter   = new IgnoreFilter(this.Listener);

            List <Test> concurrentTestFixtures;

            try
            {
                concurrentTestFixtures = assemblyToTest.GetTypes()
                                         .Where(type => callingType != type && TestFixtureBuilder.CanBuildFrom(type))
                                         .Select(TestFixtureBuilder.BuildFrom)
                                         .ToList();
            }
            catch (ReflectionTypeLoadException ex)
            {
                Console.WriteLine("ReflectionTypeLoadException caught...");
                foreach (Exception loaderException in ex.LoaderExceptions)
                {
                    Console.WriteLine("Loader Exception --------------------");
                    Console.WriteLine("Message:{0}", loaderException.Message);
                    if (loaderException is FileNotFoundException)
                    {
                        Console.WriteLine((loaderException as FileNotFoundException).FusionLog);
                    }

                    Console.WriteLine("StackTrace: {0}", loaderException.StackTrace);
                }

                throw;
            }

            var stopwatch = new Stopwatch();

            stopwatch.Start();

            var currentDirectory = Path.GetDirectoryName(assemblyToTest.Location);

            Parallel.ForEach(concurrentTestFixtures, testFixture =>
            {
                Environment.CurrentDirectory = currentDirectory;

                TestContext.Save();
                testFixture.Run(this.Listener, this.Filter);
            });

            stopwatch.Stop();

            // Find out what the failures were
            var testResults = this.AllResults(this.Listener.Results)
                              .Where(x => x.Results == null)
                              .ToList();

            var failuresAndErrors = testResults
                                    .Where(result => (result.IsFailure || result.IsError))
                                    .ToList();

            // Report the errors if there are any
            Console.WriteLine();
            foreach (TestResult failure in failuresAndErrors)
            {
                Console.WriteLine("------------------------------------------------");
                Console.WriteLine(failure.Test.TestName + " failed");
                Console.WriteLine(failure.Message);
                Console.WriteLine(failure.StackTrace);
            }

            Console.WriteLine("=================================================");
            var totalErrors       = testResults.Count(x => x.IsError);
            var totalFailures     = testResults.Count(x => x.IsFailure);
            var totalInconclusive = testResults.Count(x => x.ResultState == ResultState.Inconclusive);
            var totalIgnored      = testResults.Count(x => x.ResultState == ResultState.Ignored);
            var totalSkipped      = testResults.Count(x => x.ResultState == ResultState.Skipped);
            var totalNotRunnable  = testResults.Count(x => x.ResultState == ResultState.NotRunnable);
            var totalNotRun       = totalInconclusive + totalIgnored + totalSkipped + totalNotRunnable;
            var totalRun          = testResults.Count - totalNotRun;

            Console.WriteLine(
                String.Format("Tests run: {0}, Errors: {1}, Failures: {2}, Inconclusive: {3}, Time: {4} seconds",
                              totalRun,
                              totalErrors,
                              totalFailures,
                              totalInconclusive,
                              stopwatch.Elapsed.TotalSeconds));

            Console.WriteLine(String.Format("  Not run: {0}, Invalid: {1}, Ignored: {2}, Skipped: {3}",
                                            totalNotRun,
                                            failuresAndErrors.Count,
                                            totalIgnored,
                                            totalSkipped));
            Console.WriteLine("=================================================");

            // Build up the results for return
            var finalResult = new TestResult(new TestName());

            foreach (var result in this.Listener.Results)
            {
                finalResult.AddResult(result);
            }

            return(finalResult);
        }