通过代码在MTM中执行测试

时间:2014-05-30 12:14:34

标签: c# selenium microsoft-test-manager

我在MTM中有一组基于Selenium的测试,它们是单元测试的形式。如果我进入MTM并告诉他们跑步,我能够很好地运行它们。我想知道的是,如果我可以使用某种API来解决这些问题吗?

我们有一个用ASP.NET编写的仪表板,我们真正想要的是我们是否可以像执行测试计划的播放按钮一样。我不确定在这方面要搜索什么,或者根本不可能。

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

2 个答案:

答案 0 :(得分:3)

您可以使用 TFS API 执行您在 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();

答案 1 :(得分:2)

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

请参阅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指定要运行的单个测试用例
  6. 我的最后一个命令是

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