コード例 #1
0
        private async Task <double?> GetCodeCoverageForBuild(String projectId, int buildId)
        {
            _logger.LogInformation($"[{nameof(GetCodeCoverageForBuild)}] BEGIN {{project:{projectId}, buildId:{buildId}}}");
            try
            {
                const string CoverageStatsLabel = "Lines";

                GitHttpClient            gitClient  = _connection.GetClient <GitHttpClient>();
                TestManagementHttpClient testClient = _connection.GetClient <TestManagementHttpClient>();
                //Получить покрытие кода по id билда
                var codeCoverage = await testClient.GetCodeCoverageSummaryAsync(projectId, buildId);

                _logger.LogInformation($"[{nameof(GetCodeCoverageForBuild)}] GetCodeCoverageSummaryAsync(project:{projectId}, buildId:{buildId}) success!");

                CodeCoverageStatistics CoverageStats = null;
                if (codeCoverage.CoverageData.Count > 0)
                {
                    // TODO: Переделать на случай если будет несколько CoverageData
                    CoverageStats = codeCoverage?.CoverageData[0].CoverageStats
                                    .FirstOrDefault(x => x.Label.Equals(CoverageStatsLabel, StringComparison.OrdinalIgnoreCase));
                }

                return(CoverageStats?.Covered * 100.00 / CoverageStats?.Total);
            }
            catch (Exception e)
            {
                _logger.LogError($"[{nameof(GetCodeCoverageForBuild)}] ERROR: {e.ToString()}");
                return(null);
            }
            finally
            {
                _logger.LogInformation($"[{nameof(GetCodeCoverageForBuild)}] COMPLETED");
            }
        }
コード例 #2
0
        private static void ValidateDevOpsCredentials(TestManagementHttpClient testManagementHttpClient)
        {
            _commandLineAccess.WriteToConsole(_messages.Stages.DevOpsCredentials.Status, _messages.Types.Stage);

            TestPlan testPlan;

            try
            {
                testPlan = testManagementHttpClient.GetPlansAsync(_inputOptions.ProjectName).Result.Single(x => x.Id.Equals(_inputOptions.TestPlanId));
            }
            catch (Exception e)
            {
                var innerException     = e.InnerException ?? e;
                var innerExceptionType = innerException.GetType();
                var message            = innerException.Message;

                if (innerExceptionType == typeof(VssUnauthorizedException))
                {
                    message = string.Format(_messages.Stages.DevOpsCredentials.FailureUserNotAuthorized, _inputOptions.CollectionUri);
                }
                else if (innerExceptionType == typeof(ProjectDoesNotExistWithNameException))
                {
                    message = string.Format(_messages.Stages.DevOpsCredentials.FailureNonExistingProject, _inputOptions.ProjectName);
                }
                else if (innerExceptionType == typeof(InvalidOperationException) && e.Message.Equals(SequenceContainsNoMatchingElementName))
                {
                    message = string.Format(_messages.Stages.DevOpsCredentials.FailureNonExistingTestPlan, _inputOptions.TestPlanId);
                }

                _commandLineAccess.WriteToConsole(message, _messages.Types.Error);

                throw innerException;
            }

            try
            {
                _ = testManagementHttpClient.GetTestSuitesForPlanAsync(_inputOptions.ProjectName, testPlan.Id).Result.Single(x => x.Id.Equals(_inputOptions.TestSuiteId));
            }
            catch (Exception e)
            {
                var innerException     = e.InnerException ?? e;
                var innerExceptionType = innerException.GetType();
                var message            = innerException.Message;

                if (innerExceptionType == typeof(VssUnauthorizedException))
                {
                    message = string.Format(_messages.Stages.DevOpsCredentials.FailureUserNotAuthorized, _inputOptions.CollectionUri);
                }
                else if (innerExceptionType == typeof(InvalidOperationException) && e.Message.Equals(SequenceContainsNoMatchingElementName))
                {
                    message = string.Format(_messages.Stages.DevOpsCredentials.FailureNonExistingTestSuite, _inputOptions.TestSuiteId);
                }

                _commandLineAccess.WriteToConsole(message, _messages.Types.Error);

                throw innerException;
            }

            _commandLineAccess.WriteToConsole(_messages.Stages.DevOpsCredentials.Success, _messages.Types.Success);
        }
コード例 #3
0
        public void InitializeServer(VssConnection connection)
        {
            ArgUtil.NotNull(connection, nameof(connection));
            _connection = connection;

            TestHttpClient = connection.GetClient <TestManagementHttpClient>();
        }
コード例 #4
0
    static void Main(string[] args)
    {
        string ur = "https://xxxxxxx/";
        TfsTeamProjectCollection ttpc = new TfsTeamProjectCollection(new Uri(ur));
        //Get build information
        BuildHttpClient bhc         = ttpc.GetClient <BuildHttpClient>();
        string          projectname = "Project";
        int             buildId     = 1;
        Build           bui         = bhc.GetBuildAsync(projectname, buildId).Result;
        //Get test run for the build
        TestManagementHttpClient ithc = ttpc.GetClient <TestManagementHttpClient>();

        Console.WriteLine(bui.BuildNumber);

        QueryModel qm = new QueryModel("Select * From TestRun Where BuildNumber Contains '" + bui.BuildNumber + "'");

        List <TestRun> testruns = ithc.GetTestRunsByQueryAsync(qm, projectname).Result;

        foreach (TestRun testrun in testruns)
        {
            List <TestCaseResult> testresults = ithc.GetTestResultsAsync(projectname, testrun.Id).Result;
            foreach (TestCaseResult tcr in testresults)
            {
                Console.WriteLine(tcr.TestCase.Name);
                Console.WriteLine(tcr.Outcome);
            }

            Console.ReadLine();
        }
        Console.ReadLine();
    }
コード例 #5
0
        private static void UploadCodeCoverageAttachmentsToOldStore(TestManagementHttpClient testClient, string buildurl, string projectName, string folder)
        {
            var testRuns = testClient.GetTestRunsAsync(projectName, buildurl).Result;

            foreach (var t in testRuns)
            {
                int testRunId = t.Id;

                Dictionary <string, string> files = new Dictionary <string, string>();

                // one meta data per module
                foreach (var file in Directory.EnumerateFiles(folder, "*.coverage", SearchOption.AllDirectories))
                {
                    string fileName = Path.GetFileName(file);

                    if (!files.ContainsKey(fileName))
                    {
                        var attachment = GetAttachmentRequestModel(file);
                        Task <TestAttachmentReference> trTask = testClient.CreateTestRunAttachmentAsync(attachment, projectName, testRunId);
                        trTask.Wait();
                        files.Add(fileName, file);
                    }
                }

                // buffer
                foreach (var file in Directory.EnumerateFiles(folder, "*.coveragebuffer", SearchOption.AllDirectories))
                {
                    var attachment = GetAttachmentRequestModel(file);

                    Task <TestAttachmentReference> trTask = testClient.CreateTestRunAttachmentAsync(attachment, projectName, testRunId);
                    trTask.Wait();
                }
            }
        }
コード例 #6
0
        static void Main(string[] args)
        {
            try
            {
                //System.Diagnostics.Debugger.Launch();
                string collectionUrl = args[0]; // $(System.TeamFoundationCollectionUri)
                var    connection    = new VssConnection(new Uri(collectionUrl), new Microsoft.VisualStudio.Services.OAuth.VssOAuthAccessTokenCredential(args[4]));

                var testClient = new TestManagementHttpClient(new Uri(collectionUrl), new Microsoft.VisualStudio.Services.OAuth.VssOAuthAccessTokenCredential(args[4]));

                string buildurl    = args[1];            // $(BUILD.BUILDURI)
                string projectName = args[2];            // $(SYSTEM.TEAMPROJECT)
                string folder      = args[3];
                int    buildId     = int.Parse(args[5]); //$(Build.BuildId)
                bool   useNewStore = false;
                string projectId   = string.Empty;

                try
                {
                    bool.TryParse(args[6], out useNewStore);
                    projectId = args[7]; //$(System.TeamProjectId)
                }
                catch (Exception e)
                {
                }

                // Create test run
                RunCreateModel runCreationModel = new RunCreateModel("somename", buildId: buildId,
                                                                     startedDate: DateTime.UtcNow.ToString(), completedDate: DateTime.UtcNow.ToString(),
                                                                     state: TestRunState.InProgress.ToString(), isAutomated: true);
                var testRun = testClient.CreateTestRunAsync(runCreationModel, projectName).Result;

                TestCaseResult result = new TestCaseResult
                {
                    AutomatedTestName    = "TestOne",
                    AutomatedTestStorage = "foo.dll",
                    Build = new ShallowReference {
                        Url = buildurl
                    },
                    TestCaseTitle = "TestOne"
                };
                testClient.AddTestResultsToTestRunAsync(new TestCaseResult[] { result }, projectName, testRun.Id).Wait();
                testClient.UpdateTestRunAsync(new RunUpdateModel("somename", state: TestRunState.Completed.ToString()), projectName, testRun.Id).Wait();

                if (useNewStore)
                {
                    UploadCodeCoverageAttachmentsToNewStore(folder, connection, buildId, projectId);
                }
                else
                {
                    UploadCodeCoverageAttachmentsToOldStore(testClient, buildurl, projectName, folder);
                }
            }
            catch (AggregateException e)
            {
                Console.WriteLine(e.InnerException);
                Console.WriteLine(e);
            }
        }
コード例 #7
0
ファイル: TFSConnect.cs プロジェクト: karthik-kt/SWAT-Main
        public async Task <List <TestSuite> > GetTestCaseResultByID(int testPlanId, bool asTree)
        {
            TestManagementHttpClient witClient     = connection.GetClient <TestManagementHttpClient>();
            List <TestSuite>         TestSuiteList = await witClient.GetTestSuitesForPlanAsync(Project, testPlanId, null,
                                                                                               null, null, asTree, null, default(System.Threading.CancellationToken));

            return(TestSuiteList);
        }
コード例 #8
0
ファイル: TFSConnect.cs プロジェクト: karthik-kt/SWAT-Main
        public async Task <List <TestPlan> > GetTestPlan()
        {
            TestManagementHttpClient witClient    = connection.GetClient <TestManagementHttpClient>();
            List <TestPlan>          TestPlanList = await witClient.GetPlansAsync(Project, null, null, null,
                                                                                  null, null, null, default(System.Threading.CancellationToken));

            return(TestPlanList);
        }
コード例 #9
0
ファイル: Program.cs プロジェクト: sergesh78/TFRestApi
 static void InitClients(VssConnection Connection)
 {
     WitClient            = Connection.GetClient <WorkItemTrackingHttpClient>();
     BuildClient          = Connection.GetClient <BuildHttpClient>();
     ProjectClient        = Connection.GetClient <ProjectHttpClient>();
     GitClient            = Connection.GetClient <GitHttpClient>();
     TfvsClient           = Connection.GetClient <TfvcHttpClient>();
     TestManagementClient = Connection.GetClient <TestManagementHttpClient>();
 }
コード例 #10
0
        public TfsBuildDefinitionRepository(VssCredentials credentials, SettingStore <Dictionary <int, BuildDefinition> > buildDefinitionStore, SettingStore <BuildCollection> buildStore, ConnectionSettings connectionSettings)
        {
            _buildDefinitionStore = buildDefinitionStore;
            _buildStore           = buildStore;
            _connectionSettings   = connectionSettings;
            var tfsCollectionUri = new Uri(_connectionSettings.TfsCollectionUrl);

            _buildClient          = new BuildHttpClient(tfsCollectionUri, credentials);
            _testManagementClient = new TestManagementHttpClient(tfsCollectionUri, credentials);
        }
コード例 #11
0
        private static int RunBuilds(BuildsCommandLineOptions options)
        {
            VssConnection            connection           = CreateConnection(options);
            ISink                    sink                 = CreateSink("builds", options);
            ProjectHttpClient        projectHttpClient    = connection.GetClient <ProjectHttpClient>();
            BuildHttpClient          buildClient          = connection.GetClient <BuildHttpClient>();
            TestManagementHttpClient testManagementClient = connection.GetClient <TestManagementHttpClient>();

            Task task = new BuildManager(projectHttpClient, buildClient, testManagementClient).WriteBuildDetails(sink);

            task.Wait();
            return(0);
        }
コード例 #12
0
        public async Task <List <TestCaseResult> > UpdateTestResult(string project, string runId, TestCaseResultUpdateModel[] results)
        {
            try
            {
                connection = new VssConnection(new Uri(instance), new VssCredentials());
                TestManagementHttpClient witClient       = connection.GetClient <TestManagementHttpClient>();
                List <TestCaseResult>    testcaseresults = await witClient.UpdateTestResultsAsync(results, project, Int32.Parse(runId), null, default(CancellationToken));

                return(testcaseresults);
            }
            catch
            {
                throw;
            }
        }
コード例 #13
0
        public async Task PublishCoverageSummary(CoverageSummary coverageSummary, CancellationToken cancellationToken)
        {
            var coverageData  = coverageSummary.CodeCoverageData;
            var coverageStats = coverageData?.CoverageStats;

            if (coverageData != null && coverageStats != null && coverageStats.Count() > 0)
            {
                // log coverage stats
                TraceLogger.Info(Resources.PublishingCodeCoverageSummary);
                foreach (var coverage in coverageStats)
                {
                    TraceLogger.Info(string.Format(Resources.CoveredStats, coverage.Label, coverage.Covered, coverage.Total));
                }

                try
                {
                    var uploadToTcm = _featureFlagHelper.GetFeatureFlagState(Constants.FeatureFlags.EnablePublishToTcmServiceDirectlyFromTaskFF, false);
                    _executionContext.TelemetryDataCollector.AddOrUpdate("UploadToTcm", uploadToTcm.ToString());

                    // Upload to tcm/tfs based on feature flag
                    if (uploadToTcm)
                    {
                        TestResultsHttpClient tcmClient = _clientFactory.GetClient <TestResultsHttpClient>();
                        using (new SimpleTimer("AzurePipelinesPublisher", "UploadSummary", _executionContext.TelemetryDataCollector))
                        {
                            await tcmClient.UpdateCodeCoverageSummaryAsync(coverageData, _executionContext.ProjectId, _executionContext.BuildId, cancellationToken : cancellationToken);
                        }
                    }
                    else
                    {
                        TestManagementHttpClient tfsClient = _clientFactory.GetClient <TestManagementHttpClient>();
                        using (new SimpleTimer("AzurePipelinesPublisher", "UploadSummary", _executionContext.TelemetryDataCollector))
                        {
                            await tfsClient.UpdateCodeCoverageSummaryAsync(coverageData, _executionContext.ProjectId, _executionContext.BuildId, cancellationToken : cancellationToken);
                        }
                    }
                }
                catch (Exception ex)
                {
                    TraceLogger.Error(string.Format(Resources.FailedtoUploadCoverageSummary, ex.ToString()));
                }
            }
        }
コード例 #14
0
        /// <summary>
        /// Gets the test runs of this build.
        /// </summary>
        /// <param name="testClient">Instance of a <see cref="TestManagementHttpClient"/> class.</param>
        /// <param name="runId">Id of the test run.</param>
        /// <param name="testCount">Number of tests in the test run.</param>
        /// <param name="maxResults">Number of maximum test results to read.
        /// <c>0</c> for skipping reading of test results. <c>null</c> for reading all test results in batches.</param>
        /// <returns>The test results for a build.</returns>
        private async Task <IEnumerable <AzureDevOpsTestResult> > GetTestResults(
            TestManagementHttpClient testClient,
            int runId,
            int testCount,
            int?maxResults)
        {
            var testResults = new List <AzureDevOpsTestResult>();

            if (maxResults <= 0)
            {
                return(testResults);
            }

            const int maxPagingSize          = 10000;
            var       resultCount            = 0;
            var       remainingResultsToRead = maxResults.HasValue ? Math.Min(testCount, maxResults.Value) : testCount;

            do
            {
                var resultsToRead = Math.Min(remainingResultsToRead, maxPagingSize);
                testResults.AddRange(
                    (await testClient
                     .GetTestResultsAsync(
                         this.ProjectId,
                         runId,
                         skip: resultCount,
                         top: resultsToRead)
                     .ConfigureAwait(false))
                    .Select(test =>
                            new AzureDevOpsTestResult
                {
                    AutomatedTestName = test.AutomatedTestName,
                    Outcome           = test.Outcome,
                    ErrorMessage      = test.ErrorMessage,
                }));

                resultCount           += resultsToRead;
                remainingResultsToRead = Math.Max(remainingResultsToRead - resultsToRead, 0);
            } while (remainingResultsToRead > 0);

            return(testResults);
        }
コード例 #15
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());
            }
        }
コード例 #16
0
        public async Task PublishCoverageSummaryAsync(IAsyncCommandContext context, VssConnection connection, string project, int buildId, IEnumerable <CodeCoverageStatistics> coverageData, CancellationToken cancellationToken)
        {
            // <todo: Bug 402783> We are currently passing BuildFlavor and BuildPlatform = "" There value are required be passed to command
            CodeCoverageData data = new CodeCoverageData()
            {
                BuildFlavor   = "",
                BuildPlatform = "",
                CoverageStats = coverageData.ToList()
            };

            FeatureAvailabilityHttpClient featureAvailabilityHttpClient = connection.GetClient <FeatureAvailabilityHttpClient>();

            if (FeatureFlagUtility.GetFeatureFlagState(featureAvailabilityHttpClient, CodeCoverageConstants.EnablePublishToTcmServiceDirectlyFromTaskFF, context))
            {
                TestResultsHttpClient tcmClient = connection.GetClient <TestResultsHttpClient>();
                await tcmClient.UpdateCodeCoverageSummaryAsync(data, project, buildId, cancellationToken : cancellationToken);
            }
            else
            {
                TestManagementHttpClient tfsClient = connection.GetClient <TestManagementHttpClient>();
                await tfsClient.UpdateCodeCoverageSummaryAsync(data, project, buildId, cancellationToken : cancellationToken);
            }
        }
 public TestManagementHttpClientWrapper(TestManagementHttpClient tcm)
 {
     _tcm = tcm ?? throw new ArgumentNullException(nameof(tcm));
 }
        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 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 publis 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>();

            //Query all test runs publishing against a release environmment...
            //Ideally, we'd use the GetTestRunsAsync with release uri and release environment uri filters here,
            //but GetTestRunsAsync does not support those filters yet...
            //Hence we will use GetTestRunsByQueryAsync...

            QueryModel      runQuery               = new QueryModel("Select * from TestRun where releaseUri='" + releaseUri + "' and releaseEnvironmentUri='" + releaseEnvironmentUri + "'");
            IList <TestRun> TestRunsAgainstRelease = client.GetTestRunsByQueryAsync(runQuery, teamProject).Result;

            // if any of the test runs has tests that have not passed, then flag failure...

            bool notAllTestsPassed = false;

            foreach (TestRun t in TestRunsAgainstRelease)
            {
                Console.WriteLine("Test run: {0}; Total tests: {1}; Passed tests: {2} ", t.Name, t.TotalTests, t.PassedTests);
                if (t.TotalTests != t.PassedTests)
                {
                    notAllTestsPassed = true;
                }
                //though we don't need to get test results,
                //we are getting test results and printing tests that failed just to demo how to query results in a test run
                IList <TestCaseResult> TestResutsInRun = client.GetTestResultsAsync(project: teamProject, runId: t.Id).Result;
                foreach (TestCaseResult r in TestResutsInRun)
                {
                    Console.WriteLine("Test: {0}; Outcome {1}", r.TestCaseTitle, r.Outcome);
                }
            }

            if (notAllTestsPassed)
            {
                Console.WriteLine("Not all tests passed.. Returning failure... ");
                Environment.Exit(1);
            }
            else
            {
                Console.WriteLine("All tests passed.. Returning success... ");
                Environment.Exit(0);
            }
        }
コード例 #19
0
        public static void AddScreenshots(string tfsURL, string tfsToken, string projectId, string buildDefinitionName, string projectName, string nameOfProjectWithTests)
        {
            var dictionary = DictionaryInteractions.ReadFromPropertiesFile(ReturnPath.SolutionFolderPath() + nameOfProjectWithTests + "/ExtentReport/ReportProperties.txt");

            string[] fileArray       = Directory.GetFiles(dictionary["ReportPath"]);
            var      onlyScreenshots = fileArray.Where(s => s.IndexOf(".png") != -1);

            if (onlyScreenshots.ToList().Count != 0)
            {
                VssConnection     vc  = new VssConnection(new Uri(tfsURL), new VssBasicCredential(string.Empty, tfsToken));
                BuildHttpClient   bhc = vc.GetClient <BuildHttpClient>();
                ProjectHttpClient ddd = vc.GetClient <ProjectHttpClient>();

                //ITestManagementTeamProject ddd1 = ddd.GetProject("hgfyjh");



                var   buildsPerProject = bhc.GetBuildsAsync(projectId).GetAwaiter().GetResult();
                var   lastBuildId      = buildsPerProject.Where <Build>(x => x.Definition.Name == buildDefinitionName).Max <Build>(z => z.Id);
                Build lastBuild        = buildsPerProject.FirstOrDefault <Build>(x => x.Id == lastBuildId);

                Console.Write("Last Build Number: " + lastBuildId);
                TestManagementHttpClient ithc = vc.GetClient <TestManagementHttpClient>();
                QueryModel     qm             = new QueryModel("Select * From TestRun Where BuildNumber Contains '" + lastBuild.BuildNumber + "'");
                List <TestRun> testruns       = ithc.GetTestRunsByQueryAsync(qm, projectName).Result;
                Console.Write("testruns.Count: " + testruns.Count);

                foreach (TestRun testrun in testruns)
                {
                    List <TestCaseResult> testresults = ithc.GetTestResultsAsync(projectName, testrun.Id).Result;
                    Console.Write("testresults.Count: " + testresults.Count);
                    foreach (TestCaseResult tcr in testresults)
                    {
                        var testNamesFromScreents = onlyScreenshots.Select(s => s.ToLower().Split(new string[] { "sec\\" }, StringSplitOptions.None)[1].Split(new string[] { "20" }, StringSplitOptions.None)[0]).Distinct();
                        //var screentsShotPerTest = onlyScreenshots.Where(s => s.Split(new string[] { "sec\\" }, StringSplitOptions.None)[1].Split(new string[] { "20" }, StringSplitOptions.None)[0] == scenarioName.Replace(" ", string.Empty));

                        Console.WriteLine("tcr.Id: " + tcr.Id);
                        if (testNamesFromScreents.ToList().Contains(tcr.AutomatedTestName.Split('.').Last().ToLower()))
                        {
                            Console.WriteLine("recognize Test: " + tcr.AutomatedTestName.Split('.').Last().ToLower());
                            using (var client = new HttpClient())
                            {
                                string credentials = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "", tfsToken)));
                                client.BaseAddress = new Uri(tfsURL);  //url of our account
                                client.DefaultRequestHeaders.Accept.Clear();
                                client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", credentials);
                                var screentsShotPerTest = onlyScreenshots.Where(s => s.Split(new string[] { "sec\\" }, StringSplitOptions.None)[1].Split(new string[] { "20" }, StringSplitOptions.None)[0].ToLower() == tcr.AutomatedTestName.Split('.').Last().ToLower());

                                foreach (var screenshot in screentsShotPerTest)
                                {
                                    Console.WriteLine("screentsShotPerTest: " + screenshot);
                                    var post = new PostImageTfs()
                                    {
                                        Stream         = Convert.ToBase64String(File.ReadAllBytes(screenshot)),
                                        AttachmentType = "GeneralAttachment",
                                        Comment        = "screenshot of error",
                                        FileName       = screenshot.Split(new string[] { "sec\\" }, StringSplitOptions.None)[1]
                                    };
                                    Console.WriteLine("tcr.TestRun.Id: " + tcr.TestRun.Id);
                                    Console.WriteLine("tcr.Id: " + tcr.Id);
                                    Console.WriteLine("screenshot.Split(new string[] { DateTime.Now.Year.ToString() }, StringSplitOptions.None)[2] " + screenshot.Split(new string[] { DateTime.Now.Year.ToString() }, StringSplitOptions.None)[2]);

                                    var test = new StringContent($"{{\"stream\": \"{post.Stream}\",\"fileName\": \"{post.FileName}\",\"comment\": \"{post.Comment}\",\"attachmentType\": \"{post.AttachmentType}\"}}", Encoding.UTF8, "application/json");

                                    HttpResponseMessage response = client.PostAsync("/DefaultCollection/" + projectName + "/_apis/test/runs/" + tcr.TestRun.Id + "/results/" + tcr.Id + "/attachments?api-version=2.0-preview.1", test).Result;
                                    Console.WriteLine("response: " + response.StatusCode);
                                }
                            }
                        }
                    }
                }
            }
        }
        public static TestManagementHttpClientWrapper CreateInstance(VssCredentials vssCredentials, string uri)
        {
            var tcm = new TestManagementHttpClient(new Uri(uri), vssCredentials);

            return(new TestManagementHttpClientWrapper(tcm));
        }
