简体   繁体   中英

Not getting test case results(passed/failed/blocked) from TFS API

I'm trying to get the results of the test cases in tfs api

in test-case manager i see this results: https://gyazo.com/72ccfcef96d1718907ea702d7a8bbfc9

but when i try get this results in my test-case util i can't get any results:

var testResults = project.TestResults.ByTestId(7987);
foreach (var result in testResults)
{
    var topIteration = result.Iterations.FirstOrDefault();
    if (topIteration == null)
        continue;
    // do something
}

because topIteration always equals null.

how can i get correct results (passed/failed/blocked) from tfs api?

According to your code, seems you want to retrieve test results for particular test case using TFS API. You can try to follow below steps to achieve what you need.

Connect to TFS Server/Collection/team project

Uri tfsUri = new Uri("http://servername:8080/tfs/collectionname");
teamProjectCollection = new TfsTeamProjectCollection(tfsUri);

iTestManagementService = teamProjectCollection.GetService<ITestManagementService>();
tfsConnectedTeamProject = iTestManagementService.GetTeamProject("Team Project Name");

Once connected to TFS server, call APIs to get test results for any particular test case. Below line of code will get all results associated with passed test case Id and this returned list is not sorted

var testResults = tfsConnectedTeamProject.TestResults.ByTestId(7987);

Then you can sort this using below code using LastUpdated property of ITestCaseResult and it will return you sorted list of test result.

var resSort = from res in testResults
orderby res.LastUpdated descending
select res;

If you only need latest test result from result collection then below is code. Use above sorted list and retrieve top most record.

var latestTestResult = resSort.First<ITestCaseResult>();

Check the code below:

TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(new Uri("url"));
tfs.EnsureAuthenticated();

IBuildServer tfsBuildServer = tfs.GetService<IBuildServer>();

IBuildDefinition buildDef = tfsBuildServer.GetBuildDefinition("TeamProject", "DefinitionName");

 var BuildUri = buildDef.LastBuildUri;

 ITestManagementService testManagement = (ITestManagementService)tfs.GetService(typeof(ITestManagementService));
 ITestManagementTeamProject testManagementTeamProject = testManagement.GetTeamProject("TeamProject");
 IEnumerable<ITestRun> testRuns = testManagementTeamProject.TestRuns.ByBuild(BuildUri);


   foreach (ITestRun testRun in testRuns)
     {
       foreach (ITestCaseResult result in testRun.QueryResults())
          {
            Console.WriteLine(string.Format("TestCaseID:{0}", result.TestCaseTitle.ToString()));
            Console.WriteLine(string.Format("TestCaseOutcome:{0}", result.Outcome.ToString()));
           }
      }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM