如何在解决方案中运行所有测试

Say*_*jee 19 c# unit-testing mstest visual-studio-2013

如果我使用如下所述的/ testmetadata标志,我可以使用MSTest从命令行一次性在解决方案中运行我的所有测试:http://msdn.microsoft.com/en-us/library/ms182487. ASPX

我在Visual Studio 2013中运行SQL Server数据库单元测试,其中我似乎根本没有vsmdi文件,我也找不到添加方法的方法.我尝试创建一个testsettings文件,但是当我调用MSTest时它没有发现任何测试(显示"没有运行测试").

有没有办法让MSTest在VS2013解决方案中运行我的所有测试?

Say*_*jee 14

我想结束这个未解决的问题.我的目的是从Hudson CI服务器一次性运行所有测试,所以我编写了一个基本的控制台应用程序来查找和调用解决方案文件夹内所有DLL文件的MSTest.在以发布模式构建项目后执行此应用程序.

string execId = null;
string className = null;
string testName = null;
string testResult = null;
string resultLine = null;
List<string> results = new List<string>();
XmlDocument resultsDoc = new XmlDocument();
XmlNode executionNode = null;
XmlNode testMethodNode = null;

// Define the test instance settings
Process testInstance = null;
ProcessStartInfo testInfo = new ProcessStartInfo()
{
    UseShellExecute = false,
    CreateNoWindow = true,
};

// Fetch project list from the disk
List<string> excluded = ConfigurationManager.AppSettings["ExcludedProjects"].Split(',').ToList();
DirectoryInfo assemblyPath = new DirectoryInfo(Assembly.GetExecutingAssembly().Location);
DirectoryInfo[] directories = assemblyPath.Parent.Parent.Parent.Parent.GetDirectories();

// Create a test worklist
List<string> worklist = directories.Where(t => !excluded.Contains(t.Name))
                                    .Select(t => String.Format(ConfigurationManager.AppSettings["MSTestCommand"], t.FullName, t.Name))
                                    .ToList();

// Start test execution
Console.WriteLine("Starting Execution...");
Console.WriteLine();

Console.WriteLine("Results               Top Level Tests");
Console.WriteLine("-------               ---------------");

// Remove any existing run results
if (File.Exists("UnitTests.trx"))
{
    File.Delete("UnitTests.trx");
}

// Run each project in the worklist
foreach (string item in worklist)
{
    testInfo.FileName = item;
    testInstance = Process.Start(testInfo);
    testInstance.WaitForExit();

    if (File.Exists("UnitTests.trx"))
    {
        resultsDoc = new XmlDocument();
        resultsDoc.Load("UnitTests.trx");

        foreach (XmlNode result in resultsDoc.GetElementsByTagName("UnitTestResult"))
        {
            // Get the execution ID for the test
            execId = result.Attributes["executionId"].Value;

            // Find the execution and test method nodes
            executionNode = resultsDoc.GetElementsByTagName("Execution")
                                        .OfType<XmlNode>()
                                        .Where(n => n.Attributes["id"] != null && n.Attributes["id"].Value.Equals(execId))
                                        .First();

            testMethodNode = executionNode.ParentNode
                                            .ChildNodes
                                            .OfType<XmlNode>()
                                            .Where(n => n.Name.Equals("TestMethod"))
                                            .First();

            // Get the class name, test name and result
            className = testMethodNode.Attributes["className"].Value.Split(',')[0];
            testName = result.Attributes["testName"].Value;
            testResult = result.Attributes["outcome"].Value;
            resultLine = String.Format("{0}                {1}.{2}", testResult, className, testName);

            results.Add(resultLine);
            Console.WriteLine(resultLine);
        }

        File.Delete("UnitTests.trx");
    }
}

// Calculate passed / failed test case count
int passed = results.Where(r => r.StartsWith("Passed")).Count();
int failed = results.Where(r => r.StartsWith("Failed")).Count();

// Print the summary
Console.WriteLine();
Console.WriteLine("Summary");
Console.WriteLine("-------");
Console.WriteLine("Test Run {0}", failed > 0 ? "Failed." : "Passed.");
Console.WriteLine();

if (passed > 0)
    Console.WriteLine("\tPassed {0,7}", passed);

if (failed > 0)
    Console.WriteLine("\tFailed {0,7}", failed);

Console.WriteLine("\t--------------");
Console.WriteLine("\tTotal {0,8}", results.Count);

if (failed > 0)
    Environment.Exit(-1);
else
    Environment.Exit(0);
Run Code Online (Sandbox Code Playgroud)

我的App.config文件:

<appSettings>
    <add key="ExcludedProjects" value="UnitTests.Bootstrap,UnitTests.Utils" />
    <add key="MSTestCommand" value="&quot;c:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\MSTest.exe&quot; /testcontainer:&quot;{0}\bin\Release\{1}.dll&quot; /nologo /resultsfile:&quot;UnitTests.trx&quot;" />
</appSettings>
Run Code Online (Sandbox Code Playgroud)


jes*_*ing 5

MsTest是Visual Studio 2013的"已弃用"测试框架.它仍然用于某些类型的测试,但现在可以执行的许多其他测试都存在于新的Agile Test Runner中.现在,SQL数据库单元测试似乎仍属于"某些类型"的类别,需要通过MsTest.exe执行.

最简单的方法是使用/TestContainer命令行开关并为测试项目使用命名模式.这样,您可以使用特定的命名模式快速获取所有程序集,然后将它们提供给MsTest.简单的powershell命令可用于获取符合您的模式的所有文件,然后将它们提供给命令行.

vsmdi仍然可以在Visual Studio 2013中运行,但编辑器已从该工具中删除,而且不再有模板.所以它很难使用.这就是微软对VSDMI的看法:

警告 Visual Studio 2012中不再完全支持测试列表:

  • 您无法创建新的测试列表.
  • 您无法在Visual Studio中运行测试列表测试.
  • 如果从Visual Studio 2010升级并在解决方案中包含测试列表,则可以继续在Visual Studio中对其进行编辑.
  • 您可以使用命令行中的mstest.exe继续运行测试列表,如上所述.
  • 如果您在构建定义中使用了测试列表,则可以继续使用它.

基本上他们告诉你停止使用这种技术并使用TestCategory's'的组合来创建易于执行的测试组.

由于可以为测试容器添加多个参数,因此可以将它们分组为一个调用:

/testcontainer:[file name]        Load a file that contains tests. You can
                                  Specify this option more than once to
                                  load multiple test files.
                                  Examples:
                                    /testcontainer:mytestproject.dll
                                    /testcontainer:loadtest1.loadtest

MsTest /testcontainer:assemblyone.dll /testcontainer:assemblytwo.dll /testcontainer:assembly3.dll
Run Code Online (Sandbox Code Playgroud)

一次在多个程序集上运行MsTest.并且(还)不要使用XUnit .NET或NUnit,因为如果不切换到新的Agile测试运行器,这些不能合并到一个报告中.