gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 04:43:39 25 4
gpt4 key购买 nike

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

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

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

最佳答案

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

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

我的 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>

关于c# - 如何在解决方案中运行所有测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25952638/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com