private void Add(string result, string name, string message, string stackTrace)
        {
            var testid = Guid.NewGuid().ToString();

            name = AppDomain.CurrentDomain.FriendlyName + ":" + name;

            var resultType = new UnitTestResultType
            {
                outcome  = result,
                testName = name,
                testId   = testid
            };

            if (result == "Failed")
            {
                failed           = true;
                resultType.Items = new OutputType[] { new OutputType {
                                                          ErrorInfo = new OutputTypeErrorInfo {
                                                              Message = message, StackTrace = stackTrace
                                                          }
                                                      } };
            }

            results.Add(resultType);

            definitions.Add(new UnitTestType
            {
                name = name,
                id   = testid
            });
        }
        private static IEnumerable <UnitTestResultType> ExtractTestResults(UnitTestResultType unitTestResult)
        {
            // Flatten inner results from MSTest data rows
            var innerResults = unitTestResult.InnerResults?.UnitTestResult;

            if (innerResults is null)
            {
                return(new[] { unitTestResult });
            }
            else
            {
                return(innerResults.AsEnumerable());
            }
        }
        public DataTable GetTestResultData()
        {
            string    fileName;
            DataTable testResultTable = null;

            try
            {
                // Construct DirectoryInfo for the folder path passed in as an argument
                string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
                //baseDirectory = baseDirectory.Substring(0, baseDirectory.IndexOf("bin"));
                DirectoryInfo di          = new DirectoryInfo(baseDirectory);
                ResultTable   resultTable = new ResultTable();
                testResultTable = resultTable.CreateTestResultTable();
                // For each .trx file in the given folder process it
                foreach (FileInfo file in di.GetFiles("*.trx"))
                {
                    fileName = file.Name;
                    // Deserialize TestRunType object from the trx file
                    StreamReader  fileStreamReader = new StreamReader(file.FullName);
                    XmlSerializer xmlSer           = new XmlSerializer(typeof(TestRunType));
                    TestRunType   testRunType      = (TestRunType)xmlSer.Deserialize(fileStreamReader);
                    // Navigate to UnitTestResultType object and update the sheet with test result information
                    foreach (object itob1 in testRunType.Items)
                    {
                        ResultsType resultsType = itob1 as ResultsType;
                        if (resultsType != null)
                        {
                            foreach (object itob2 in resultsType.Items)
                            {
                                UnitTestResultType unitTestResultType = itob2 as UnitTestResultType;
                                if (unitTestResultType != null)
                                {
                                    DataRow row = testResultTable.NewRow();
                                    row[Constant.PROCESSEDFILENAME] = fileName;
                                    row[Constant.TESTID]            = unitTestResultType.testId;
                                    row[Constant.TESTNAME]          = unitTestResultType.testName;
                                    row[Constant.TESTOUTCOME]       = unitTestResultType.outcome;
                                    row[Constant.ERRORMESSAGE]      = ((System.Xml.XmlNode[])(((OutputType)(((TestResultType)(unitTestResultType)).Items[0])).ErrorInfo.Message))[0].Value;
                                    testResultTable.Rows.Add(row);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
            }
            return(testResultTable);
        }
Beispiel #4
0
        private string TryGetMessage(UnitTestResultType unitTestResultType)
        {
            if (unitTestResultType.Items == null || !unitTestResultType.Items.Any())
            {
                return(string.Empty);
            }

            var item    = unitTestResultType.Items.First() as OutputType;
            var message = item?.ErrorInfo?.Message as XmlNode[];

            if (message == null || !message.Any())
            {
                return(string.Empty);
            }

            return(message[0].Value ?? string.Empty);
        }
        private static string ExtractDatesAndStdoutError(UnitTestResultType resultItem)
        {
            if (resultItem == null)
            {
                throw new ArgumentNullException(nameof(resultItem));
            }

            string error = string.Empty, stdout = string.Empty;

            // Some items don't contain either Log or Error
            if (resultItem.Items != null)
            {
                // extract error if available
                try
                {
                    error = (((OutputType)resultItem.Items[0]).ErrorInfo == null
                        ? string.Empty
                        : ((XmlNode[])((OutputType)resultItem.Items[0]).ErrorInfo.Message)[0].Value);
                }
                catch (Exception ex)
                {
                    Log.Error($"Error parsing ErrorInfo from test result: {resultItem.testName}", ex);
                    error = string.Empty;
                }

                // extract StdOut if available
                try
                {
                    stdout = ((XmlNode[])((OutputType)resultItem.Items[0]).StdOut)[0].Value;
                }
                catch (Exception ex)
                {
                    Log.Error($"Error parsing Stdout from test result: {resultItem.testName}", ex);
                    stdout = string.Empty;
                }
            }

            return($"Start:\t{resultItem.startTime}\n" +
                   $"End:\t{resultItem.endTime}\n\n" +
                   $"Error:\n{error}\n\n" +
                   $"Log:\n{stdout}");
        }
        private static object MapTestResult(TestInfo testInfo)
        {
            var testResult = new UnitTestResultType()
            {
                executionId = testInfo.ExecutionId,
                testId      = testInfo.TestId,
                testName    = testInfo.FullName,
                duration    = TimeSpan.FromMilliseconds(testInfo.Duration).ToString(),
                testType    = TestTypeId,
                outcome     = MapStatus(testInfo.Status).ToString(),
                testListId  = TestListId,
                relativeResultsDirectory = testInfo.ExecutionId
            };

            if (testResult.outcome == "NotExecuted")
            {
                var output = new OutputType()
                {
                    StdOut = testInfo.Skip
                };
                testResult.Items = new[] { output };
            }
            else if (testResult.outcome == "Failed")
            {
                var output = new OutputType()
                {
                    ErrorInfo = new OutputTypeErrorInfo()
                    {
                        Message    = testInfo.FailMessage,
                        StackTrace = testInfo.StackTrace
                    }
                };
                testResult.Items = new[] { output };
            }
            return(testResult);
        }
        public override string Transform(TestCaseSummary testFileSummary)
        {
            if (testFileSummary == null)
            {
                throw new ArgumentNullException("testFileSummary");
            }


            testRun = new TestRunType
            {
                id   = Guid.NewGuid().ToString(),
                name = "Chutzpah_JS_UnitTest_" + DateTime.Now.ToString("yy-MMM-dd hh:mm:ss zz")
            };

            var windowsIdentity = System.Security.Principal.WindowsIdentity.GetCurrent();

            if (windowsIdentity != null)
            {
                testRun.runUser = windowsIdentity.Name;
            }

            testRun.Items = new object[]
            {
                new TestRunTypeResultSummary(),
                new ResultsType(),
                new TestDefinitionType(),
                new TestEntriesType1(),
                new TestRunTypeTestLists(),
                new TestRunTypeTimes(),
                new TestSettingsType
                {
                    name      = "Default",
                    id        = Guid.NewGuid().ToString(),
                    Execution = new TestSettingsTypeExecution
                    {
                        TestTypeSpecific = new TestSettingsTypeExecutionTestTypeSpecific {
                        }
                    }
                }
            };
            // Time taken is current time
            testRun.Items.GetInstance <TestRunTypeTimes>(VSTSExtensions.TestRunItemType.Times).creation = DateTime.Now.AddSeconds(-testFileSummary.TimeTaken).ToString("O");
            testRun.Items.GetInstance <TestRunTypeTimes>(VSTSExtensions.TestRunItemType.Times).start    = DateTime.Now.AddSeconds(-testFileSummary.TimeTaken).ToString("O");
            testRun.Items.GetInstance <TestRunTypeTimes>(VSTSExtensions.TestRunItemType.Times).queuing  = DateTime.Now.AddSeconds(-testFileSummary.TimeTaken).ToString("O");


            var testList = new TestListType
            {
                name = "Results Not in a List",
                id   = Guid.NewGuid().ToString()
            };

            var testTypeId = Guid.NewGuid();

            var currentTestCases = testFileSummary.Tests.Select(x => new VSTSTestCase().UpdateWith(x));


            var testsHelper = currentTestCases.ToList();

            testRun.Items.GetInstance <TestRunTypeTimes>(VSTSExtensions.TestRunItemType.Times).finish = DateTime.Now.ToString("O");
            testRun.Items.GetInstance <TestRunTypeResultSummary>(VSTSExtensions.TestRunItemType.ResultSummary).outcome = testsHelper.Count(x => x.Passed) == testsHelper.Count() ? "Passed" : "Failed";

            var counter = new CountersType
            {
                aborted             = 0,
                completed           = 0,
                disconnected        = 0,
                error               = 0,
                passed              = testsHelper.Count(x => x.Passed),
                executed            = testsHelper.Count,
                failed              = testsHelper.Count(x => !x.Passed),
                total               = testsHelper.Count,
                inProgress          = 0,
                pending             = 0,
                warning             = 0,
                notExecuted         = 0,
                notRunnable         = 0,
                passedButRunAborted = 0,
                inconclusive        = 0,
                timeout             = 0
            };

            // total attribute is not written if totalSpecified is false
            counter.totalSpecified = true;

            testRun.Items.GetInstance <TestRunTypeResultSummary>(VSTSExtensions.TestRunItemType.ResultSummary).Items = new object[]
            {
                counter
            };

            testRun.Items.GetInstance <TestDefinitionType>(VSTSExtensions.TestRunItemType.TestDefinition).Items = testsHelper
                                                                                                                  .Select(
                (testCase) => new UnitTestType
            {
                id      = testCase.Id.ToString(),
                name    = testCase.TestName,
                storage = testCase.InputTestFile,
                Items   = new[]
                {
                    new BaseTestTypeExecution
                    {
                        id = testCase.ExecutionId.ToString()
                    }
                },
                TestMethod = new UnitTestTypeTestMethod
                {
                    adapterTypeName = "Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestAdapter",
                    className       = Path.GetFileNameWithoutExtension(testCase.InputTestFile),
                    codeBase        = testCase.InputTestFile,
                    name            = testCase.TestName
                }
            }).ToArray();

            testRun.Items.GetInstance <TestDefinitionType>(VSTSExtensions.TestRunItemType.TestDefinition).ItemsElementName = testsHelper
                                                                                                                             .Select(
                (testCase) => ItemsChoiceType4.UnitTest).ToArray();

            testRun.Items.GetInstance <TestRunTypeTestLists>(VSTSExtensions.TestRunItemType.TestLists).TestList = new[]
            {
                testList,
                // This has to be hard-coded.
                new TestListType
                {
                    name = "All Loaded Results",
                    id   = "19431567-8539-422a-85d7-44ee4e166bda"
                }
            };

            testRun.Items.GetInstance <TestEntriesType1>(VSTSExtensions.TestRunItemType.TestEntries).TestEntry = testsHelper.Select(testCase => new TestEntryType
            {
                testId      = testCase.Id.ToString(),
                executionId = testCase.ExecutionId.ToString(),
                testListId  = testList.id
            }).ToArray();

            testRun.Items.GetInstance <ResultsType>(VSTSExtensions.TestRunItemType.Results).Items = testsHelper.Select((testCase) =>
            {
                var unitTestResultType = new UnitTestResultType
                {
                    executionId = testCase.ExecutionId.ToString(),
                    testId      = testCase.Id.ToString(),
                    testName    = testCase.TestName,

                    computerName = Environment.MachineName,
                    duration     = new TimeSpan(0, 0, testCase.TimeTaken).ToString("c"),
                    // I tried adding this to StandardConsoleRunner, but it demanded too many changes.
                    // Setting start to the creation date.
                    startTime = DateTime.Now.AddSeconds(-testFileSummary.TimeTaken).ToString("O"),
                    // Setting end time to creation date + time taken to run this test.
                    endTime = DateTime.Now.AddSeconds((-testFileSummary.TimeTaken) + testCase.TimeTaken).ToString("O"),
                    // This is for specific test type.
                    testType   = "13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b",
                    outcome    = testCase.Passed ? "Passed" : "Failed",
                    testListId = testList.id,
                };

                if (!testCase.Passed)
                {
                    unitTestResultType.Items = new[]
                    {
                        new OutputType()
                        {
                            ErrorInfo = new OutputTypeErrorInfo {
                                Message = testCase.exception != null ? testCase.exception.ToString(): string.Join(",", testCase.TestResults.Where(x => !x.Passed).Select(x => x.Message))
                            }
                        }
                    };
                }
                return(unitTestResultType);
            }).ToArray();


            testRun.Items.GetInstance <ResultsType>(VSTSExtensions.TestRunItemType.Results).ItemsElementName =
                testsHelper.Select(testCase => ItemsChoiceType3.UnitTestResult).ToArray();

            var ns = new XmlSerializerNamespaces();

            ns.Add("", "http://microsoft.com/schemas/VisualStudio/TeamTest/2010");

            var stringStream = new StringWriter();
            var xs           = new XmlSerializer(typeof(TestRunType));

            xs.Serialize(stringStream, testRun, ns);

            return(stringStream.ToString());
        }
Beispiel #8
0
 public UnitTestResult(UnitTestResultType content, TestResults trx)
 {
     _content = content;
     _trx     = trx;
 }
 public UnitTestResult(UnitTestResultType content)
 {
     _content = content;
 }