1

For our new TFS 2015 server I want to create a webpage where you can see a history of warning counts and code coverage for each build. How do I get the information how many warnings occurred and how much code of the build was covered with unit tests? I have tried finding this by getting Builds via the BuildHttpClient but when debugging through the Build class I have not found any property that has that information. Here is the code how I get the builds:

    public static Build[] GetRunningBuilds(this BuildHttpClient buildServer, string collectionName)
    {
        return buildServer.GetBuildsAsync(collectionName, statusFilter: BuildStatus.InProgress).Result.ToArray();
    }

    public static Build[] GetFailedBuilds(this BuildHttpClient buildServer, string collectionName)
    {
        var builds = buildServer.GetBuildsAsync(collectionName, statusFilter: BuildStatus.Completed, resultFilter: BuildResult.Failed).Result.ToList();
        builds.AddRange(buildServer.GetBuildsAsync(collectionName, statusFilter: BuildStatus.Completed, resultFilter: BuildResult.PartiallySucceeded).Result);

        return builds.OrderBy(b=>b.FinishTime).ToArray();
    }

    public static DefinitionReference[] GetBuildDefinitions(this BuildHttpClient buildServer, string collectionName)
    {
        var buildDefinitions = buildServer.GetDefinitionsAsync(project: collectionName).Result.ToList();
        return buildDefinitions.ToArray();
    }
2
  • Are you using XAML builds or the new build system? Aug 26, 2016 at 16:56
  • The new build system. Aug 29, 2016 at 7:19

2 Answers 2

1

You need to use "GetBuildCodeCoverageAsync" method in Microsoft.TeamFoundation.TestManagement.WebApi to get code coverage information.

ITestManagementHttpClient.GetBuildCodeCoverageAsync(string, int, int, object, System.Threading.CancellationToken)

System.Threading.Tasks.Task> GetBuildCodeCoverageAsync(string project, int buildId, int flags, [object userState = null], [System.Threading.CancellationToken cancellationToken = null]) Member of Microsoft.TeamFoundation.TestManagement.WebApi.ITestManagementHttpClient

Summary: Sends REST based msg to Server for getting Code Coverage object for build based on buildId and flags This API can be consumed in GET process.

For the flags information, refer to this link for details: Get code coverage data for a build.

1
  • Is there a reason why the TFS portal would show results but the response from GetBuildCodeCoverageAsync would be an empty list?
    – Tedford
    Mar 28, 2019 at 21:22
0

To get code-coverage, you need to use the CoverageAnalysisManager.

Like this:

TfsTeamProjectCollection tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("http://tfs_server_here:8080/tfs/something_here"));
ITestManagementService tcm = (ITestManagementService) tfs.GetService(typeof(ITestManagementService));
ITestManagementTeamProject testManagementTeamProject = tcm.GetTeamProject("team_name_here");
ICoverageAnalysisManager coverageAnalysisManager = testManagementTeamProject.CoverageAnalysisManager;
IBuildCoverage[] queryBuildCoverage = coverageAnalysisManager.QueryBuildCoverage(build.Uri.ToString(), CoverageQueryFlags.Modules);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.