-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTfsApi.cs
221 lines (208 loc) · 8.75 KB
/
TfsApi.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.TestManagement.Client;
namespace tfs_cli
{
class TfsApi
{
public const int DURATION_MULTIPLIER = 10000000;
private TfsTeamProjectCollection _tfs;
private ConnectionData _conData;
private ITestManagementTeamProject _project;
private ITestPlan _plan;
public TfsApi(TfsTeamProjectCollection tfs, ConnectionData con)
{
_tfs = tfs;
_conData = con;
}
public ITestManagementTeamProject GetProject()
{
// get tfs project
try
{
ITestManagementService test_service = (ITestManagementService)_tfs.GetService(typeof(ITestManagementService));
_project = test_service.GetTeamProject(_conData.Project());
return _project;
}
catch (Exception e) { TfsCliHelper.ExitWithError(string.Format("Could not get TFS project {0}: {1}", _conData.Project(), e.Message)); }
return null;
}
public ITestPlan GetTestPlan()
{
if (_project == null)
GetProject();
string query = string.Format("SELECT * FROM TestPlan WHERE PlanName = \"{0}\"", _conData.Testplan());
ITestPlanCollection plans = _project.TestPlans.Query(query);
if (plans.Count == 0)
TfsCliHelper.ExitWithError(string.Format("TestPlan {0} not found in project {1}", _conData.Testplan(), _conData.Project()));
else if (plans.Count > 1)
TfsCliHelper.ExitWithError("More then one TestPlan found. This situation is currently not supported. Please fill a feature request.");
_plan = plans.First();
return _plan;
}
public ITestConfiguration GetTestConfiguration(string name)
{
if(_project == null)
GetProject();
foreach (ITestConfiguration config in _project.TestConfigurations.Query(
"Select * from TestConfiguration"))
{
if (config.Name == name)
return config;
}
// otherwise create new
ITestConfiguration configuration = _project.TestConfigurations.Create();
configuration.Name = name;
configuration.Description = "tfs_cli configuration";
configuration.Values.Add(new KeyValuePair<string, string>("Browser", "IE"));
configuration.Save();
return configuration;
}
public ITestRun CreateRun(ITestSuiteBase suite, string title, string comment, string buildNum, string attach)
{
if(_plan == null)
GetTestPlan();
ITestRun tfsRun = SaveRun(suite, title, buildNum);
tfsRun.Comment = comment;
if (attach != null)
{
tfsRun.Attachments.Add(tfsRun.CreateAttachment(attach));
}
tfsRun.Save();
return tfsRun;
}
public ITestCaseResult GetTestResult(ITestRun run, string test_name)
{
for (int i = 0; i < run.QueryResults().Count; i++)
{
ITestCaseResult res = run.QueryResults()[i];
if(res.GetTestCase().Title == test_name)
return res;
}
return null;
}
public void UpdateTestResult(
ITestCaseResult result,
string outcome,
string duration,
string comment,
string failure_type,
string error_message,
string attach
)
{
try
{
result.Outcome = (TestOutcome)Enum.Parse(typeof(TestOutcome), outcome);
result.Comment = comment;
result.Owner = _tfs.AuthorizedIdentity;
result.RunBy = _tfs.AuthorizedIdentity;
result.DateStarted = DateTime.Now;
result.Duration = new TimeSpan(DURATION_MULTIPLIER * (long.Parse(duration) + 1));
result.DateCompleted = DateTime.Now.AddTicks(result.Duration.Ticks);
result.FailureType = (FailureType)Enum.Parse(typeof(FailureType), failure_type);
result.ErrorMessage = error_message;
var iteration = CreateIteration(result, outcome, comment, duration, attach);
result.Iterations.Add(iteration);
result.State = TestResultState.Completed;
result.Save(false);
}
catch (System.FormatException)
{
TfsCliHelper.ExitWithError("Provided parameters are incorrect (e.g. duration is in wrong format)");
}
catch (Exception e)
{
TfsCliHelper.Debug(e.StackTrace);
TfsCliHelper.ExitWithError("Error occured while creating test result: " + e.Message);
}
}
public List<ITestSuiteBase> GetSuites()
{
if(_plan == null)
GetTestPlan();
List<ITestSuiteBase> res = new List<ITestSuiteBase>();
if (_plan.RootSuite.TestCases.Count > 0)
{
res.Add(_plan.RootSuite);
}
GetSuitesRecursive(_plan.RootSuite, res);
return res;
}
public ITestSuiteBase GetSuite(string name)
{
if(_plan == null)
GetTestPlan();
if (name == null)
return _plan.RootSuite;
foreach (ITestSuiteBase suite in GetSuites())
{
if (suite.Title == name)
return suite;
}
TfsCliHelper.ExitWithError(string.Format("Testsuite \"{0}\" not found inside testplan \"{1}\"", name, _plan.Name));
return null;
}
private void GetSuitesRecursive(IStaticTestSuite suite, List<ITestSuiteBase> res){
foreach(ITestSuiteEntry entry in suite.Entries)
{
if(entry.EntryType == TestSuiteEntryType.StaticTestSuite)
{
if (entry.TestSuite.AllTestCases.Count > 0)
{
res.Add(entry.TestSuite);
GetSuitesRecursive((IStaticTestSuite)entry.TestSuite, res);
}
}
else if(entry.EntryType == TestSuiteEntryType.DynamicTestSuite)
{
TfsCliHelper.ExitWithError(string.Format("Testplan {0} has dynamic test suite {1}. Dynamic suites are not supported.", suite.Plan.Name, suite.Title));
}
}
return;
}
private ITestRun SaveRun(ITestSuiteBase suite, string title, string buildNum)
{
ITestRun tfsRun = _plan.CreateTestRun(false);
tfsRun.DateStarted = DateTime.Now;
tfsRun.DateCompleted = DateTime.Now;
tfsRun.Title = title;
tfsRun.BuildNumber = buildNum;
tfsRun.Owner = _tfs.AuthorizedIdentity;
// Add testpoints to testrun
tfsRun.AddTestPoints(_plan.QueryTestPoints(string.Format("SELECT * FROM TestPoint WHERE SuiteId = {0}", suite.Id)), _tfs.AuthorizedIdentity);
// save test run
tfsRun.Save();
return tfsRun;
}
private ITestIterationResult CreateIteration(ITestCaseResult result, string outcome, string comment, string duration, string attach)
{
// create and populate iteration
try
{
var iteration = result.CreateIteration(1);
iteration.Outcome = (TestOutcome)Enum.Parse(typeof(TestOutcome), outcome);
iteration.DateStarted = DateTime.Now;
iteration.DateCompleted = DateTime.Now;
iteration.Duration = new TimeSpan(DURATION_MULTIPLIER * (long.Parse(duration) + 1));
iteration.Comment = comment;
if (attach != null)
iteration.Attachments.Add(iteration.CreateAttachment(attach));
return iteration;
}
catch (System.FormatException)
{
TfsCliHelper.ExitWithError("Provided parameters are incorrect (e.g. duration is in wrong format)");
}
catch (Exception)
{
TfsCliHelper.ExitWithError("Error occured while creating test iteration");
}
return null;
}
}
}