To use TFS Test Cases via code, you should first create TFS Collection and Team Test Project. You can find more information in my previous post: How to: Connect to TFS Team Project C# Code.
Next you need to connect to a specific test plan. You can read more in the article: Manage TFS Test Plans C# Code.
In MS Test Manager, you can edit, create, delete test cases. Here I am going to show you how to do these actions via C# code.
Get All TFS Test Cases
Our first job is to create a wrapper class for the TFS Test Case (ITestCase). It will contain only the most important properties of the base core TFS Test Case object and will be serializable because the standard MS objects are not. This means that you cannot use them in web services or put them into the clipboard.
If you want to get all test cases in the current Team Project, you can use the following method.
If you want to get the test cases from a particular test suite. You can use a slightly modified method. The test plan and the current suite are refreshed to obtain the latest added test cases. Otherwise, the TFS will return you the cached snapshot of the query.
Delete TFS Test Case
Unfortunately, the TFS API doesn’t support delete operations of TFS Test Cases. You can remove them from TFS Test Suites, but they are not deleted. You can find them with TFS Queries or using MS Test Manager – Test Case Manager View.
Create TFS Test Case
Here I will show you how to create a new TFS Test Case without test steps. Only the most basic properties will be initialized. You will be able to find more information about test steps and shared steps in my future TFS API related posts.
The above extension method can be used in two modes – SAVE and UPDATE. If the parameter createdNew is right, a new test case will be created and saved. Otherwise, the properties of the existing test case will be updated and saved.
If the test case is added to TFS test suite, it should be saved/created in advance because of that we call the Flush-Save methods before the Suite-Test Case association. Also after the addition to TFS Test Suite you need to save the test case again.