コード例 #21
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);
        }
コード例 #22
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);
        }
        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);
        }
コード例 #24
0
 public BuildManager(ProjectHttpClient projectHttpClient, BuildHttpClient buildClient, TestManagementHttpClient testManagementClient)
 {
     this.projectHttpClient    = projectHttpClient;
     this.buildClient          = buildClient;
     this.testManagementClient = testManagementClient;
 }
コード例 #25
0
        static void Main(string[] args)
        {
            //Pre-req: Create a query of test cases with Work item queries to be bulk deleted and then use this sample
            //Command line usage:
            //BulkDeleteTestCaseWorkItems.exe <account or tfs server> <team project name> <query in quotes> <pat token>
            //Example: BulkDeleteTestCaseWorkItems.exe https://manojbableshwar.visualstudio.com HealthClinic "Shared Queries/Troubleshooting/P4 Test Cases" <pat token>

            Uri accountUri; string teamProjectName, witTestCaseQuery, personalAccessToken; VssConnection connection;

            if (args.Length < 3 || args.Length > 4)
            {
                Console.WriteLine("Incomplete arguments. See: https://github.com/ManojBableshwar/VstsTestRestApiSamples/tree/master/BulkDeleteTestCaseWorkItems");
                return;
            }
            accountUri       = new Uri(args[0]);
            teamProjectName  = args[1];
            witTestCaseQuery = args[2];
            if (args.Length == 4)
            {
                // Create a connection to the account - use this for pat auth
                personalAccessToken = args[3];  // See https://www.visualstudio.com/docs/integrate/get-started/authentication/pats
                connection          = new VssConnection(accountUri, new VssBasicCredential(string.Empty, personalAccessToken));
            }
            else
            {
                // Create a connection to the account using client auth when pat is not available, so that users can auth using username/password
                connection = new VssConnection(accountUri, new VssClientCredentials());
            }

            // Get an instance of the work item tracking client to query test case work items to be deleted
            WorkItemTrackingHttpClient witClient = connection.GetClient <WorkItemTrackingHttpClient>();

            // Get an instance of the work item tracking client to delete test cases
            TestManagementHttpClient testClient = connection.GetClient <TestManagementHttpClient>();

            //Get the ID of the query specified...
            QueryHierarchyItem query = witClient.GetQueryAsync(teamProjectName, witTestCaseQuery).Result;

            //Query work item ids in the query...
            WorkItemQueryResult TestCaseIdsToDelete = witClient.QueryByIdAsync(query.Id).Result;

            if (TestCaseIdsToDelete.WorkItems.Count() > 0)
            {
                Console.WriteLine("Found {0} work items in query '{1}'.. Proceeding to delete...", TestCaseIdsToDelete.WorkItems.Count(), witTestCaseQuery);
            }
            else
            {
                Console.WriteLine("Found {0} work items returned in query '{1}'; Exiting... ", TestCaseIdsToDelete.WorkItems.Count(), witTestCaseQuery);
                return;
            }

            //Extract work item Ids to fetch work item details..
            int[]    workItemIds = TestCaseIdsToDelete.WorkItems.Select <Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models.WorkItemReference, int>(wif => { return(wif.Id); }).ToArray();
            string[] fields      = new[]
            {
                "System.Id",
                "System.Title",
                "System.WorkItemType"
            };

            // Fetch work item details..
            IEnumerable <WorkItem> TestCasesToDelete = witClient.GetWorkItemsAsync(workItemIds, fields, TestCaseIdsToDelete.AsOf).Result;

            foreach (var testcase in TestCasesToDelete)
            {
                // Skip if work item type is not test case, since DeleteTestCaseAsync is only for test case work item delete...
                if (testcase.Fields["System.WorkItemType"].ToString() != "Test Case")
                {
                    Console.WriteLine("Not a test case work item, skipping: {0} => {1}", testcase.Id, testcase.Fields["System.Title"]);
                }
                else
                {
                    try
                    {
                        //delete test case work item...
                        testClient.DeleteTestCaseAsync(teamProjectName, Convert.ToInt32(testcase.Id)).SyncResult();
                        Console.WriteLine("Deleted testcase: {0} => {1}", testcase.Id, testcase.Fields["System.Title"]);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Failed to delete testcase {0} => {1}. Error: {2}", testcase.Id, testcase.Fields["System.Title"], e.Message);
                    }
                }
            }
        }
