Exemple #1
0
        /// <summary>
        /// Create successed test run
        /// </summary>
        /// <param name="TeamProjectName"></param>
        /// <param name="TestPlanId"></param>
        /// <param name="StaticSuitePath"></param>
        /// <param name="TestCaseIds"></param>
        private static void CreateTestResultCompleted(string TeamProjectName, int TestPlanId, string StaticSuitePath, int[] TestCaseIds)
        {
            TestPlan testPlan = TestManagementClient.GetPlanByIdAsync(TeamProjectName, TestPlanId).Result;

            var testPlanRef = new Microsoft.TeamFoundation.TestManagement.WebApi.ShallowReference(testPlan.Id.ToString(), testPlan.Name, testPlan.Url);

            RunCreateModel runCreate = new RunCreateModel(
                name: "Test run from console - completed",
                plan: testPlanRef,
                startedDate: DateTime.Now.ToString("o"),
                isAutomated: true
                );

            TestRun testRun = TestManagementClient.CreateTestRunAsync(runCreate, TeamProjectName).Result;

            List <TestCaseResult> testResults = new List <TestCaseResult>();

            foreach (int testCaseId in TestCaseIds)
            {
                testResults.Add(PassedTest(TeamProjectName, TestPlanId, StaticSuitePath, testCaseId));
            }

            testResults = TestManagementClient.AddTestResultsToTestRunAsync(testResults.ToArray(), TeamProjectName, testRun.Id).Result;

            RunUpdateModel runUpdateModel = new RunUpdateModel(
                completedDate: DateTime.Now.ToString("o"),
                state: Enum.GetName(typeof(TestRunState), TestRunState.Completed)
                );

            testRun = TestManagementClient.UpdateTestRunAsync(runUpdateModel, TeamProjectName, testRun.Id).Result;

            PrintBasicRunInfo(testRun);
        }
        /// <summary>
        /// Mark the test run as completed
        /// </summary>
        public async Task <TestRun> EndTestRunAsync(TestRunData testRunData, int testRunId, bool publishAttachmentsAsArchive = false, CancellationToken cancellationToken = default(CancellationToken))
        {
            ArgUtil.NotNull(testRunData, nameof(testRunData));
            Trace.Entering();
            RunUpdateModel updateModel = new RunUpdateModel(
                completedDate: testRunData.CompleteDate,
                state: TestRunState.Completed.ToString()
                );
            TestRun testRun = await _testResultsServer.UpdateTestRunAsync(_projectName, testRunId, updateModel, cancellationToken);

            // Uploading run level attachments, only after run is marked completed;
            // so as to make sure that any server jobs that acts on the uploaded data (like CoverAn job does for Coverage files)
            // have a fully published test run results, in case it wants to iterate over results

            if (publishAttachmentsAsArchive)
            {
                await UploadTestRunAttachmentsAsArchiveAsync(testRunId, testRunData.Attachments, cancellationToken);
            }
            else
            {
                await UploadTestRunAttachmentsIndividualAsync(testRunId, testRunData.Attachments, cancellationToken);
            }

            _executionContext.Output(string.Format(CultureInfo.CurrentCulture, "Published Test Run : {0}", testRun.WebAccessUrl));

            return(testRun);
        }
Exemple #3
0
        /// <summary>
        /// Create failed test results
        /// </summary>
        /// <param name="TeamProjectName"></param>
        private static void CreateTestResultFailed(string TeamProjectName)
        {
            RunCreateModel runCreate = new RunCreateModel(
                name: "Test run from console - failed",
                startedDate: DateTime.Now.ToString("o"),
                isAutomated: true
                );

            TestRun testRun = TestManagementClient.CreateTestRunAsync(runCreate, TeamProjectName).Result;

            TestCaseResult testCaseResult = new TestCaseResult();

            testCaseResult.AutomatedTestName = "MyTestSuite.TestName";
            testCaseResult.TestCaseTitle     = "Check my function";
            testCaseResult.StackTrace        = "Add StackTrace here";
            testCaseResult.ErrorMessage      = "Test 'MyTestSuite.TestName' failed";
            testCaseResult.Outcome           = Enum.GetName(typeof(TestOutcome), TestOutcome.Failed);
            testCaseResult.CompletedDate     = DateTime.Now;
            testCaseResult.State             = Enum.GetName(typeof(TestRunState), TestRunState.Completed);

            TestManagementClient.AddTestResultsToTestRunAsync(new TestCaseResult[] { testCaseResult }, TeamProjectName, testRun.Id).Wait();

            RunUpdateModel runUpdateModel = new RunUpdateModel(
                completedDate: DateTime.Now.ToString("o"),
                state: Enum.GetName(typeof(TestRunState), TestRunState.NeedsInvestigation)
                );

            testRun = TestManagementClient.UpdateTestRunAsync(runUpdateModel, TeamProjectName, testRun.Id).Result;

            PrintBasicRunInfo(testRun);
        }
Exemple #4
0
 public async Task <TestRun> UpdateTestRunAsync(
     string projectName,
     int testRunId,
     RunUpdateModel updateModel,
     CancellationToken cancellationToken = default(CancellationToken))
 {
     return(await TestHttpClient.UpdateTestRunAsync(updateModel, projectName, testRunId, cancellationToken));
 }
 private void ResetValues()
 {
     _runId     = 0;
     _projectId = "";
     _attachmentRequestModel  = null;
     _resultCreateModels      = null;
     _resultsLevelAttachments = new Dictionary <int, List <TestAttachmentRequestModel> >();
     _updateProperties        = null;
     _batchSizes = new List <int>();
 }
Exemple #6
0
        public async Task <List <string> > LogResults(List <VstsTestCaseResult> results)
        {
            var orgUri = new Uri(OrganizationUrl);

            VssCredentials credentials = new VssCredentials(new Microsoft.VisualStudio.Services.Common.VssBasicCredential(string.Empty, Personalaccesstoken));
            var            connection  = new VssConnection(orgUri, credentials);

            using (var testClient = connection.GetClient <TestManagementHttpClient>())
            {
                var resultPointIds = results.Where(p => _testCaseToPointMap.Keys.Contains(p.Id)).Select(p => GetPointIdByCaseId(p.Id)).ToArray();

                List <TestCaseResult> testcaseResults = new List <TestCaseResult>();
                foreach (var pointId in resultPointIds)
                {
                    TestCaseResult temp = new TestCaseResult()
                    {
                        State = "Completed"
                    };
                    temp.Id        = GenerateTestResultID();
                    temp.TestPoint = new ShallowReference(pointId.ToString());
                    var current = results.Where(p => p.Id == GetCaseIdByPointId(pointId)).First();
                    temp.Outcome = current.Outcome;
                    if (current.AssociatedBugs != null)
                    {
                        var tempDefects = new List <ShallowReference>();
                        foreach (var defect in current.AssociatedBugs)
                        {
                            tempDefects.Add(new ShallowReference(defect));
                        }
                        temp.AssociatedBugs = tempDefects;
                    }
                    temp.Comment = current.Comment;
                    testcaseResults.Add(temp);
                }

                RunCreateModel run     = new RunCreateModel(name: "Mvt", plan: new ShallowReference(PlanId.ToString()), pointIds: resultPointIds);
                TestRun        testrun = await testClient.CreateTestRunAsync(run, ProjectName);

                var testResults = await testClient.UpdateTestResultsAsync(testcaseResults.ToArray(), ProjectName, testrun.Id);

                RunUpdateModel runmodel      = new RunUpdateModel(state: "Completed", deleteUnexecutedResults: true);  //, deleteUnexecutedResults: true);
                TestRun        testRunResult = await testClient.UpdateTestRunAsync(runmodel, ProjectName, testrun.Id); //, runmodel);

                var loggedCases = await testClient.GetTestResultsAsync(ProjectName, testRunResult.Id);

                return(loggedCases.Select(p => p.TestCase.Id).ToList());
                //TestRunState.Completed;
                //TestRunOutcome.Passed.ToString();
            }
        }
