使用 C# 创建类型测试用例的 Tfs 工作项

Tfs work item of type test case creation using c#

我正在尝试在 visual studio 中使用 c# 在 TFS 中创建类型测试用例的工作项。我能够设置除字段 "steps" 之外的所有字段的值。如何设置step字段的值?

我试过 workitem.Fields["steps"].value = "value" 但没有用。

这不起作用,因为您必须使用对 TestManagement Client 的引用。然后获取您的测试用例并添加如下新步骤:

TfsTeamProjectCollection tfs;

tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(uri)); 
tfs.Authenticate();

ITestManagementService service = (ITestManagementService)tfs.GetService(typeof(ITestManagementService));
ITestManagementTeamProject testProject = service.GetTeamProject(project);    

ITestCase testCase = TestProject.TestCases.Find(1);
ITestStep newStep = testCase.CreateTestStep();
newStep.Title = "New Step Title";
newStep.ExpectedResult = "New Step Expected Result";
testCase.Actions.Add(newStep);

查看这些链接:

  1. Programmatically Create Test Case Steps
  2. TFS API Part 51 – Adding Test Step & Shared Step

请注意 Steps 字段是 html 类型。您可以尝试使用 REST API 来设置步骤的值。举例如下:

POST https://xxxx.visualstudio.com/{teamproject}/_apis/wit/workitems/$Test%20Case?api-version=4.1

Content-Type: application/json-patch+json

[
  {
    "op": "add",
    "path": "/fields/System.Title",
    "from": null,
    "value": "Sample testcase"
  },
  {
    "op": "add",
    "path": "/fields/Microsoft.VSTS.TCM.Steps",
    "from": null,
    "value": "<steps id=\"0\" last=\"3\"><step id=\"2\" type=\"ValidateStep\"><parameterizedString isformatted=\"true\">&lt;DIV&gt;&lt;DIV&gt;&lt;P&gt;step1&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;</parameterizedString><parameterizedString isformatted=\"true\">&lt;DIV&gt;&lt;P&gt;&amp;nbsp;0&lt;/P&gt;&lt;/DIV&gt;</parameterizedString><description/></step><step id=\"3\" type=\"ValidateStep\"><parameterizedString isformatted=\"true\">&lt;DIV&gt;&lt;P&gt;step2&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;</parameterizedString><parameterizedString isformatted=\"true\">&lt;P&gt;&amp;nbsp;pass&lt;/P&gt;</parameterizedString><description/></step></steps>"
  }
]