コード例 #26
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/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 publis 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>();

            //Query all test runs publishing against a build

            IList <TestRun> TestRunsAgainstBuild = client.GetTestRunsAsync(projectId: teamProject, buildUri: buildUri).Result;


            // if any of the test runs has tests that have not passed, then flag failure...

            bool notAllTestsPassed = false;

            foreach (TestRun t in TestRunsAgainstBuild)
            {
                Console.WriteLine("Test run: {0}; Total tests: {1}; Passed tests: {2} ", t.Name, t.TotalTests, t.PassedTests);
                if (t.TotalTests != t.PassedTests)
                {
                    notAllTestsPassed = true;
                }
                //though we don't need to get test results,
                //we are getting test results and printing tests that failed just to demo how to query results in a test run
                IList <TestCaseResult> TestResutsInRun = client.GetTestResultsAsync(project: teamProject, runId: t.Id).Result;
                foreach (TestCaseResult r in TestResutsInRun)
                {
                    Console.WriteLine("Test: {0}; Outcome {1}", r.TestCaseTitle, r.Outcome);
                }
            }

            if (notAllTestsPassed)
            {
                Console.WriteLine("Not all tests passed.. Returning failure... ");
                Environment.Exit(1);
            }
            else
            {
                Console.WriteLine("All tests passed.. Returning success... ");
                Environment.Exit(0);
            }
        }