【发布时间】:2016-10-25 08:37:31
【问题描述】:
我有一种方法似乎可以仅针对其运行的配置更新 TFS 测试结果。但这似乎非常低效。有人可以帮我弄清楚我做错了什么吗? - 具体来说,我似乎不必为给定的测试 ID 获取 testSuites,然后遍历这些套件的所有测试以与我已有的测试 ID 进行比较。
例如,我针对 Windows 8/Chrome 进行了测试。所以 TestContext 包含映射到配置的适当值。我想在它存在的每个测试套件中更新该配置的测试结果。这是代码:
public void UpdateResult(TestContext context, bool passed)
{
int testCaseID = Convert.ToInt32(context.Test.Properties["id"]);
TfsTeamProjectCollection projectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(tfsUri);
ITestManagementService service = projectCollection.GetService<ITestManagementService>();
ITestManagementTeamProject teamProject = service.GetTeamProject(teamProjectName);
ITestCase testCase = teamProject.TestCases.Find(testCaseID);
ITestSuiteCollection testSuites = teamProject.TestSuites.ReferencingTestCase(testCaseID);
if (testCase != null)
{
// ---- Get Test Plan
int testPlanId = 20589;
ITestPlan testPlan = teamProject.TestPlans.Find(testPlanId);
// ---- Create Test Run
ITestRun testRun = teamProject.TestRuns.Create();
testRun = testPlan.CreateTestRun(true);
// ---- Get TestPoint
ITestPointCollection points = testPlan.QueryTestPoints("SELECT * FROM TestPoint WHERE TestCaseId =" + testCaseID);
foreach (ITestPoint tp in points)
{
//Get configuration, which contains configuration values
ITestConfiguration testConfiguration = teamProject.TestConfigurations.Query("Select * from TestConfiguration WHERE Name='" + tp.ConfigurationName + "'")[0];
IDictionary<string, string> testConfigValues = testConfiguration.Values;
if ((!testConfigValues.ContainsKey("Browser") ||
testConfigValues["Browser"].ToLower() == context.Test.Properties["Browser"].ToString().ToLower())
&& (!testConfigValues.ContainsKey("Operating System") ||
testConfigValues["Operating System"].ToLower() == context.Test.Properties["Operating System"].ToString().ToLower())
)
{
testRun.AddTestPoint(tp, testPlan.Owner);
}
}
testRun.State = TestRunState.Completed;
testRun.Save();
foreach (ITestSuiteBase testSuite in testSuites)
{
foreach (ITestSuiteEntry testCse in testSuite.TestCases)
{
ITestCaseResultCollection results = testRun.QueryResults();
if (testCse.TestCase.Id == testCaseID)
{
foreach (IdAndName config in testCse.Configurations)
{
IEnumerable<ITestCaseResult> testCaseResults = from tr in results
where
tr.TestConfigurationId == config.Id &&
tr.TestCaseId == testCase.Id
select tr;
foreach (ITestCaseResult result in testCaseResults)
{
result.Outcome = passed ? TestOutcome.Passed : TestOutcome.Failed;
result.State = TestResultState.Completed;
result.Save(true);
}
}
}
}
}
testRun.Save();
testRun.Refresh();
}
}
【问题讨论】: