How to create a test run and result using the Team Foundation Server API?

How to create a test run and result using the Team Foundation Server API?

Creating a test run and result using the Team Foundation Server (TFS) API involves several steps, including creating a test plan, test suite, test case, test run, and test result. The TFS API provides a set of classes and methods to interact with these entities programmatically.

Here's a step-by-step guide on how to create a test run and result using the TFS API in C#:

  • Install the required NuGet packages: To work with the TFS API, you need to install the "Microsoft.TeamFoundationServer.ExtendedClient" NuGet package. You can install it using the NuGet Package Manager Console:

    Install-Package Microsoft.TeamFoundationServer.ExtendedClient 
  • Import the necessary namespaces: In your C# code file, import the required namespaces:

using Microsoft.TeamFoundation.TestManagement.Client; using Microsoft.TeamFoundation.Client; 
  • Connect to your TFS project:
var tfsUri = new Uri("http://YourTfsServer:8080/tfs/YourCollection"); var tfsCredentials = new System.Net.NetworkCredential("YourUsername", "YourPassword"); var tfsProjectName = "YourProject"; var tfsConfigurationServer = TfsConfigurationServerFactory.GetConfigurationServer(tfsUri); tfsConfigurationServer.Credentials = tfsCredentials; var tfsProjectCollection = tfsConfigurationServer.GetTeamProjectCollection(tfsProjectName); var testManagementService = tfsProjectCollection.GetService<ITestManagementService>(); var testManagementTeamProject = testManagementService.GetTeamProject(tfsProjectName); 
  • Create a test run:
ITestPlan testPlan = testManagementTeamProject.TestPlans.Find(YourTestPlanId); ITestSuiteBase testSuite = testPlan.RootSuite.Entries[YourTestSuiteId]; ITestPointCollection testPoints = testPlan.QueryTestPoints("SELECT * FROM TestPoint WHERE SuiteId = " + testSuite.Id); ITestRun testRun = testPlan.CreateTestRun(false); testRun.Title = "Your Test Run Title"; testRun.State = TestRunState.InProgress; testRun.Save(); 
  • Create a test result:
ITestCase testCase = testManagementTeamProject.TestCases.Find(YourTestCaseId); ITestPoint testPoint = testPoints.FirstOrDefault(tp => tp.TestCaseId == testCase.Id); ITestResult testResult = testRun.QueryResults().FirstOrDefault(r => r.TestCaseId == testCase.Id); if (testResult == null) { testResult = testRun.CreateTestResult(testPoint); testResult.Outcome = TestOutcome.Passed; // Set the outcome of the test (Passed, Failed, etc.) testResult.State = TestResultState.Completed; testResult.Save(); } 

Remember to replace YourTfsServer, YourCollection, YourUsername, YourPassword, YourProject, YourTestPlanId, YourTestSuiteId, and YourTestCaseId with your specific values.

With these steps, you can programmatically create a test run and result in TFS using the TFS API in C#. This can be helpful when integrating test automation frameworks or custom testing tools with TFS.

Examples

  1. "Team Foundation Server API create test run C#"

    Description: Learn how to create a test run using the Team Foundation Server (TFS) API in C#.

    // Connect to TFS TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(new Uri("https://your-tfs-url")); tfs.EnsureAuthenticated(); // Get test management service ITestManagementService testManagementService = tfs.GetService<ITestManagementService>(); // Get test project ITestManagementTeamProject testProject = testManagementService.GetTeamProject("YourProjectName"); // Create a test run ITestRun testRun = testProject.TestRuns.Create(); // Set properties of the test run testRun.Title = "New Test Run"; testRun.StartDate = DateTime.Now; testRun.State = TestRunState.InProgress; // Save the test run testRun.Save(); 

    This code establishes a connection to TFS, retrieves the test management service, creates a new test run, sets its properties (such as title, start date, and state), and then saves the test run.

  2. "TFS API create test run and result C#"

    Description: See an example of creating a test run and result using the TFS API in C#.

    // Connect to TFS TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(new Uri("https://your-tfs-url")); tfs.EnsureAuthenticated(); // Get test management service ITestManagementService testManagementService = tfs.GetService<ITestManagementService>(); // Get test project ITestManagementTeamProject testProject = testManagementService.GetTeamProject("YourProjectName"); // Create a test run ITestRun testRun = testProject.TestRuns.Create(); // Set properties of the test run testRun.Title = "New Test Run"; testRun.StartDate = DateTime.Now; testRun.State = TestRunState.InProgress; // Create a test result ITestCaseResult testCaseResult = testRun.QueryResults()[0].CreateTestResult(); // Set properties of the test result testCaseResult.Outcome = TestOutcome.Passed; testCaseResult.Comment = "Test passed successfully"; // Save the test result testCaseResult.Save(); // Save the test run testRun.Save(); 

    This code connects to TFS, creates a test run, sets its properties, creates a test result within the test run, sets its outcome and comment, and then saves both the test result and test run.

  3. "TFS API create test run and result example C#"

    Description: Explore a sample code demonstrating how to create a test run and result using the TFS API in C#.

    // Connect to TFS TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(new Uri("https://your-tfs-url")); tfs.EnsureAuthenticated(); // Get test management service ITestManagementService testManagementService = tfs.GetService<ITestManagementService>(); // Get test project ITestManagementTeamProject testProject = testManagementService.GetTeamProject("YourProjectName"); // Create a test run ITestRun testRun = testProject.TestRuns.Create(); // Set properties of the test run testRun.Title = "New Test Run"; testRun.StartDate = DateTime.Now; testRun.State = TestRunState.InProgress; // Create a test result ITestCaseResult testCaseResult = testRun.QueryResults()[0].CreateTestResult(); // Set properties of the test result testCaseResult.Outcome = TestOutcome.Passed; testCaseResult.Comment = "Test passed successfully"; // Save the test result testCaseResult.Save(); // Save the test run testRun.Save(); 

    This code snippet connects to TFS, creates a test run, sets its properties, creates a test result within the test run, sets its outcome and comment, and then saves both the test result and test run.

  4. "C# TFS API create test run and result"

    Description: Learn how to use C# with the TFS API to create a test run and result.

    // Connect to TFS TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(new Uri("https://your-tfs-url")); tfs.EnsureAuthenticated(); // Get test management service ITestManagementService testManagementService = tfs.GetService<ITestManagementService>(); // Get test project ITestManagementTeamProject testProject = testManagementService.GetTeamProject("YourProjectName"); // Create a test run ITestRun testRun = testProject.TestRuns.Create(); // Set properties of the test run testRun.Title = "New Test Run"; testRun.StartDate = DateTime.Now; testRun.State = TestRunState.InProgress; // Create a test result ITestCaseResult testCaseResult = testRun.QueryResults()[0].CreateTestResult(); // Set properties of the test result testCaseResult.Outcome = TestOutcome.Passed; testCaseResult.Comment = "Test passed successfully"; // Save the test result testCaseResult.Save(); // Save the test run testRun.Save(); 

    This code establishes a connection to TFS, creates a test run, sets its properties, creates a test result within the test run, sets its outcome and comment, and then saves both the test result and test run.

  5. "C# TFS API create test run and result example"

    Description: See an example of using C# with the TFS API to create a test run and result.

    // Connect to TFS TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(new Uri("https://your-tfs-url")); tfs.EnsureAuthenticated(); // Get test management service ITestManagementService testManagementService = tfs.GetService<ITestManagementService>(); // Get test project ITestManagementTeamProject testProject = testManagementService.GetTeamProject("YourProjectName"); // Create a test run ITestRun testRun = testProject.TestRuns.Create(); // Set properties of the test run testRun.Title = "New Test Run"; testRun.StartDate = DateTime.Now; testRun.State = TestRunState.InProgress; // Create a test result ITestCaseResult testCaseResult = testRun.QueryResults()[0].CreateTestResult(); // Set properties of the test result testCaseResult.Outcome = TestOutcome.Passed; testCaseResult.Comment = "Test passed successfully"; // Save the test result testCaseResult.Save(); // Save the test run testRun.Save(); 

    This code snippet connects to TFS, creates a test run, sets its properties, creates a test result within the test run, sets its outcome and comment, and then saves both the test result and test run.


More Tags

dispose event-bubbling imputation string-formatting unpack click intervention outlook-addin filestructure user-accounts

More C# Questions

More Chemical reactions Calculators

More Dog Calculators

More Animal pregnancy Calculators

More Biochemistry Calculators