4

Using the TFS API, how can I get the outcome/result of a specific test case in a given test suite and plan?

With outcome/result I mean the value that tests are grouped by in MTM: Passed, failed, active, in progress or blocked

MTM test case outcome/result

Oskar
  • 7,945
  • 5
  • 36
  • 44
  • you may want to look at this http://stackoverflow.com/questions/8731854/querying-failed-unit-tests-from-tfs-sdk – Florian May 27 '13 at 15:03
  • Thanks, but that's not really what I'm looking for. As I said, I want to find out the result given a specific test case ID. – Oskar May 27 '13 at 15:12

3 Answers3

1

This is how I do it.

To get passed and totalTests I use: ITestRun run*

run.PassedTests and run.TotalTests

To see run state I use:

TestRunSTate.Aborted and TestRunState.InProgress

To see if the failed or is inconclusive I use:

TestOutcome.Failed or TestOutcome.Inconclusive

First I only used ITestRun to easy se results, but I see they lack any kind of "failed" there which I find very disturbing. So to send the right numbers to my test report that is mailed to the product owner I do the following when talking to the tfs api:

var tfs = Connect(optionsModel.CollectionUri);
var tcm = GetService<ITestManagementService>(tfs);
var wis = GetService<WorkItemStore>(tfs);

_testProject = tcm.GetTeamProject(optionsModel.TeamProjectName);

var plan = _testProject.TestPlans.Find(optionsModel.PlanId);

if (plan == null)
    throw new Exception("Could not find plan with that id.");

var run = plan.CreateTestRun(true);


var testSuite = _testProject.TestSuites.Find(optionsModel.SuiteId);


if (testSuite == null)
            throw new Exception("Could not find suite with that id.");

AddTestCasesBySuite(testSuite, optionsModel.ConfigId, plan, run);

run.Title = optionsModel.Title;
run.Save();

var failedTests = run.QueryResultsByOutcome(TestOutcome.Failed).Count;
var inconclusiveTests = run.QueryResultsByOutcome(TestOutcome.Inconclusive).Count;

Hope this helps optionsmodel is the information I take in from the user running the tsts

Nagik
  • 31
  • 4
0

I was trying to do the same thing, but using the REST API.

Just in case it helps someone, I managed to do that obtaining the testpoints from the suite:

https://dev.azure.com/{organization}/{project}/_apis/testplan/Plans/{planId}/Suites/{suiteId}/TestPoint?api-version=5.1-preview.2

More info: https://learn.microsoft.com/en-us/rest/api/azure/devops/testplan/test%20point/get%20points%20list?view=azure-devops-rest-5.1

Thiago
  • 1,103
  • 1
  • 16
  • 25
-1

You can use ITestManagementService and TestPlan query to get the result of specific Test plan

    var server = new Uri("http://servername:8080/tfs/collectionname");
    var tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(server);
    var service = tfs.GetService<ITestManagementService>();
    var testProject = service.GetTeamProject(teamProject);
    var plans = testProject.TestPlans.Query("SELECT * FROM TestPlan").Where(tp => tp.Name == YOURTESTPLANNAME).FirstOrDefault();

   ITestPlanCollection plans = tfsConnectedTeamProject.TestPlans.Query("Select * From TestPlan");
        foreach (ITestPlan plan in plans)
        {
            if (plan.RootSuite != null && plan.RootSuite.Entries.Count > 0)
            {
                foreach (ITestSuiteEntry suiteEntry in plan.RootSuite.Entries)
                {
                    var suite = suiteEntry.TestSuite as IStaticTestSuite;
                    if (suite != null)
                    {
                        ITestSuiteEntryCollection suiteentrys = suite.TestCases;
                        foreach (ITestSuiteEntry testcase in suiteentrys)
                        {
                            // Write code to get the test case 
                        }
                    }
                }
            }
        }

I hope this may help you.

SoftSan
  • 2,482
  • 3
  • 23
  • 54
  • 1
    This answer contains nothing about how to get the result/outcome of a test case, only how to iterate over test plans and test suites. – Oskar Dec 20 '13 at 16:41