C# 通过代码在MTM中执行测试

C# 通过代码在MTM中执行测试,c#,selenium,microsoft-test-manager,C#,Selenium,Microsoft Test Manager,我在MTM中有一组基于硒的测试,它们是单元测试的形式。如果我进入MTM并告诉他们跑步,我就能让他们跑得很好。我想知道的是,是否有某种API我可以用来启动这些 我们有一个用ASP.NET编写的仪表板,我们真正想要的是,如果我们能有一个播放按钮来执行测试计划。我真的不确定在这方面应该寻找什么,甚至根本不知道这是否可能 一个可能的解决方案是我构建一个测试工具,并使用反射来运行DLL,但这会很麻烦。您可以使用MSTEST.exe,而不是使用Microsoft测试管理器提供的用户界面,从命令行自动运行相关

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

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


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

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

看到和

下面是一个如何执行此操作的示例:

将MSTEST.EXE添加到您的路径中,我的路径位于C:\Program Files x86\Microsoft Visual Studio 12.0\Common7\IDE 打开命令 查看可用命令列表,我将使用/testcontainer和/test /testcontainer指定.dll所在的位置 /测试指定要运行的单个测试用例 我最后的命令是

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

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

看到和

下面是一个如何执行此操作的示例:

将MSTEST.EXE添加到您的路径中,我的路径位于C:\Program Files x86\Microsoft Visual Studio 12.0\Common7\IDE 打开命令 查看可用命令列表,我将使用/testcontainer和/test /testcontainer指定.dll所在的位置 /测试指定要运行的单个测试用例 我最后的命令是

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

您可以使用TFSAPI执行您在MTM中管理的测试。 不幸的是,这个API在MSDN上的文档记录非常糟糕,这真是一个遗憾。。。 我的提示:在MSDN页面上更改为Visual Studio 2012版本,您将获得更多文档,尽管文档太少,但总比什么都没有好

下面是一个示例,说明如何在您选择的测试环境中,针对特定测试配置,在您的测试计划中运行属于特定测试套件的所有测试用例:

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();

您可以使用TFSAPI执行您在MTM中管理的测试。 不幸的是,这个API在MSDN上的文档记录非常糟糕,这真是一个遗憾。。。 我的提示:在MSDN页面上更改为Visual Studio 2012版本,您将获得更多文档,尽管文档太少,但总比什么都没有好

下面是一个示例,说明如何在您选择的测试环境中,针对特定测试配置,在您的测试计划中运行属于特定测试套件的所有测试用例:

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();

只有链接的答案通常不被接受。你能从那篇文章中抽出一些相关的引语,这样这个答案对未来的访问者仍然有用,即使链接已经失效了吗,这没问题。为建议干杯JeffX我会尝试一下,完成后在这里更新。我已将Elena标记为最佳答案我也想投票给你,感谢你的帮助,但不幸的是我没有足够的代表来做这件事。@Jonkers,我想你现在可以了。只链接的答案通常不被接受。你能从那篇文章中抽出一些相关的引语,这样这个答案对未来的访问者仍然有用,即使链接已经失效了吗,这没问题。为这个建议干杯JeffX,我会尝试一下,一旦我完成了,我会在这里更新。我已经把Elena标记为最佳答案我也想投票给你,感谢你的帮助,但不幸的是我没有足够的代表来做这件事。@Jonkers,我想你现在可以了。Elena,还有一个问题,这会在MTM环境中设置的一台机器上启动它们吗?或者这会在本地计算机上启动它们?这段代码会在MTM环境中启动它们。该示例包含call project.TestenEnvironments.Find。。。然后使用设置测试运行属性时获得的环境:testRun.Controller=TestenEnvironment.ControllerName。这是指定应该在哪个MTM环境测试上运行的方法。只是更新一下。代码工作得很好!我遇到的唯一问题是行:ITestPointCollection testpoints=plan.querytestpoints从测试点中选择*,其中SuiteId=+suite.Id+和ConfigurationId=+testConfiguration.Id;它没有返回任何内容,因此我必须使用以下命令:ITestPointCollection testpoints=plan.QueryTestPointsstring.FormatSELECT*从测试点开始,其中RecursiveSuiteID={0},plan.RootSuite.Id;再次感谢埃琳娜!Elena,还有一个问题,这会在MTM环境中设置的一台机器上启动它们吗?或者这会在本地计算机上启动它们?这段代码会在MTM环境中启动它们。该示例包含call project.TestenEnvironments.Find。。。然后使用
设置测试运行属性时获得的环境:testRun.Controller=testEnvironment.ControllerName。这是指定应该在哪个MTM环境测试上运行的方法。只是更新一下。代码工作得很好!我遇到的唯一问题是行:ITestPointCollection testpoints=plan.querytestpoints从测试点中选择*,其中SuiteId=+suite.Id+和ConfigurationId=+testConfiguration.Id;它没有返回任何内容,因此我必须使用以下命令:ITestPointCollection testpoints=plan.QueryTestPointsstring.FormatSELECT*从测试点开始,其中RecursiveSuiteID={0},plan.RootSuite.Id;再次感谢埃琳娜!我创建了一个类似的工具。也许你可以试一试。谢谢分享拉斐尔。我一定要看一看。我创建了一个类似的工具。也许你可以试一试。谢谢分享拉斐尔。我一定要看一看。