Beispiel #1
0
        public void SetUp()
        {
            fakeTestNode = new NUnitTestCase(FakeTestData.GetTestNode());
            var settings = Substitute.For <IAdapterSettings>();

            settings.CollectSourceInformation.Returns(true);
            testConverter = new TestConverter(new TestLogger(new MessageLoggerStub()), FakeTestData.AssemblyPath, settings);
        }
		protected override void OnCreateTests ()
		{
			if (testInfo.Tests == null)
				return;
			
			foreach (NunitTestInfo test in testInfo.Tests) {
				UnitTest newTest;
				if (test.Tests != null)
					newTest = new NUnitTestSuite (rootSuite, test);
				else
					newTest = new NUnitTestCase (rootSuite, test, ClassName);
				newTest.FixtureTypeName = test.FixtureTypeName;
				newTest.FixtureTypeNamespace = test.FixtureTypeNamespace;
				Tests.Add (newTest);
			}
		}
        private List <NUnitTestCase> ConvertTestRunToNUnitCases(TestRun testRun)
        {
            var testCases = new List <NUnitTestCase>();

            foreach (var unitTest in testRun.TestDefinitions)
            {
                var currentTestCase = new NUnitTestCase
                {
                    FullName  = $"{unitTest.TestMethod.className}.{unitTest.TestMethod.name}",
                    ClassName = unitTest.TestMethod.className,
                    Id        = Guid.Parse(unitTest.id),
                };
                testCases.Add(currentTestCase);
            }

            return(testCases);
        }
        public void SetUp()
        {
            testLog  = new FakeFrameworkHandle();
            settings = Substitute.For <IAdapterSettings>();
            executor = Substitute.For <INUnit3TestExecutor>();
            executor.Settings.Returns(settings);
            settings.CollectSourceInformation.Returns(true);
            using (var testConverter = new TestConverter(new TestLogger(new MessageLoggerStub()), FakeTestData.AssemblyPath, settings))
            {
                fakeTestNode = new NUnitTestCase(FakeTestData.GetTestNode());

                // Ensure that the converted testcase is cached
                testConverter.ConvertTestCase(fakeTestNode);
                Assert.NotNull(testConverter.GetCachedTestCase("123"));

                listener = new NUnitEventListener(testLog, testConverter, executor);
            }
        }
        public static void AddTraitsFromTestNode(this TestCase testCase, NUnitTestCase testNCase,
                                                 IDictionary <string, CachedTestCaseInfo> traitsCache, ITestLogger logger, IAdapterSettings adapterSettings)
        {
            var testNode     = testNCase.Node;
            var ancestor     = testNode.ParentNode;
            var key          = ancestor?.Attributes?["id"]?.Value;
            var categoryList = new CategoryList(testCase, adapterSettings);

            // Reading ancestor properties of a test-case node. And adding to the cache.
            while (ancestor != null && key != null)
            {
                if (traitsCache.ContainsKey(key))
                {
                    categoryList.AddRange(traitsCache[key].Traits.Where(o => o.Name == NunitTestCategoryLabel).Select(prop => prop.Value).ToList());

                    if (traitsCache[key].Explicit)
                    {
                        testCase.SetPropertyValue(CategoryList.NUnitExplicitProperty, true);
                    }

                    var traitsList = traitsCache[key].Traits.Where(o => o.Name != NunitTestCategoryLabel).ToList();
                    if (traitsList.Count > 0)
                    {
                        testCase.Traits.AddRange(traitsList);
                    }
                }
                else
                {
                    categoryList.ProcessTestCaseProperties(ancestor, true, key, traitsCache);
                    // Adding entry to dictionary, so that we will not make SelectNodes call again.
                    if (categoryList.LastNodeListCount == 0 && !traitsCache.ContainsKey(key))
                    {
                        traitsCache[key] = new CachedTestCaseInfo();
                    }
                }
                ancestor = ancestor.ParentNode;
                key      = ancestor?.Attributes?["id"]?.Value;
            }

            // No Need to store test-case properties in cache.
            categoryList.ProcessTestCaseProperties(testNode, false);
            categoryList.UpdateCategoriesToVs();
        }
        /// <summary>
        /// Converts an NUnit test into a TestCase for Visual Studio,
        /// using the best method available according to the exact
        /// type passed and caching results for efficiency.
        /// </summary>
        public TestCase ConvertTestCase(NUnitTestCase testNode)
        {
            if (!testNode.IsTestCase)
            {
                throw new ArgumentException("The argument must be a test case", nameof(testNode));
            }

            // Return cached value if we have one
            string id = testNode.Id;

            if (_vsTestCaseMap.ContainsKey(id))
            {
                return(_vsTestCaseMap[id]);
            }

            // Convert to VS TestCase and cache the result
            var testCase = MakeTestCaseFromXmlNode(testNode);

            _vsTestCaseMap.Add(id, testCase);
            return(testCase);
        }
        public void ThatRunStateIsHandledForNone()
        {
            var sut = new NUnitTestCase(XmlHelper.CreateXmlNode(xml_none));

            Assert.That(sut.RunState, Is.EqualTo(NUnitTestCase.eRunState.NA));
        }
        public void ThatRunStateIsHandledForExplicit()
        {
            var sut = new NUnitTestCase(XmlHelper.CreateXmlNode(xml_explicit));

            Assert.That(sut.RunState, Is.EqualTo(NUnitTestCase.eRunState.Explicit));
        }
        /// <summary>
        /// Makes a TestCase from an NUnit test, adding
        /// navigation data if it can be found.
        /// </summary>
        private TestCase MakeTestCaseFromXmlNode(NUnitTestCase testNode)
        {
            string fullyQualifiedName = testNode.FullName;

            if (adapterSettings.UseParentFQNForParametrizedTests)
            {
                var parent = testNode.Parent();
                if (parent.IsParameterizedMethod)
                {
                    var parameterizedTestFullName = parent.FullName;

                    // VS expected FullyQualifiedName to be the actual class+type name,optionally with parameter types
                    // in parenthesis, but they must fit the pattern of a value returned by object.GetType().
                    // It should _not_ include custom name or param values (just their types).
                    // However, the "fullname" from NUnit's file generation is the custom name of the test, so
                    // this code must convert from one to the other.
                    // Reference: https://github.com/microsoft/vstest-docs/blob/master/RFCs/0017-Managed-TestCase-Properties.md

                    // Using the nUnit-provided "fullname" will cause failures at test execution time due to
                    // the FilterExpressionWrapper not being able to parse the test names passed-in as filters.

                    // To resolve this issue, for parameterized tests (which are the only tests that allow custom names),
                    // the parent node's "fullname" value is used instead. This is the name of the actual test method
                    // and will allow the filtering to work as expected.

                    // Note that this also means you can no longer select a single tests of these to run.
                    // When you do that, all tests within the parent node will be executed

                    if (!string.IsNullOrEmpty(parameterizedTestFullName))
                    {
                        fullyQualifiedName = parameterizedTestFullName;
                    }
                }
            }

            var testCase = new TestCase(
                fullyQualifiedName,
                new Uri(NUnitTestAdapter.ExecutorUri),
                _sourceAssembly)
            {
                DisplayName  = testNode.Name,
                CodeFilePath = null,
                LineNumber   = 0,
            };

            if (adapterSettings.UseNUnitIdforTestCaseId)
            {
                var id = testNode.Id;
                testCase.Id = EqtHash.GuidFromString(id);
            }
            if (CollectSourceInformation && _navigationDataProvider != null)
            {
                var className  = testNode.ClassName;
                var methodName = testNode.MethodName;

                var navData = _navigationDataProvider.GetNavigationData(className, methodName);
                if (navData.IsValid)
                {
                    testCase.CodeFilePath = navData.FilePath;
                    testCase.LineNumber   = navData.LineNumber;
                }
            }

            testCase.AddTraitsFromTestNode(testNode, TraitsCache, _logger, adapterSettings);

            return(testCase);
        }
		void FillTests (NunitTestInfo ti)
		{
			if (ti.Tests == null)
				return;
			foreach (NunitTestInfo test in ti.Tests) {
				UnitTest newTest;
				if (test.Tests != null)
					newTest = new NUnitTestSuite (this, test);
				else
					newTest = new NUnitTestCase (this, test, test.PathName);
				newTest.FixtureTypeName = test.FixtureTypeName;
				newTest.FixtureTypeNamespace = test.FixtureTypeNamespace;
				Tests.Add (newTest);

			}
			oldList = new UnitTest [Tests.Count];
			Tests.CopyTo (oldList, 0);
		}