public void Publish_NullTestResultFiles()
 {
     SetupMocks();
     var resultCommand = new ResultsCommandExtension();
     resultCommand.Initialize(_hc);
     var command = new Command("results", "publish");
     Assert.Throws<ArgumentException>(() => resultCommand.ProcessCommand(_ec.Object, command));
 }
 public void Publish_InvalidTestRunner()
 {
     SetupMocks();
     var resultCommand = new ResultsCommandExtension();
     resultCommand.Initialize(_hc);
     var command = new Command("results", "publish");
     command.Properties.Add("resultFiles", "ResultFile.txt");
     command.Properties.Add("type", "MyTestRunner");
     Assert.Throws<ArgumentException>(() => resultCommand.ProcessCommand(_ec.Object, command));
 }
 public void Publish_NonExistingTestResultFile()
 {
     _mockResultReader.Setup(x => x.ReadResults(It.IsAny<IExecutionContext>(), It.IsAny<string>(), It.IsAny<TestRunContext>()))
        .Throws(new IOException("Could not find file 'nonexisting.file'"));
     SetupMocks();
     var resultCommand = new ResultsCommandExtension();
     resultCommand.Initialize(_hc);
     var command = new Command("results", "publish");
     command.Properties.Add("resultFiles", "nonexisting.file");
     command.Properties.Add("type", "mockResults");
     resultCommand.ProcessCommand(_ec.Object, command);
     Assert.Equal(0, _errors.Count());
     Assert.Equal(1, _warnings.Count());
     Assert.Equal(StringUtil.Loc("InvalidResultFiles", "Could not find file 'nonexisting.file'"), _warnings[0]);
 }
        public void VerifyStartEndTestRunTimeWhenPublishingToSingleTestRun()
        {
            SetupMocks();
            var resultCommand = new ResultsCommandExtension();
            resultCommand.Initialize(_hc);
            var command = new Command("results", "publish");
            command.Properties.Add("resultFiles", "file1.trx,file2.trx");
            command.Properties.Add("type", "NUnit");
            command.Properties.Add("mergeResults", bool.TrueString);
            var resultsFiles = new List<string> { "file1.trx", "file2.trx" };
            var durationInMs = 10;
            var testRunData = new TestRunData();
            var testCaseResultData = new TestCaseResultData();
            testCaseResultData.DurationInMs = durationInMs.ToString();

            testRunData.Results = new TestCaseResultData[] { testCaseResultData, testCaseResultData };
            testRunData.Attachments = new string[] { "attachment1", "attachment2" };

            _mockTestRunPublisher.Setup(q => q.StartTestRunAsync(It.IsAny<TestRunData>(), It.IsAny<CancellationToken>()))
                .Callback((TestRunData trd, CancellationToken cancellationToken) =>
                {
                    var startedDate = DateTime.Parse(trd.StartDate, null, DateTimeStyles.RoundtripKind);
                    var endedDate = DateTime.Parse(trd.CompleteDate, null, DateTimeStyles.RoundtripKind);
                    Assert.Equal(resultsFiles.Count * testRunData.Results.Length * durationInMs, (endedDate - startedDate).TotalMilliseconds);
                });
            _mockTestRunPublisher.Setup(q => q.AddResultsAsync(It.IsAny<TestRun>(), It.IsAny<TestCaseResultData[]>(), It.IsAny<CancellationToken>()))
                .Callback((TestRun testRun, TestCaseResultData[] tcrd, CancellationToken cancellationToken) =>
                {
                });
            _mockTestRunPublisher.Setup(q => q.ReadResultsFromFile(It.IsAny<TestRunContext>(), It.IsAny<string>()))
                .Returns(testRunData);
            _mockTestRunPublisher.Setup(q => q.EndTestRunAsync(It.IsAny<TestRunData>(), It.IsAny<int>(), It.IsAny<bool>(), It.IsAny<CancellationToken>()))
                .Returns(Task.CompletedTask);

            resultCommand.ProcessCommand(_ec.Object, command);
        }
        public void VerifyResultsAreMergedWhenPublishingToSingleTestRun()
        {
            SetupMocks();
            var resultCommand = new ResultsCommandExtension();
            resultCommand.Initialize(_hc);
            var command = new Command("results", "publish");
            command.Properties.Add("resultFiles", "file1.trx,file2.trx");
            command.Properties.Add("type", "NUnit");
            command.Properties.Add("mergeResults", bool.TrueString);
            var resultsFiles = new List<string> { "file1.trx", "file2.trx" };

            var testRunData = new TestRunData();
            testRunData.Results = new TestCaseResultData[] { new TestCaseResultData(), new TestCaseResultData() };
            testRunData.Attachments = new string[] { "attachment1", "attachment2" };

            _mockTestRunPublisher.Setup(q => q.StartTestRunAsync(It.IsAny<TestRunData>(), It.IsAny<CancellationToken>()))
                .Callback((TestRunData trd, CancellationToken cancellationToken) =>
                {
                    Assert.Equal(resultsFiles.Count * testRunData.Attachments.Length, trd.Attachments.Length);
                });
            _mockTestRunPublisher.Setup(q => q.AddResultsAsync(It.IsAny<TestRun>(), It.IsAny<TestCaseResultData[]>(), It.IsAny<CancellationToken>()))
                .Callback((TestRun testRun, TestCaseResultData[] tcrd, CancellationToken cancellationToken) =>
                {
                    Assert.Equal(resultsFiles.Count * testRunData.Results.Length, tcrd.Length);
                });
            _mockTestRunPublisher.Setup(q => q.ReadResultsFromFile(It.IsAny<TestRunContext>(), It.IsAny<string>()))
                .Returns(testRunData);
            _mockTestRunPublisher.Setup(q => q.EndTestRunAsync(It.IsAny<TestRunData>(), It.IsAny<int>(), It.IsAny<bool>(), It.IsAny<CancellationToken>()))
                .Returns(Task.CompletedTask);

            resultCommand.ProcessCommand(_ec.Object, command);
        }
        public void Publish_DataIsHonoredWhenTestResultsFieldIsNotSpecified()
        {
            SetupMocks();

            var resultCommand = new ResultsCommandExtension();
            resultCommand.Initialize(_hc);
            var command = new Command("results", "publish");
            command.Properties.Add("type", "NUnit");
            command.Data = "testfile1,testfile2";
            resultCommand.ProcessCommand(_ec.Object, command);

            Assert.Equal(0, _errors.Count());
        }
        public void Publish_InvalidNUnitResultFile()
        {
            SetupMocks();

            string jUnitFilePath = "NUnitSampleResults.txt";
            File.WriteAllText(jUnitFilePath, "badformat", Encoding.UTF8);

            var resultCommand = new ResultsCommandExtension();
            resultCommand.Initialize(_hc);
            var command = new Command("results", "publish");
            command.Properties.Add("resultFiles", jUnitFilePath);
            command.Properties.Add("type", "NUnit");

            try
            {
                resultCommand.ProcessCommand(_ec.Object, command);
            }
            finally
            {
                File.Delete(jUnitFilePath);
            }

            Assert.Equal(0, _errors.Count());
            Assert.Equal(1, _warnings.Count());
        }