Exemple #7
0
        /// <summary>
        /// Create failed test results for all tests in test suite
        /// </summary>
        /// <param name="TeamProjectName"></param>
        /// <param name="TestPlanId"></param>
        /// <param name="StaticSuitePath"></param>
        private static void CreateTestResultFailed(string TeamProjectName, int TestPlanId, string StaticSuitePath)
        {
            TestPlan testPlan    = TestManagementClient.GetPlanByIdAsync(TeamProjectName, TestPlanId).Result;
            int      testSuiteId = GetSuiteId(TeamProjectName, TestPlanId, StaticSuitePath);

            var testPlanRef = new Microsoft.TeamFoundation.TestManagement.WebApi.ShallowReference(testPlan.Id.ToString(), testPlan.Name, testPlan.Url);

            RunCreateModel runCreate = new RunCreateModel(
                name: "Test run from console - failed",
                plan: testPlanRef,
                startedDate: DateTime.Now.ToString("o"),
                isAutomated: true
                );

            TestRun testRun = TestManagementClient.CreateTestRunAsync(runCreate, TeamProjectName).Result;

            List <TestCaseResult> testResults = new List <TestCaseResult>();

            List <SuiteTestCase> testCases = TestManagementClient.GetTestCasesAsync(TeamProjectName, TestPlanId, testSuiteId).Result; //Get all test cases from suite

            foreach (var testCase in testCases)
            {
                testResults.Add(FailedTest(TeamProjectName, TestPlanId, testSuiteId, testCase.Workitem.Id, testRun.Id));
            }

            testResults = TestManagementClient.AddTestResultsToTestRunAsync(testResults.ToArray(), TeamProjectName, testRun.Id).Result;

            var definedTestResults = TestManagementClient.GetTestResultsAsync(TeamProjectName, testRun.Id).Result; // Get test result

            TestManagementClient.CreateTestResultAttachmentAsync(GetAttachmentModel(@"img\iconfinder_Insect-robot_131435.png"), TeamProjectName, testRun.Id, definedTestResults.ElementAt(0).Id).Wait();


            RunUpdateModel runUpdateModel = new RunUpdateModel(
                errorMessage: "Test failed",
                completedDate: DateTime.Now.ToString("o"),
                state: Enum.GetName(typeof(TestRunState), TestRunState.NeedsInvestigation)
                );

            testRun = TestManagementClient.UpdateTestRunAsync(runUpdateModel, TeamProjectName, testRun.Id).Result;

            TestManagementClient.CreateTestRunAttachmentAsync(GetAttachmentModel(@"img\Screen_Shot_2018-01-16.jpg"), TeamProjectName, testRun.Id).Wait();

            PrintBasicRunInfo(testRun);
        }
