Пример #1
0
        /// <summary>
        /// Makes a TestCase from an NUnit test, adding
        /// navigation data if it can be found.
        /// </summary>
        private TestCase MakeTestCaseFromXmlNode(NUnitEventTestCase testNode)
        {
            string fullyQualifiedName = testNode.FullName;

            if (adapterSettings.UseParentFQNForParametrizedTests)
            {
                var parent = testNode.Parent;
                if (parent != null && 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  = CreateDisplayName(fullyQualifiedName, testNode.Name),
                CodeFilePath = null,
                LineNumber   = 0,
            };

            if (adapterSettings.UseNUnitIdforTestCaseId)
            {
                var id = testNode.Id;
                testCase.Id = EqtHash.GuidFromString(id);
            }
            if (CollectSourceInformation && _navigationDataProvider != null)
            {
                if (!CheckCodeFilePathOverride())
                {
                    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;
                    }
                }
            }
            else
            {
                _ = CheckCodeFilePathOverride();
            }

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

            return(testCase);

            bool CheckCodeFilePathOverride()
            {
                var codeFilePath = testNode.Properties.FirstOrDefault(p => p.Name == "_CodeFilePath");

                if (codeFilePath == null)
                {
                    return(false);
                }
                testCase.CodeFilePath = codeFilePath.Value;
                var lineNumber = testNode.Properties.FirstOrDefault(p => p.Name == "_LineNumber");

                testCase.LineNumber = lineNumber != null?Convert.ToInt32(lineNumber.Value) : 1;

                return(true);
            }
        }