【问题标题】:Execute Test in MTM via Code通过代码在 MTM 中执行测试
【发布时间】:2014-06-09 23:55:25
【问题描述】:

我在 MTM 中有一组基于 Selenium 的测试,它们采用单元测试的形式。如果我进入 MTM 并告诉他们运行,我就能很好地运行它们。我想知道是否有某种 API 可以用来启动这些?

我们有一个用 ASP.NET 编写的仪表板,我们真正想要的是如果我们可以有一个播放按钮来执行测试计划。我不太确定在这方面要搜索什么,甚至根本不确定。

一个可能的解决方案是我构建一个测试工具并使用反射来运行 DLL,但这会很混乱。

【问题讨论】:

标签: c# selenium microsoft-test-manager


【解决方案1】:

您可以使用 TFS API 执行您在 MTM 中管理的测试。
不幸的是,这个 APIMSDN 上的文档记录很差,真的很遗憾...
我的提示:更改到 MSDN 页面上的 Visual Studio 2012 版本,您将获得更多文档(仍然太少但总比没有好......)。

这里是一个示例,如何在您的 Test Plan 中为特定 运行属于特定 Test Suite 的所有 Test Cases >测试配置在您选择的测试环境中:

string tfsUri= <tfs uri like    @"https://<your tfs>/tfs/<your collection>"      >;
string userName = <TFS user name>;
string password = <password>,
string projectName = <TFS project name>;
int planId = <test plan id>;
int suiteId = <test suite id>;
int settingsId = <test settings id>;
int configurationId = <test configuration id>;
string environmentName = <test environment you want to run the tests on>;

TfsTeamProjectCollection tfsCollection = new TfsTeamProjectCollection(new Uri(tfsUri), new System.Net.NetworkCredential(userName, password));
tfsCollection.EnsureAuthenticated();

ITestManagementService testManagementService = tfsCollection.GetService<ITestManagementService>();
ITestManagementTeamProject project = testManagementService.GetTeamProject(projectName);

//Get user name
TeamFoundationIdentity tfi = testManagementService.AuthorizedIdentity;

ITestPlan plan = project.TestPlans.Find(planId);
ITestSuiteBase suite = project.TestSuites.Find(suiteId);
ITestSettings testSettings = project.TestSettings.Find(settingsId);
ITestConfiguration testConfiguration = project.TestConfigurations.Find(configurationId);

// Unfortunately test environment name is not exactly the name you see in MTM.
// In order to get the name of your environments just call
// project.TestEnvironments.Query()
// set a breakpoint, run this code in debuger and check the names.      
ITestEnvironment testEnvironment = project.TestEnvironments.Find((from te in project.TestEnvironments.Query()
                                                                  where te.Name.ToUpper().Equals(environmentName.ToUpper())
                                                                  select te.Id).SingleOrDefault());

ITestRun testRun = plan.CreateTestRun(true);
testRun.Owner = tfi;
testRun.Controller = testEnvironment.ControllerName;
testRun.CopyTestSettings(testSettings);
testRun.TestEnvironmentId = testEnvironment.Id;
testRun.Title = "Tests started from the dashboard";

//Get test points
ITestPointCollection testpoints = plan.QueryTestPoints("SELECT * FROM TestPoint WHERE SuiteId = " + suite.Id + " and ConfigurationId = " + testConfiguration.Id);

foreach (ITestPoint tp in testpoints)
{
    testRun.AddTestPoint(tp, tfi);
}

// This call starts your tests!
testRun.Save();

【讨论】:

  • Elena,还有一个问题,这会在 MTM 环境中设置的其中一台机器上启动它们吗?或者这会在本地机器上启动它们吗?
  • 此代码在 MTM 环境中启动它们。该示例包含调用 project.TestEnvironments.Find(...) ,然后使用设置测试运行属性时获得的环境:testRun.Controller = testEnvironment.ControllerName。这是指定应该在哪个 MTM 环境测试上运行的方法。
  • 只是为了更新这一点。代码完美运行!我唯一遇到的问题是这条线: ITestPointCollection testpoints = plan.QueryTestPoints("SELECT * FROM TestPoint WHERE SuiteId = " + suite.Id + " and ConfigurationId = " + testConfiguration.Id);它没有返回任何东西,所以我不得不使用它: ITestPointCollection testpoints = plan.QueryTestPoints(string.Format("SELECT * FROM TestPoint WHERE RecursiveSuiteID = {0}", plan.RootSuite.Id));再次感谢埃琳娜!
【解决方案2】:

您可以使用 MSTEST.exe 从命令行运行具有相关自动化的测试用例,而不是使用 Microsoft 测试管理器提供的用户界面。这使您可以从批处理文件自动开始运行。

Running Automated Tests from the Command LineUsing MSTest from the Command Line

下面是一个例子:

  1. 将 MSTEST.EXE 添加到您的路径,我的位于 C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE
  2. 打开cmd
  3. 查看可用命令列表Here,我将使用 /testcontainer 和 /test
  4. /testcontainer 指定 .dll 的位置
  5. /test 指定运行哪个单独的测试用例

我的最终命令是

mstest /testcontainer:"C:\Trunk\Project\bin\x86\Debug\TestProject.dll" /test:SmokeTest

【讨论】:

  • 仅链接的答案通常在 SO 上不受欢迎。您能否从那篇文章中提取一些相关引述,以便该答案对未来的访问者仍然有用,即使该链接已失效?
  • 当然,这完全没有问题。
  • 为 JeffX 的建议干杯,一旦我完成,我会试一试并在这里更新。
  • 我已将 Elena 标记为最佳答案,我也想为您投票以寻求帮助,但不幸的是,我没有足够的代表来执行此操作。
  • @Jonkers,我想你现在可以了。
猜你喜欢
  • 1970-01-01
  • 2015-05-28
  • 1970-01-01
  • 2019-02-27
  • 2012-10-28
  • 1970-01-01
  • 2016-06-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多