Exemple #8
0
        private static void updateTestResultsInAzure(string testPlanId, List <Tuple <int, string> > resultList)
        {
            try{
                VssConnection connection = new VssConnection(new Uri(TFUrl), new VssBasicCredential(string.Empty, UserPAT));

                DateTime utcDate    = DateTime.UtcNow; //getting time for the run report name
                var      culture    = new CultureInfo("en-US");
                string   reportName = "Katalon Automated Tests: " + utcDate.ToString(culture) + " " + utcDate.Kind;

                int[]            excPointIds  = new int[resultList.Count];
                TestCaseResult[] excTestCases = new TestCaseResult[resultList.Count];
                for (int i = 0; i < resultList.Count; i++)
                {
                    excPointIds[i] = resultList[i].Item1;
                    string         extrapolatedOutcome = resultList[i].Item2 == "PASSED" ? "Passed" : "Failed"; //we only care if the test passed or not
                    TestCaseResult caseResult          = new TestCaseResult()
                    {
                        State = "Completed", Outcome = extrapolatedOutcome, Id = 100000 + i
                    };
                    excTestCases[i] = caseResult;
                }

                TestManagementClient = connection.GetClient <TestManagementHttpClient>();
                RunCreateModel run = new RunCreateModel(
                    name: reportName,
                    plan:  new Microsoft.TeamFoundation.TestManagement.WebApi.ShallowReference(testPlanId),
                    pointIds: excPointIds
                    );

                TestRun        testrun       = TestManagementClient.CreateTestRunAsync(run, teamProjectName).Result;
                var            testResults   = TestManagementClient.UpdateTestResultsAsync(excTestCases, teamProjectName, testrun.Id).Result;
                RunUpdateModel runmodel      = new RunUpdateModel(state: "Completed");
                TestRun        testRunResult = TestManagementClient.UpdateTestRunAsync(runmodel, teamProjectName, testrun.Id, runmodel).Result;
            }
            catch (Exception e) { //catch exception with writing test case results, don't make this kill the whole process
                Console.WriteLine(e.Message.ToString());
                Console.WriteLine(e.StackTrace.ToString());
            }
        }
        public TestRunPublisherTests()
        {
            _attachmentFilePath = "attachment.txt";

            File.WriteAllText(_attachmentFilePath, "asdf");
            _testRunContext = new TestRunContext("owner", "platform", "config", 1, "builduri", "releaseuri", "releaseenvuri");

            _reader = new Mock <IResultReader>();
            _reader.Setup(x => x.ReadResults(It.IsAny <IExecutionContext>(), It.IsAny <string>(), It.IsAny <TestRunContext>()))
            .Callback <IExecutionContext, string, TestRunContext>
                ((executionContext, filePath, runContext) =>
            {
                _runContext      = runContext;
                _resultsFilepath = filePath;
            })
            .Returns((IExecutionContext executionContext, string filePath, TestRunContext runContext) =>
            {
                TestRunData trd = new TestRunData(
                    name: "xyz",
                    buildId: runContext.BuildId,
                    completedDate: "",
                    state: "InProgress",
                    isAutomated: true,
                    dueDate: "",
                    type: "",
                    buildFlavor: runContext.Configuration,
                    buildPlatform: runContext.Platform,
                    releaseUri: runContext.ReleaseUri,
                    releaseEnvironmentUri: runContext.ReleaseEnvironmentUri
                    );
                trd.Attachments = new string[] { "attachment.txt" };
                return(trd);
            });

            _testResultServer = new Mock <ITestResultsServer>();
            _testResultServer.Setup(x => x.InitializeServer(It.IsAny <VssConnection>(), It.IsAny <IExecutionContext>()));
            _testResultServer.Setup(x => x.AddTestResultsToTestRunAsync(It.IsAny <TestCaseResult[]>(), It.IsAny <string>(), It.IsAny <int>(), It.IsAny <CancellationToken>()))
            .Callback <TestCaseResult[], string, int, CancellationToken>
                ((currentBatch, projectName, testRunId, cancellationToken) =>
            {
                _batchSizes.Add(currentBatch.Length);
                _resultCreateModels = currentBatch;
            })
            .Returns(() =>
            {
                List <TestCaseResult> resultsList = new List <TestCaseResult>();
                int i = 0;
                int j = 1;
                foreach (TestCaseResult resultCreateModel in _resultCreateModels)
                {
                    List <TestSubResult> SubResults = null;
                    if (resultCreateModel.SubResults != null)
                    {
                        SubResults = new List <TestSubResult>();
                        foreach (var subresultdata in resultCreateModel.SubResults)
                        {
                            var subResult = new TestSubResult();
                            subResult.Id  = j++;
                            SubResults.Add(subResult);
                        }
                    }
                    resultsList.Add(new TestCaseResult()
                    {
                        Id = ++i, SubResults = SubResults
                    });
                }
                return(Task.FromResult(resultsList));
            });

            _testResultServer.Setup(x => x.CreateTestRunAsync(It.IsAny <string>(), It.IsAny <RunCreateModel>(), It.IsAny <CancellationToken>()))
            .Callback <string, RunCreateModel, CancellationToken>
                ((projectName, testRunData, cancellationToken) =>
            {
                _projectId = projectName;
                _testRun   = (TestRunData)testRunData;
            })
            .Returns(Task.FromResult(new TestRun()
            {
                Name = "TestRun", Id = 1
            }));

            _testResultServer.Setup(x => x.UpdateTestRunAsync(It.IsAny <string>(), It.IsAny <int>(), It.IsAny <RunUpdateModel>(), It.IsAny <CancellationToken>()))
            .Callback <string, int, RunUpdateModel, CancellationToken>
                ((projectName, testRunId, updateModel, cancellationToken) =>
            {
                _runId            = testRunId;
                _projectId        = projectName;
                _updateProperties = updateModel;
            })
            .Returns(Task.FromResult(new TestRun()
            {
                Name = "TestRun", Id = 1
            }));

            _testResultServer.Setup(x => x.CreateTestRunAttachmentAsync(
                                        It.IsAny <TestAttachmentRequestModel>(), It.IsAny <string>(), It.IsAny <int>(), It.IsAny <CancellationToken>()))
            .Callback <TestAttachmentRequestModel, string, int, CancellationToken>
                ((reqModel, projectName, testRunId, cancellationToken) =>
            {
                _attachmentRequestModel = reqModel;
                _projectId = projectName;
                _runId     = testRunId;
            })
            .Returns(Task.FromResult(new TestAttachmentReference()));

            _testResultServer.Setup(x => x.CreateTestResultAttachmentAsync(It.IsAny <TestAttachmentRequestModel>(), It.IsAny <string>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <CancellationToken>()))
            .Callback <TestAttachmentRequestModel, string, int, int, CancellationToken>
                ((reqModel, projectName, testRunId, testCaseResultId, cancellationToken) =>
            {
                if (_resultsLevelAttachments.ContainsKey(testCaseResultId))
                {
                    _resultsLevelAttachments[testCaseResultId].Add(reqModel);
                }
                else
                {
                    _resultsLevelAttachments.Add(testCaseResultId, new List <TestAttachmentRequestModel>()
                    {
                        reqModel
                    });
                }
            })
            .Returns(Task.FromResult(new TestAttachmentReference()));
            _testResultServer.Setup(x => x.CreateTestSubResultAttachmentAsync(It.IsAny <TestAttachmentRequestModel>(), It.IsAny <string>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <CancellationToken>()))
            .Callback <TestAttachmentRequestModel, string, int, int, int, CancellationToken>
                ((reqModel, projectName, testRunId, testCaseResultId, testSubResultId, cancellationToken) =>
            {
                if (_subResultsLevelAttachments.ContainsKey(testCaseResultId))
                {
                    if (_subResultsLevelAttachments[testCaseResultId].ContainsKey(testSubResultId))
                    {
                        _subResultsLevelAttachments[testCaseResultId][testSubResultId].Add(reqModel);
                    }
                    else
                    {
                        _subResultsLevelAttachments[testCaseResultId].Add(testSubResultId, new List <TestAttachmentRequestModel>()
                        {
                            reqModel
                        });
                    }
                }
                else
                {
                    Dictionary <int, List <TestAttachmentRequestModel> > subResulAtt = new Dictionary <int, List <TestAttachmentRequestModel> >();
                    subResulAtt.Add(testSubResultId, new List <TestAttachmentRequestModel>()
                    {
                        reqModel
                    });
                    _subResultsLevelAttachments.Add(testCaseResultId, subResulAtt);
                }
            })
            .Returns(Task.FromResult(new TestAttachmentReference()));
        }
        /// <summary>
        /// Mark the test run as completed 
        /// </summary>
        public async Task EndTestRunAsync(TestRunData testRunData, int testRunId, bool publishAttachmentsAsArchive = false, CancellationToken cancellationToken = default(CancellationToken))
        {
            Trace.Entering();
            RunUpdateModel updateModel = new RunUpdateModel(
                completedDate: testRunData.CompleteDate,
                state: TestRunState.Completed.ToString()
                );
            TestRun testRun = await _testResultsServer.UpdateTestRunAsync(_projectName, testRunId, updateModel, cancellationToken);

            // Uploading run level attachments, only after run is marked completed;
            // so as to make sure that any server jobs that acts on the uploaded data (like CoverAn job does for Coverage files)  
            // have a fully published test run results, in case it wants to iterate over results 

            if (publishAttachmentsAsArchive)
            {
                await UploadTestRunAttachmentsAsArchiveAsync(testRunId, testRunData.Attachments, cancellationToken);
            }
            else
            {
                await UploadTestRunAttachmentsIndividualAsync(testRunId, testRunData.Attachments, cancellationToken);
            }

            _executionContext.Output(string.Format(CultureInfo.CurrentCulture, "Published Test Run : {0}", testRun.WebAccessUrl));
        }
 private void ResetValues()
 {
     _runId = 0;
     _projectId = "";
     _attachmentRequestModel = null;
     _resultCreateModels = null;
     _resultsLevelAttachments = new Dictionary<int, List<TestAttachmentRequestModel>>();
     _updateProperties = null;
     _batchSizes = new List<int>();
 }
        public TestRunPublisherTests()
        {
            _attachmentFilePath = "attachment.txt";

            File.WriteAllText(_attachmentFilePath, "asdf");
            _testRunContext = new TestRunContext("owner", "platform", "config", 1, "builduri", "releaseuri", "releaseenvuri");

            _reader = new Mock<IResultReader>();
            _reader.Setup(x => x.ReadResults(It.IsAny<IExecutionContext>(), It.IsAny<string>(), It.IsAny<TestRunContext>()))
                        .Callback<IExecutionContext, string, TestRunContext>
                        ((executionContext, filePath, runContext) =>
                        {
                            _runContext = runContext;
                            _resultsFilepath = filePath;
                        })
                        .Returns((IExecutionContext executionContext, string filePath, TestRunContext runContext) =>
                        {
                            TestRunData trd = new TestRunData(
                                name: "xyz",
                                buildId: runContext.BuildId,
                                completedDate: "",
                                state: "InProgress",
                                isAutomated: true,
                                dueDate: "",
                                type: "",
                                buildFlavor: runContext.Configuration,
                                buildPlatform: runContext.Platform,
                                releaseUri: runContext.ReleaseUri,
                                releaseEnvironmentUri: runContext.ReleaseEnvironmentUri
                            );
                            trd.Attachments = new string[] { "attachment.txt" };
                            return trd;
                        });

            _testResultServer = new Mock<ITestResultsServer>();
            _testResultServer.Setup(x => x.InitializeServer(It.IsAny<Client.VssConnection>()));
            _testResultServer.Setup(x => x.AddTestResultsToTestRunAsync(It.IsAny<TestResultCreateModel[]>(), It.IsAny<string>(), It.IsAny<int>(), It.IsAny<CancellationToken>()))
                        .Callback<TestResultCreateModel[], string, int, CancellationToken>
                        ((currentBatch, projectName, testRunId, cancellationToken) =>
                        {
                            _batchSizes.Add(currentBatch.Length);
                            _resultCreateModels = currentBatch;
                        })
                        .Returns(() =>
                        {
                            List<TestCaseResult> resultsList = new List<TestCaseResult>();
                            int i = 0;
                            foreach (TestResultCreateModel resultCreateModel in _resultCreateModels)
                            {
                                resultsList.Add(new TestCaseResult() { Id = ++i });
                            }
                            return Task.FromResult(resultsList);
                        });

            _testResultServer.Setup(x => x.CreateTestRunAsync(It.IsAny<string>(), It.IsAny<RunCreateModel>(), It.IsAny<CancellationToken>()))
                        .Callback<string, RunCreateModel, CancellationToken>
                        ((projectName, testRunData, cancellationToken) =>
                        {
                            _projectId = projectName;
                            _testRun = (TestRunData)testRunData;
                        })
                        .Returns(Task.FromResult(new TestRun() { Name = "TestRun", Id = 1 }));

            _testResultServer.Setup(x => x.UpdateTestRunAsync(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<RunUpdateModel>(), It.IsAny<CancellationToken>()))
                        .Callback<string, int, RunUpdateModel, CancellationToken>
                        ((projectName, testRunId, updateModel, cancellationToken) =>
                        {
                            _runId = testRunId;
                            _projectId = projectName;
                            _updateProperties = updateModel;
                        })
                        .Returns(Task.FromResult(new TestRun() { Name = "TestRun", Id = 1 }));

            _testResultServer.Setup(x => x.CreateTestRunAttachmentAsync(
                        It.IsAny<TestAttachmentRequestModel>(), It.IsAny<string>(), It.IsAny<int>(), It.IsAny<CancellationToken>()))
                        .Callback<TestAttachmentRequestModel, string, int, CancellationToken>
                        ((reqModel, projectName, testRunId, cancellationToken) =>
                        {
                            _attachmentRequestModel = reqModel;
                            _projectId = projectName;
                            _runId = testRunId;
                        })
                        .Returns(Task.FromResult(new TestAttachmentReference()));

            _testResultServer.Setup(x => x.CreateTestResultAttachmentAsync(It.IsAny<TestAttachmentRequestModel>(), It.IsAny<string>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<CancellationToken>()))
                        .Callback<TestAttachmentRequestModel, string, int, int, CancellationToken>
                        ((reqModel, projectName, testRunId, testCaseResultId, cancellationToken) =>
                        {
                            if (_resultsLevelAttachments.ContainsKey(testCaseResultId))
                            {
                                _resultsLevelAttachments[testCaseResultId].Add(reqModel);
                            }
                            else
                            {
                                _resultsLevelAttachments.Add(testCaseResultId, new List<TestAttachmentRequestModel>() { reqModel });
                            }
                        })
                        .Returns(Task.FromResult(new TestAttachmentReference()));
        }
        /// <inheritdoc />
        public async Task <TestRun> PublishAsync(TestRun testRun)
        {
            using (var timer = new SimpleTimer("PublishTestRun", _logger,
                                               new TelemetryDataWrapper(_telemetry, TelemetryConstants.PipelineTestRunPublisherEventArea, TelemetryConstants.PublishTestRun),
                                               TimeSpan.FromMilliseconds(int.MaxValue)))
            {
                var runCreateModel = new RunCreateModel(name: testRun.TestRunName, buildId: _pipelineConfig.BuildId,
                                                        state: TestRunState.InProgress.ToString(), isAutomated: true, type: RunType.NoConfigRun.ToString());

                runCreateModel.PipelineReference = new PipelineReference()
                {
                    PipelineId     = _pipelineConfig.BuildId,
                    StageReference = new StageReference()
                    {
                        StageName = _pipelineConfig.StageName, Attempt = _pipelineConfig.StageAttempt
                    },
                    PhaseReference = new PhaseReference()
                    {
                        PhaseName = _pipelineConfig.PhaseName, Attempt = _pipelineConfig.PhaseAttempt
                    },
                    JobReference = new JobReference()
                    {
                        JobName = _pipelineConfig.JobName, Attempt = _pipelineConfig.JobAttempt
                    }
                };

                // Create the test run on the server
                var run = await _httpClient.CreateTestRunAsync(runCreateModel, _pipelineConfig.Project);

                _logger.Info($"PipelineTestRunPublisher : PublishAsync : Created test run with id {run.Id}.");
                _telemetry.AddAndAggregate(TelemetryConstants.TestRunIds, new List <int> {
                    run.Id
                },
                                           TelemetryConstants.PipelineTestRunPublisherEventArea);
                _telemetry.AddAndAggregate($"{testRun.ParserUri.Split('/')[0]}RunsCount", 1,
                                           TelemetryConstants.PipelineTestRunPublisherEventArea);

                // Populate test reulsts
                var testResults = new List <TestCaseResult>();

                foreach (var passedTest in testRun.PassedTests)
                {
                    testResults.Add(new TestCaseResult
                    {
                        TestCaseTitle     = passedTest.Name,
                        AutomatedTestName = passedTest.Name,
                        DurationInMs      = passedTest.ExecutionTime.TotalMilliseconds,
                        State             = "Completed",
                        AutomatedTestType = "NoConfig",
                        Outcome           = TestOutcome.Passed.ToString()
                    });
                }

                foreach (var failedTest in testRun.FailedTests)
                {
                    testResults.Add(new TestCaseResult
                    {
                        TestCaseTitle     = failedTest.Name,
                        AutomatedTestName = failedTest.Name,
                        DurationInMs      = failedTest.ExecutionTime.TotalMilliseconds,
                        State             = "Completed",
                        AutomatedTestType = "NoConfig",
                        Outcome           = TestOutcome.Failed.ToString(),
                        StackTrace        = failedTest.StackTrace
                    });
                }

                foreach (var skippedTest in testRun.SkippedTests)
                {
                    testResults.Add(new TestCaseResult
                    {
                        TestCaseTitle     = skippedTest.Name,
                        AutomatedTestName = skippedTest.Name,
                        DurationInMs      = skippedTest.ExecutionTime.TotalMilliseconds,
                        State             = "Completed",
                        AutomatedTestType = "NoConfig",
                        Outcome           = TestOutcome.NotExecuted.ToString()
                    });
                }

                var batchedResults = testResults.Batch(BatchSize);

                foreach (var batch in batchedResults)
                {
                    // Update the run with test results
                    await _httpClient.AddTestResultsToTestRunAsync(batch.ToArray(), _pipelineConfig.Project, run.Id);
                }

                var runUpdateModel = new RunUpdateModel(state: TestRunState.Completed.ToString())
                {
                    RunSummary = new List <RunSummaryModel>()
                };

                runUpdateModel.RunSummary.Add(new RunSummaryModel(resultCount: testRun.TestRunSummary.TotalFailed, testOutcome: TestOutcome.Failed));
                runUpdateModel.RunSummary.Add(new RunSummaryModel(resultCount: testRun.TestRunSummary.TotalPassed, testOutcome: TestOutcome.Passed));
                runUpdateModel.RunSummary.Add(new RunSummaryModel(resultCount: testRun.TestRunSummary.TotalSkipped, testOutcome: TestOutcome.NotExecuted));

                // Complete the run
                await _httpClient.UpdateTestRunAsync(runUpdateModel, _pipelineConfig.Project, run.Id);

                return(new PipelineTestRun(testRun.ParserUri, testRun.RunNamePrefix, testRun.TestRunId, run.Id));
            }
        }
Exemple #14
0
        static void Main(string[] args)
        {
            string collectionUri;

            //set to Uri of the TFS collection
            //if this scipt is running in Build/Release workflow, we will fetch collection Uri from environment variable
            //See https://www.visualstudio.com/en-us/docs/build/define/variables for full list of agent environment variables
            if (Environment.GetEnvironmentVariable("TF_BUILD") == "True")
            {
                collectionUri = Environment.GetEnvironmentVariable("SYSTEM_TEAMFOUNDATIONCOLLECTIONURI");
                Console.WriteLine("Fetched Collection (or VSTS account) from environment variable SYSTEM_TEAMFOUNDATIONCOLLECTIONURI: {0}", collectionUri);
            }
            else   // set it to TFS instance of your choice
            {
                //collectionUri = "http://buildmachine1:8080/tfs/TestDefault";
                collectionUri = "https://manojbableshwar.visualstudio.com";
                Console.WriteLine("Using Collection (or VSTS account): {0}", collectionUri);
            }

            //authentication..
            VssConnection connection = new VssConnection(new Uri(collectionUri), new VssCredentials());

            //set the team project name in which the test results must be published...
            // get team project name from the agent environment variables if the script is running in Build workflow..
            string teamProject;

            if (Environment.GetEnvironmentVariable("TF_BUILD") == "True")
            {
                teamProject = Environment.GetEnvironmentVariable("SYSTEM_TEAMPROJECT");
                Console.WriteLine("Fetched team project from environment variable SYSTEM_TEAMPROJECT: {0}", teamProject);
            }
            else //else set it the team project of your choice...
            {
                teamProject = "PartsUnlimited";
                Console.WriteLine("Using team project: {0}", teamProject);
            }



            //Client to use test run and test result APIs...
            TestManagementHttpClient client = connection.GetClient <TestManagementHttpClient>();

            //Test run model to initialize test run parameters..
            //For automated test runs, isAutomated must be set.. Else manual test run will be created..

            //<<Q: do we want to mark run in progress here?>>
            RunCreateModel TestRunModel = new RunCreateModel(name: "Sample test run from CSV file", isAutomated: true,
                                                             startedDate: DateTime.Now.ToString(), comment: "create run comment");

            //Since we are doing a Asycn call, .Result will wait for the call to complete...
            TestRun testRun = client.CreateTestRunAsync(teamProject, TestRunModel).Result;

            Console.WriteLine("Step 1: test run created -> {0}: {1}; Run url: {2} ", testRun.Id, testRun.Name, testRun.WebAccessUrl);

            string resultsFilePath;

            if (args.Length == 0)
            {
                resultsFilePath = "Results.csv";
            }
            else
            {
                resultsFilePath = args[0];
            }

            //List to hold results from parsed from CSV file...
            List <TestResultCreateModel> testResultsFromCsv = new List <TestResultCreateModel>();

            var reader = new StreamReader(File.OpenRead(resultsFilePath));

            while (!reader.EndOfStream)
            {
                var line   = reader.ReadLine();
                var values = line.Split(',');
                Console.WriteLine("Publishing test {0}", values[0]);

                //Assign values from each line in CSV to result model...
                TestResultCreateModel testResultModel = new TestResultCreateModel();
                testResultModel.TestCaseTitle = testResultModel.AutomatedTestName = values[0];
                testResultModel.Outcome       = values[1];
                //Setting state to completed since we are only publishing results.
                //In advanced scenarios, you can choose to create a test result,
                // move it into in progress state while test test acutally runs and finally update the outcome with state as completed
                testResultModel.State         = "Completed";
                testResultModel.ErrorMessage  = values[2];
                testResultModel.StackTrace    = values[3];
                testResultModel.StartedDate   = values[4];
                testResultModel.CompletedDate = values[5];

                //Add the result ot the results list...
                testResultsFromCsv.Add(testResultModel);
            }

            //Publish the results...
            List <TestCaseResult> resultObj = client.AddTestResultsToTestRunAsync(testResultsFromCsv.ToArray(), teamProject, testRun.Id).Result;

            Console.WriteLine("Step 2: test results published...");

            //Mark the run complete...
            RunUpdateModel testRunUpdate   = new RunUpdateModel(completedDate: DateTime.Now.ToString(), state: "Completed");
            TestRun        RunUpdateResult = client.UpdateTestRunAsync(teamProject, testRun.Id, testRunUpdate).Result;

            Console.WriteLine("Step 3: Test run completed: {0} ", RunUpdateResult.WebAccessUrl);
        }
Exemple #15
0
        /// <inheritdoc />
        public async Task PublishAsync(TestRun testRun)
        {
            var runUri = testRun.ParserUri.Split("/");
            var r      = new RunCreateModel(name: $"{runUri[0]} test run {testRun.TestRunId} - automatically inferred results", buildId: _pipelineConfig.BuildId,
                                            state: TestRunState.InProgress.ToString(), isAutomated: true, type: RunType.NoConfigRun.ToString());
            var run = await _httpClient.CreateTestRunAsync(r, _pipelineConfig.Project);

            var testResults = new List <TestCaseResult>();

            foreach (var passedTest in testRun.PassedTests)
            {
                testResults.Add(new TestCaseResult
                {
                    TestCaseTitle     = passedTest.Name,
                    AutomatedTestName = passedTest.Name,
                    DurationInMs      = passedTest.ExecutionTime.TotalMilliseconds,
                    State             = "Completed",
                    AutomatedTestType = "NoConfig",
                    Outcome           = TestOutcome.Passed.ToString()
                });
            }

            foreach (var failedTest in testRun.FailedTests)
            {
                testResults.Add(new TestCaseResult
                {
                    TestCaseTitle     = failedTest.Name,
                    AutomatedTestName = failedTest.Name,
                    DurationInMs      = failedTest.ExecutionTime.TotalMilliseconds,
                    State             = "Completed",
                    AutomatedTestType = "NoConfig",
                    Outcome           = TestOutcome.Failed.ToString(),
                    StackTrace        = failedTest.StackTrace
                });
            }

            foreach (var skippedTest in testRun.SkippedTests)
            {
                testResults.Add(new TestCaseResult
                {
                    TestCaseTitle     = skippedTest.Name,
                    AutomatedTestName = skippedTest.Name,
                    DurationInMs      = skippedTest.ExecutionTime.TotalMilliseconds,
                    State             = "Completed",
                    AutomatedTestType = "NoConfig",
                    Outcome           = TestOutcome.NotExecuted.ToString()
                });
            }

            await _httpClient.AddTestResultsToTestRunAsync(testResults.ToArray(), _pipelineConfig.Project, run.Id);

            //  var runUpdateModel = new RunUpdateModel(state: TestRunState.Completed.ToString());

            var runUpdateModel = new RunUpdateModel(state: TestRunState.Completed.ToString())
            {
                RunSummary = new List <RunSummaryModel>()
            };

            runUpdateModel.RunSummary.Add(new RunSummaryModel(resultCount: testRun.TestRunSummary.TotalFailed, testOutcome: TestOutcome.Failed));
            runUpdateModel.RunSummary.Add(new RunSummaryModel(resultCount: testRun.TestRunSummary.TotalPassed, testOutcome: TestOutcome.Passed));
            runUpdateModel.RunSummary.Add(new RunSummaryModel(resultCount: testRun.TestRunSummary.TotalSkipped, testOutcome: TestOutcome.NotExecuted));


            await _httpClient.UpdateTestRunAsync(runUpdateModel, _pipelineConfig.Project, run.Id);
        }
        static void Main(string[] args)
        {
            string collectionUri;

            //set to Uri of the TFS collection
            //if this code is running in Build/Release workflow, we will fetch collection Uri from environment variable
            //See https://www.visualstudio.com/en-us/docs/build/define/variables for full list of agent environment variables
            if (Environment.GetEnvironmentVariable("TF_BUILD") == "True")
            {
                collectionUri = Environment.GetEnvironmentVariable("SYSTEM_TEAMFOUNDATIONCOLLECTIONURI");
                Console.WriteLine("Fetched Collection (or VSTS account) from environment variable SYSTEM_TEAMFOUNDATIONCOLLECTIONURI: {0}", collectionUri);
            }
            else   // set it to TFS instance of your choice
            {
                collectionUri = "http://buildmachine1:8080/tfs/TestDefault";
                Console.WriteLine("Using Collection (or VSTS account): {0}", collectionUri);
            }

            //authentication..
            VssConnection connection = new VssConnection(new Uri(collectionUri), new VssCredentials());

            //set the team project name in which the test results must be published...
            // get team project name from the agent environment variables if the script is running in Build workflow..
            string teamProject;

            if (Environment.GetEnvironmentVariable("TF_BUILD") == "True")
            {
                teamProject = Environment.GetEnvironmentVariable("SYSTEM_TEAMPROJECT");
                Console.WriteLine("Fetched team project from environment variable SYSTEM_TEAMPROJECT: {0}", teamProject);
            }
            else //else set it the team project of your choice...
            {
                teamProject = "DefaultAgileGitProject";
                Console.WriteLine("Using team project: {0}", teamProject);
            }

            // get the build number to publish results against...

            string buildNumber, buildUri; int buildId;

            // if this code is running in build workflow, BUILD_BUILDNUMBER and BUILD_BUILDID environment variables have the build info...
            if (Environment.GetEnvironmentVariable("TF_BUILD") == "True")
            {
                //build number is human readable format of the build name, you can confiure it's format in build options..
                buildNumber = Environment.GetEnvironmentVariable("BUILD_BUILDNUMBER");
                Console.WriteLine("Fetched build number from environment variable BUILD_BUILDNUMBER: {0}", buildNumber);

                //build id is the id associated with the build number, which we will use to associate the test run with...
                buildId = Convert.ToInt32(Environment.GetEnvironmentVariable("BUILD_BUILDID"));
                Console.WriteLine("Fetched build id from environment variable BUILD_BUILDID: {0}", buildId);

                //build uri is a more elaborate form of build id, in the vstfs:///Build/Build/<id> format...
                //We will use this for querying test runs against this build...
                buildUri = Environment.GetEnvironmentVariable("BUILD_BUILDURI");
                Console.WriteLine("Fetched build uri from environment variable BUILD_BUILDURI: {0}", buildUri);
            }
            else //if the code is running in standalone mode, you'll have to use Build APIs to fetch the build number...
            //see https://www.visualstudio.com/en-us/docs/integrate/api/build/overview for build APIs...
            {
                buildNumber = "20161124.2";
                buildId     = 40;
                buildUri    = "vstfs:///Build/Build/40";
                Console.WriteLine("Using build number: {0}; build id: {1}; build uri: {2}", buildNumber, buildId, buildUri);
            }

            //Client to use test run and test result APIs...
            TestManagementHttpClient client = connection.GetClient <TestManagementHttpClient>();

            //Test run model to initialize test run parameters..
            //For automated test runs, isAutomated must be set.. Else manual test run will be created..

            //<<Q: do we want to mark run in progress here?>>
            RunCreateModel TestRunModel = new RunCreateModel(name: "Sample test run from CSV file against buildNumber: " + buildNumber, isAutomated: true,
                                                             startedDate: DateTime.Now.ToString(), buildId: buildId);

            //Since we are doing a Asycn call, .Result will wait for the call to complete...
            TestRun testRun = client.CreateTestRunAsync(teamProject, TestRunModel).Result;

            Console.WriteLine("Step 1: test run created -> {0}: {1}; Run url: {2} ", testRun.Id, testRun.Name, testRun.WebAccessUrl);

            string resultsFilePath;

            if (args.Length == 0)
            {
                resultsFilePath = "Results.csv";
            }
            else
            {
                resultsFilePath = args[0];
            }

            //List to hold results from parsed from CSV file...
            List <TestResultCreateModel> testResultsFromCsv = new List <TestResultCreateModel>();

            var reader = new StreamReader(File.OpenRead(resultsFilePath));

            while (!reader.EndOfStream)
            {
                var line   = reader.ReadLine();
                var values = line.Split(',');
                Console.WriteLine("Publishing test {0}", values[0]);

                //Assign values from each line in CSV to result model...
                TestResultCreateModel testResultModel = new TestResultCreateModel();
                testResultModel.TestCaseTitle = testResultModel.AutomatedTestName = values[0];
                testResultModel.Outcome       = values[1];
                //Setting state to completed since we are only publishing results.
                //In advanced scenarios, you can choose to create a test result,
                // move it into in progress state while test test acutally runs and finally update the outcome with state as completed
                testResultModel.State         = "Completed";
                testResultModel.ErrorMessage  = values[2];
                testResultModel.StackTrace    = values[3];
                testResultModel.StartedDate   = values[4];
                testResultModel.CompletedDate = values[5];

                //Add the result ot the results list...
                testResultsFromCsv.Add(testResultModel);
            }

            //Publish the results...
            List <TestCaseResult> publishedResults = client.AddTestResultsToTestRunAsync(testResultsFromCsv.ToArray(), teamProject, testRun.Id).Result;

            Console.WriteLine("Step 2: test results published...");

            //Mark the run complete...
            RunUpdateModel testRunUpdate   = new RunUpdateModel(completedDate: DateTime.Now.ToString(), state: "Completed");
            TestRun        RunUpdateResult = client.UpdateTestRunAsync(teamProject, testRun.Id, testRunUpdate).Result;

            Console.WriteLine("Step 3: Test run completed: {0}", RunUpdateResult.WebAccessUrl);
        }
        private static async Task CreateBugFromPenTestAsync(string collectionUri, string teamProjectName, string team, string releaseUri, string releaseEnvironmentUri, string filePath, bool failOnHigh, string personalAccessToken)
        {
            var connection = GetConnection(collectionUri, personalAccessToken);

            var testManagementClient = connection.GetClient <TestManagementHttpClient>();
            var projectClient        = connection.GetClient <ProjectHttpClient>();
            var witClient            = connection.GetClient <WorkItemTrackingHttpClient>();
            var workClient           = connection.GetClient <WorkHttpClient>();

            var queryModel    = new QueryModel(query: "SELECT * FROM TestRun WHERE ReleaseUri = '" + releaseUri + "' and ReleaseEnvironmentUri in ('" + releaseEnvironmentUri + "')");
            var testRunResult = await testManagementClient.GetTestRunsByQueryAsync(queryModel, teamProjectName);

            var project = await projectClient.GetProject(teamProjectName);

            string projectId = project.Id.ToString();

            var teamContext  = new TeamContext(teamProjectName, team);
            var teamSettings = await workClient.GetTeamSettingsAsync(teamContext);

            var teamFieldValues = await workClient.GetTeamFieldValuesAsync(teamContext);

            var teamIteration = await workClient.GetTeamIterationAsync(teamContext, teamSettings.BacklogIteration.Id);

            var teamIterations = await workClient.GetTeamIterationsAsync(teamContext, "current");

            string areaPath      = teamFieldValues.DefaultValue;
            string iterationPath = "";

            if (teamIterations.Count > 0)
            {
                iterationPath = teamIterations[0].Path;
            }
            else
            {
                iterationPath = teamIteration.Path;
            }

            TestRun testRun;

            if (testRunResult == null || testRunResult.Count == 0)
            {
                var runCreateModel = new RunCreateModel("OWASP ZAP Security Tests", isAutomated: true, releaseUri: releaseUri, releaseEnvironmentUri: releaseEnvironmentUri);
                testRun = await testManagementClient.CreateTestRunAsync(projectId, runCreateModel);
            }
            else
            {
                testRun = testRunResult.FirstOrDefault();
            }

            var    targetUrl = System.Environment.GetEnvironmentVariable("targeturl");
            var    report    = GetReport(filePath, targetUrl);
            string title     = String.Empty;

            bool testsPassed = true;
            bool highFailure = false;
            var  results     = new List <TestResultCreateModel>();

            if (report != null && report.Issues != null && report.Issues.Count() > 0)
            {
                foreach (Issue issue in report.Issues)
                {
                    if (issue.RiskDescription.Contains("Information"))
                    {
                        continue;
                    }
                    testsPassed = false;
                    //create bug
                    if (issue.RiskDescription.Contains("High"))
                    {
                        highFailure = true;
                    }
                    title = issue.IssueDescription;

                    await CreateBugAsync(collectionUri, teamProjectName, team, title, areaPath, iterationPath, personalAccessToken);

                    results.Add(new TestResultCreateModel
                    {
                        AutomatedTestName = title,
                        Outcome           = testsPassed ? "Passed" : "Failed",
                        TestCaseTitle     = title,
                        State             = "Completed"
                    });
                }
                var testResults = await testManagementClient.AddTestResultsToTestRunAsync(results.ToArray(), teamProjectName, testRun.Id);
            }

            // TODO CloseFixedBugs()

            var updateProperties = new RunUpdateModel(state: "Completed", completedDate: DateTime.Today.ToShortDateString());
            var updateResults    = await testManagementClient.UpdateTestRunAsync(projectId, testRun.Id, updateProperties);

            if (highFailure && failOnHigh)
            {
                Console.WriteLine("High Issue Found. Deployment Failed.");
                throw new Exception("High Issue Found.  Deployment Failed.");
            }
        }
