-1

I have a TestSuite (TFS object) object which calls an add method .This add method accepts ITestCase as an argument .This ITestCase interface has different methods and properties described at below link:

http://msdn.microsoft.com/en-us/library/dd984690

Now i want to pass ITestCase object which implements some of those methods mentioned at above link.

   var ts = testPlan.TestSuites[i];
   var testCase = ts.TestCases.Add(<ITestCase testcase>);
Daniel Mann
  • 57,011
  • 13
  • 100
  • 120
krrishna
  • 2,050
  • 4
  • 47
  • 101

1 Answers1

1

Assuming that you actually want to create a real test case, as opposed to a mock in a unit test (or similar), then this blog post has a code sample which creates one: http://www.ewaldhofman.nl/post/2009/12/11/TFS-SDK-2010-e28093-Part-5-e28093-Create-a-new-Test-Case-work-item.aspx.

Here's some hacked-together code that creates a new test case, based on the blog entry above, and then adds it to an existing test suite (called "ExampleSuite2", located in a test plan called "TfsVersioning Test Plan"):

var tfsUri = new Uri("http://localhost:8080/tfs/");
var tfsConfigServer = new TfsConfigurationServer(tfsUri, new UICredentialsProvider());
tfsConfigServer.EnsureAuthenticated();

var projCollectionNode = tfsConfigServer.CatalogNode.QueryChildren(new[] {CatalogResourceTypes.ProjectCollection}, false,                                                               CatalogQueryOptions.None).FirstOrDefault();
var collectionId = new Guid(projCollectionNode.Resource.Properties["InstanceId"]);
var projCollection = tfsConfigServer.GetTeamProjectCollection(collectionId);

ITestManagementService tms = projCollection.GetService<ITestManagementService>();
var project = tms.GetTeamProject("TfsVersioning");

var testCase = project.TestCases.Create();
testCase.Title = "Browse my blog";
var navigateToSiteStep = testCase.CreateTestStep();
navigateToSiteStep.Title = "Navigate to \"http://www.ewaldhofman.nl\"";
testCase.Actions.Add(navigateToSiteStep);

var clickOnFirstPostStep = testCase.CreateTestStep();
clickOnFirstPostStep.Title = "Click on the first post in the summary";
clickOnFirstPostStep.ExpectedResult = "The details of the post are visible";
testCase.Actions.Add(clickOnFirstPostStep);
testCase.Save();

//Add test case to an existing test suite
var plans = project.TestPlans.Query("SELECT * FROM TestPlan where PlanName = 'TfsVersioning Test Plan'");
var plan = plans.First();
var firstMatchingSuite = project.TestSuites.Query("SELECT * FROM TestSuite where Title = 'ExampleSuite2'").First();
((IStaticTestSuite)firstMatchingSuite).Entries.Add(testCase);
plan.Save();

After running this code the new test case appeared in MTM, associated with the target test suite.

Ian Gilroy
  • 2,031
  • 16
  • 14
  • Gilory , That blog has the code which creates the test cases at Project level.I want it at a particular test plan and then at a test suite level – krrishna Aug 15 '12 at 16:10
  • Test cases belong to a project. After creating the test case I'm assuming that you can just add it to whichever test suite you need it to be associated with. – Ian Gilroy Aug 15 '12 at 16:19
  • Where will it add the test case if i follow the approach shared in your link ?And where do i find the test case in MTM which was created ? – krrishna Aug 16 '12 at 05:33
  • You find it by opening MTM and clicking on the test suite that you added the new test case to. I just tried this out - I'll add the code to my answer above. – Ian Gilroy Aug 16 '12 at 09:44
  • Thanks a ton Ian.It works perfectly. One more question i have is, after executing the testcase.save() method and while the code executes plan.save() method, if the execution fails with an error how can i discard testcase.save () changes ? I don't want that WorkItem to be created at all if it can't be added to TestSuite due to any reason. And also incase if a test plan doesn't have any test suite(usually MTM takes testplan name as TestSuite name in such cases) the code doesn't add the test case under TestSuite , but adds it at test project level . – krrishna Aug 17 '12 at 02:17
  • I'm no expert in this area but I guess...Do both of the .Save() calls as late as possible. You could save the test case immediately before adding it the test suite. I'd then wrap all of it in an exception handler which contains some rollback logic if something goes wrong. BTW: I'd recommend Shai Raiten's blog for TFS SDK samples. It has tons of useful entries. For example, this post discusses adding and removing test plans: http://blogs.microsoft.co.il/blogs/shair/archive/2010/06/30/tfs-api-part-26-add-remove-test-plans.aspx. – Ian Gilroy Aug 17 '12 at 07:36