Exemple #18
0
        static void Main(string[] args)
        {
            string collectionUri;

            //set to Uri of the TFS collection
            //if this code is running in Build/Release workflow, we will fetch collection Uri from environment variable
            //See https://www.visualstudio.com/en-us/docs/build/define/variables for full list of agent environment variables
            if (Environment.GetEnvironmentVariable("TF_BUILD") == "True")
            {
                collectionUri = Environment.GetEnvironmentVariable("SYSTEM_TEAMFOUNDATIONCOLLECTIONURI");
                Console.WriteLine("Fetched Collection (or VSTS account) from environment variable SYSTEM_TEAMFOUNDATIONCOLLECTIONURI: {0}", collectionUri);
            }
            else // set it to TFS instance of your choice
            {
                collectionUri = "http://buildmachine1:8080/tfs/DefaultCollection";
                Console.WriteLine("Using Collection (or VSTS account): {0}", collectionUri);
            }

            //authentication..
            VssConnection connection = new VssConnection(new Uri(collectionUri), new VssCredentials());

            //set the team project name in which the test results must be published...
            // get team project name from the agent environment variables if the script is running in Build/Release workflow..
            string teamProject;

            if (Environment.GetEnvironmentVariable("TF_BUILD") == "True")
            {
                teamProject = Environment.GetEnvironmentVariable("SYSTEM_TEAMPROJECT");
                Console.WriteLine("Fetched team project from environment variable SYSTEM_TEAMPROJECT: {0}", teamProject);
            }
            else //else set it the team project of your choice...
            {
                teamProject = "FabrikamFiber";
                Console.WriteLine("Using team project: {0}", teamProject);
            }

            // get the build number to publish results against...

            string buildNumber = null, buildUri = null, releaseUri = null, releaseEnvironmentUri = null; int buildId;

            // if this code is running in build/release workflow, we will use agent environment variables for fetch build/release Uris to associate information
            if (Environment.GetEnvironmentVariable("TF_BUILD") == "True")
            {
                //If RELEASE_RELEASEURI variable is set, then this code is running in the Release workflow, so we fetch release details
                if (Environment.GetEnvironmentVariable("RELEASE_RELEASEURI") != "")
                {
                    releaseUri = Environment.GetEnvironmentVariable("RELEASE_RELEASEURI");
                    Console.WriteLine("Fetched release uri from environment variable RELEASE_RELEASEURI: {0}", releaseUri);

                    releaseEnvironmentUri = Environment.GetEnvironmentVariable("RELEASE_ENVIRONMENTURI");
                    Console.WriteLine("Fetched release environemnt uri from environment variable RELEASE_ENVIRONMENTURI: {0}", releaseEnvironmentUri);
                }
                //note that the build used to deploy and test a Release is an Aritfact.
                //If you have multiple builds or are using external artifacts like Jenkins, make sure you use Aritfact variables to find the build information
                //See https://www.visualstudio.com/en-us/docs/release/author-release-definition/understanding-tasks#predefvariables for pre-defined variables available in release
                //See https://www.visualstudio.com/en-us/docs/release/author-release-definition/understanding-artifacts#variables for artifact variables documentation
                //For example, you'll have to use RELEASE_ARTIFACTS_<aftifactname>_BUILDID to find the build number.
                //Here we are assuming a simple setup, where we are working with Team Build and using Build variables instead...

                //build number is human readable format of the build name, you can confiure it's format in build options..
                buildNumber = Environment.GetEnvironmentVariable("BUILD_BUILDNUMBER");
                Console.WriteLine("Fetched build number from environment variable BUILD_BUILDNUMBER: {0}", buildNumber);

                //build id is the id associated with the build number, which we will use to associate the test run with...
                buildId = Convert.ToInt32(Environment.GetEnvironmentVariable("BUILD_BUILDID"));
                Console.WriteLine("Fetched build id from environment variable BUILD_BUILDID: {0}", buildId);

                //build uri is a more elaborate form of build id, in the vstfs:///Build/Build/<id> format...
                //We will use this for querying test runs against this build...
                buildUri = Environment.GetEnvironmentVariable("BUILD_BUILDURI");
                Console.WriteLine("Fetched build uri from environment variable BUILD_BUILDURI: {0}", buildUri);
            }
            else //if the code is running in standalone mode, you'll have to use Build and Release APIs to fetch the build and release information...
            //see https://www.visualstudio.com/en-us/docs/integrate/api/build/overview for build APIs...
            //and https://www.visualstudio.com/en-us/docs/release/overview for release APIs...
            {
                buildNumber           = "20161124.2";
                buildId               = 3;
                buildUri              = "vstfs:///Build/Build/40";
                releaseUri            = "vstfs:///ReleaseManagement/Release/2";
                releaseEnvironmentUri = "vstfs:///ReleaseManagement/Environment/2";
                Console.WriteLine("Using build number: {0}; build id: {1}; build uri: {2}; release uri: {3}; release environment uri: {4}", buildNumber, buildId, buildUri, releaseUri, releaseEnvironmentUri);
            }

            //Client to use test run and test result APIs...
            TestManagementHttpClient client = connection.GetClient <TestManagementHttpClient>();

            //Test run model to initialize test run parameters..
            //For automated test runs, isAutomated must be set.. Else manual test run will be created..

            //<<Q: do we want to mark run in progress here?>>
            RunCreateModel TestRunModel     = new RunCreateModel(name: "Sample test run from CSV file against buildNumber: " + buildNumber, isAutomated: true,
                                                                 startedDate: DateTime.Now.ToString(), buildId: buildId, releaseUri: releaseUri, releaseEnvironmentUri: releaseEnvironmentUri);

            //Since we are doing a Asycn call, .Result will wait for the call to complete...
            TestRun testRun = client.CreateTestRunAsync(teamProject, TestRunModel).Result;

            Console.WriteLine("Step 1: test run created -> {0}: {1}; Run url: {2} ", testRun.Id, testRun.Name, testRun.WebAccessUrl);


            string resultsFilePath;

            if (args.Length == 0)
            {
                resultsFilePath = "Results.csv";
            }
            else
            {
                resultsFilePath = args[0];
            }


            TestAttachmentRequestModel fileAttachment = GetAttachmentRequestModel(resultsFilePath);
            TestAttachmentReference    testAttachment = client.CreateTestRunAttachmentAsync(fileAttachment, teamProject, testRun.Id).Result;

            Console.WriteLine("Created test run attachment -> Id: {0}, Url: {2}", testAttachment.Id, testAttachment.Url);

            //List to hold results from parsed from CSV file...
            List <TestResultCreateModel> testResultsFromCsv = new List <TestResultCreateModel>();

            //TestMethodName, Outcome, ErrorMessage, StackTrace, StartedDate, CompletedDate
            string        traceFilePath;
            List <string> resultLogFiles = new List <string>();

            var reader = new StreamReader(File.OpenRead(resultsFilePath));

            while (!reader.EndOfStream)
            {
                var line   = reader.ReadLine();
                var values = line.Split(',');
                Console.WriteLine("Publishing test {0}", values[0]);

                //Assign values from each line in CSV to result model...
                TestResultCreateModel testResultModel = new TestResultCreateModel();
                testResultModel.TestCaseTitle = testResultModel.AutomatedTestName = values[0];
                testResultModel.Outcome       = values[1];
                //Setting state to completed since we are only publishing results.
                //In advanced scenarios, you can choose to create a test result,
                // move it into in progress state while test test acutally runs and finally update the outcome with state as completed
                testResultModel.State         = "Completed";
                testResultModel.ErrorMessage  = values[2];
                testResultModel.StackTrace    = values[3];
                testResultModel.StartedDate   = values[4];
                testResultModel.CompletedDate = values[5];

                //store the path of the attachment in a list. We will publish attachments to results after we publish results and obtain result ids.
                traceFilePath = values[6]; resultLogFiles.Add(traceFilePath);

                //Add the result ot the results list...
                testResultsFromCsv.Add(testResultModel);
            }

            //Publish the results...
            List <TestCaseResult> publishedResults = client.AddTestResultsToTestRunAsync(testResultsFromCsv.ToArray(), teamProject, testRun.Id).Result;

            Console.WriteLine("Step 2: test results published...");

            //results are published in the order they were submitted. H
            //We will now loop through the results file and publish the attachments for each test result.
            //Path of the attachment for each result (sample trace log) is in last column of the result csv file...
            int i = 0;

            foreach (TestCaseResult r in publishedResults)
            {
                Console.WriteLine("Adding attachment for Test Result -> Id: {0}", r.Id);
                fileAttachment = GetAttachmentRequestModel(resultLogFiles.ElementAt(i++));
                testAttachment = client.CreateTestResultAttachmentAsync(fileAttachment, teamProject, testRun.Id, r.Id).Result;
                Console.WriteLine("Created test result attachment -> Id: {0}, Url: {1}", testAttachment.Id, testAttachment.Url);
            }

            //Mark the run complete...
            RunUpdateModel testRunUpdate   = new RunUpdateModel(completedDate: DateTime.Now.ToString(), state: "Completed");
            TestRun        RunUpdateResult = client.UpdateTestRunAsync(teamProject, testRun.Id, testRunUpdate).Result;

            Console.WriteLine("Step 3: Test run completed: {0}", RunUpdateResult.WebAccessUrl);
        }