运行 MSTest 单元测试 作为 TeamBuild 的一部分

发布于 2024-09-17 00:04:08 字数 3524 浏览 2 评论 0原文

我一直在寻找一个明确的网络资源来帮助解决这个问题,但到目前为止还没有运气。那么,如何将单元测试作为 TFS 构建的一部分来运行呢?我对此有很多问题,为了方便起见,我在这篇文章中对它们进行了编号。抱歉这篇文章的长度。

Visual Studio Team System 2008 安装在我的构建计算机上。

我有一个 TFSBuild.proj 文件,已将其设置为 true。为此,我

<ItemGroup>
  <TestContainer Include="'C:\MyBuild\Binaries\Debug\MyUnitTests.dll" />
</ItemGroup>

在解决方案中添加了 MyUnitTests 单元测试项目。

解决方案构建正确,但运行单元测试步骤失败并出现错误:

(CoreTestConfiguration 目标)->无法加载测试容器 'C : \MyBuild\Binaries\Debug\MyUnitTests.dll' 或其之一 依赖关系。警告详细信息:

后面没有实际的警告详细信息。我注意到文件路径似乎在消息中包含不正确的空格 - C : \MyBuild\Binaries\Debug\ 。实际路径是C:\MyBuild\etc

  1. 难道 OutDir 被设置为错误的路径?如何确定构建机器上的 OutDir 目标设置是什么?

为了确保这不仅仅是文件路径设置不正确,我尝试对 TestContainer 中的路径进行硬编码:

<ItemGroup>
  <TestContainer Include="'C:\MyBuild\Binaries\Debug\MyUnitTests.dll" />
</ItemGroup>

错误消息是相同的。我尝试过的其他事情包括尝试在 vsmdi 文件上运行测试:

<MetaDataFile Include="$(SolutionRoot)\MySolution\MySolution.vsmdi">
</MetaDataFile>

这次出现了不同的错误消息:

(CoreTestConfiguration 目标)-> MSBUILD:警告 MSB6006: “MSTest.exe”退出,代码为 1。

  1. 这里发生了什么?
  2. 为什么我在单元测试的错误消息中看不到任何详细信息?是因为我没有安装Studio 2008 Team System(开发者或测试者)版本吗?

我还想询问有关指定用于在构建中运行测试的部署项目的问题。从 Visual Studio 运行时,我的单元测试具有在 localtestrun.testrunco​​nfig 中指定的部署项。

  1. 当单元测试作为构建的一部分运行时,如何确保部署这些内容?

  2. 当构建运行 MSTest 时,如何将 MSTest 的输出捕获到文件中?也就是说,MSTest 是否使用命令行开关 /resultsfile:myfile.trx 运行?

  3. 是否有任何推荐的 Web 资源来了解如何将单元测试集成到构建中?

更新(1天后)

我已设法让单元测试作为构建的一部分运行,但存在问题。这是我到目前为止所发现的

  1. 我无法使用应该只运行项目中所有单元测试的语法来运行它。相反,我必须创建一个测试列表并通过引用 vsmdi 文件来运行。这不起作用 - 没有运行单元测试:

这很有效:

<ItemGroup>
  <MetaDataFile Include="$(SolutionRoot)\$(ReleaseName)\$(BranchName)\TheApplication\TheApplication.vsmdi">
    <TestList>TestsForTheBuild</TestList>
    <TestRunConfig>$(SolutionRoot)\$(ReleaseName)\$(BranchName)\TheApplication\LocalTestRun.testrunconfig</TestRunConfig>
  </MetaDataFile>
</ItemGroup>
  1. 在 Visual Studio IDE 中运行测试和使用 MSTest 执行测试有显着差异。问题在于项目引用 - MSTests 不会部署任何被单元测试项目视为“未使用的引用”的引用,而 Visual Studio IDE 会部署。

我正在测试的许多代码都使用 Unity 应用程序块动态实例化对象。为了实例化它们,定义它们的 dll 需要作为引用包含在项目中。因为对象仅在运行时通过反射实例化,所以编译器可以看到它们是未使用的引用。

当 Visual Studio 运行测试时,所有引用(无论是否未使用)都会被编译并复制到运行测试的 Out 目录。当 MSTest.exe 运行测试时,它不会部署任何“未使用的”引用。我花了几个小时才发现,当 MSTest 运行测试时,单元测试项目中项目引用的 dll 根本不在 Out 目录中。 。这是因为这个“未使用的参考”问题。

要解决此问题:将“未使用的引用”dll 作为部署项包含在 testrunco​​nfig 文件中。这将迫使它们被复制。

  1. 我发现的下一个问题是,当在同一版本中编译多个解决方案文件时,某些单元测试会失败。我发现这个问题有两种“风味”。 一个。单元测试根本不运行,除非相关的解决方案是要编译的解决方案列表中的最后一个解决方案文件。例如,如果这是我的“SolutionToBuild”ItemGroup,并且我有与 SolutionTwo 相关的单元测试,它们只是没有运行。

为了让它们开始运行,我必须将 SolutionTwo 移动到列表的底部。

b.通过构建中的三个解决方案,我的单元测试可以运行,但许多测试失败。如果我从构建中删除其他两个解决方案,则所有单元测试都会通过。尽管单元测试与其他两个解决方案无关。

我已经找到了问题 2 和 2 的答案。 3& 4 同上。 2. 2. 您在运行单元测试的构建输出中看不到任何详细信息。您只能看到运行是通过还是失败。 3. 要查看各个测试结果,您需要查看 MSTest 在构建运行时生成的 TestResults 目录和 .trx 文件。 4. testrunco​​nfig 文件中标记为部署项的任何内容都将被部署:当我通过 MSTest 中未使用的引用问题提出问题时,我感到很困惑。

I've been looking for a definitive web resource to help with this, but no luck so far. So, how do I run unit tests as part of a TFS build? I have a number of questions for this, so to make it easier I've numbered them in this post. Sorry for this post's length.

Visual Studio Team System 2008 is installed on my build machine.

I have a TFSBuild.proj file that I 've set to <RunTest>true</RunTest>. To go with this, I've added

<ItemGroup>
  <TestContainer Include="'C:\MyBuild\Binaries\Debug\MyUnitTests.dll" />
</ItemGroup>

MyUnitTests is the unit testing project in the solution.

The solution builds correctly, but the Run unit test step is failing with the error:

(CoreTestConfiguration target) -> Unable to load the test container
'C : \MyBuild\Binaries\Debug\MyUnitTests.dll' or one of its
dependencies. warning details:

No actual warnng details follow. I notice that the file path seems to include incorrect spaces - C : \MyBuild\Binaries\Debug\ in the message. The actual path is C:\MyBuild\etc.

  1. Could it be that OutDir is somehow set to the wrong path? How do I determine what the OutDir target is set to on my build machine?

To make sure this is not just a file path being incorrectly set, I tried just hard-coding the path in the TestContainer:

<ItemGroup>
  <TestContainer Include="'C:\MyBuild\Binaries\Debug\MyUnitTests.dll" />
</ItemGroup>

The error message was identical. Other things I've tried include trying to run tests off the vsmdi file:

<MetaDataFile Include="$(SolutionRoot)\MySolution\MySolution.vsmdi">
</MetaDataFile>

This time there was a different error message:

(CoreTestConfiguration target) -> MSBUILD : warning MSB6006:
"MSTest.exe" exited with code 1.

  1. What's happening here?
  2. And why am I not seeing any detail in the error messages for the unit tests? Is it because I don't have Studio 2008 Team System(Developer or Tester) edition installed?

I'd also like to ask about specifying deployment items for running tests in a build. When run from Visual Studio, my unit tests have deployment items specified in localtestrun.testrunconfig.

  1. How do I make sure these are deployed when the unit tests run as part of the build?

  2. How can I capture the output of MSTest into a file when the build runs it? That is, have MSTest run with the command-line switch /resultsfile:myfile.trx?

  3. Is there any recommended web resource to learn about integrating unit tests into a build?

Update (1 day later)

I've managed to get unit tests to run as part of the build, but with problems. Here's what I've found so far

  1. I was unable to get it to run using the syntax that is supposed to just run all unit tests in a project. Instead, I had to create a test list and run by referring to a vsmdi file. This didn't work - no unit tests ran:

This worked:

<ItemGroup>
  <MetaDataFile Include="$(SolutionRoot)\$(ReleaseName)\$(BranchName)\TheApplication\TheApplication.vsmdi">
    <TestList>TestsForTheBuild</TestList>
    <TestRunConfig>$(SolutionRoot)\$(ReleaseName)\$(BranchName)\TheApplication\LocalTestRun.testrunconfig</TestRunConfig>
  </MetaDataFile>
</ItemGroup>
  1. There were significant differences to running tests in the Visual Studio IDE and using MSTest to execute them. The problem was with project references - MSTests wouldn't deploy any references deemed by the unit test project to be 'Unused References', while the Visual Studio IDE would.

A lot of the code I'm testing instantiates objects dynamically using the Unity Application block. In order for them to be instantiated, the dll where they're defined needs to be included in the project as a reference. Because the objects are being instantiated via Reflection only at run time, as far as the compiler can see they're unused references.

When Visual Studio runs the tests, all references, whether unused or not, are compiled and copied to the Out directory that tests run from. When MSTest.exe runs the tests, it doesn't deploy any "unused" reference. It took me a couple of hours to discover dlls that were project references in the unit test project simply weren't in the Out directory when MSTest was running the tests. . It was because of this "unused reference" issue.

To solve this: include the 'unused references' dlls as deployment items in the testrunconfig file. This will force them to be copied.

  1. The next problem I've found is that some unit tests fail when there are multiple solution files being compiled in the same build. I found two "flavours" of this problem.
    a. Unit tests weren't running at all unless the solution the related to was the last solution file in the list of solutions to compile. For example, if this was my "SolutionToBuild" ItemGroup, and I had unit tests relating to SolutionTwo, they just didn't run.

To get them to start running, I had to move SolutionTwo to the botom of the list.

b. With three solutions in the build, my unit tests run, but many tests fail. If I remove the other two solutions from the build, all unit tests pass.This is despite the unit tests having nothing to do with the other two solutions.

I've figured out the answers to my questions 2 & 3 & 4 above. 2.
2. You don't see any detail in the build's output for unit tests running. You only see whether the run passed or failed.
3. To see individual test results, you need to see the TestResults directory and .trx file that MSTest generates when the build runs it.
4. Anything marked as a deployment item in your testrunconfig file will be deployed: I was confused when I asked the question by the unused references problem in MSTest.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晨曦÷微暖 2024-09-24 00:04:08

我发现三步消除很有用:

  1. 测试是否在 Visual Studio 开发环境中运行?

  2. 测试是否在本地 msbuild 中运行?

  3. 测试是否在构建服务器上的 msbuild 中运行?

我使用下面的 build.cmd 脚本在本地计算机上运行 msbuild,环境与 TFS 构建服务器将使用的环境非常匹配。

build.cmd 脚本

@setlocal

SET MSBUILDVER=v4.0.30319 
SET MSBUILDEXE=%FrameworkDir%%MSBUILDVER%\MSBuild.exe
SET builduri=local

%MSBUILDEXE% BuildDefinitions\TFSBuild.proj %*

注意:此脚本必须从 Visual Studio 2010 命令提示符窗口运行:“

开始”->“所有程序 ->微软 Visual Studio 2010 -> Visual Studio 工具 -> Visual Studio 命令提示符 (2010)

一般来说,在 TFSBuild.proj 中设置 true 应该会导致单元测试在构建中列出的任何解决方案中自动运行,但听起来这对您不起作用?

如果从命令 shell 将命令行选项 /p:RunTest=true 传递给 msbuild 会发生什么?

如有必要,您应该能够通过 TFSBuild.rsp 文件将自定义 TFS 构建命令行选项添加到服务器构建,但对于 RunTest 来说并不是必需的。

TFS 构建服务器应自动创建 .trx 结果文件 - 通过 Visual Studio 中的 Build Explorer 检查构建结果 - 查看测试结果将在 VS 中打开 .trx 文件。

不确定其他具体问题的解决方案,但我会尝试在本地命令窗口中运行构建,以更清楚地了解构建过程中发生的情况。
错误消息将显示为红色文本,警告消息将显示为黄色文本,许多其他中间构建步骤信息将显示为灰色文本。

有很多关于 TFS 构建主题的优秀博客文章 --
Aaron Hallberg 的博客 (blogs.msdn.com/b/aaronhallberg) 是一个不错的起点。此外,MSDN 有很多关于 TFS / MS Build 的好信息 - 例如: 在 Team Foundation Build 中运行构建

I find a three step elimination is useful:

  1. Do the tests run in Visual Studio dev environment?

  2. Do the tests run in msbuild locally?

  3. Do the tests run in msbuild on the build server?

I use the build.cmd script below to run msbuild on my local machine in an environment that very closely matches what will be used by the TFS build server.

build.cmd script

@setlocal

SET MSBUILDVER=v4.0.30319 
SET MSBUILDEXE=%FrameworkDir%%MSBUILDVER%\MSBuild.exe
SET builduri=local

%MSBUILDEXE% BuildDefinitions\TFSBuild.proj %*

NOTE: This script must be run from a Visual Studio 2010 command prompt window:

Start -> All Programs -> Microsoft Visual Studio 2010 -> Visual Studio Tools -> Visual Studio Command Prompt (2010)

In general, setting the true in TFSBuild.proj should cause unit tests to run automatically in any solutions listed in the build, but it sounds like that is not working for you?

What happens if you pass command line option /p:RunTest=true to msbuild from command shell?

You should be able to add custom TFS build command line options to the server build through the TFSBuild.rsp file if necessary, but should not really be necessary for RunTest.

TFS build server should automatically create a .trx results file -- check the build results through Build Explorer in Visual Studio -- the View Test Results will open the .trx file in VS.

Not sure about solutions to your other specific problems, but i would try running the build in a command window locally to get a clearer view of what is happening during the build.
Error messages will show up as red text, warning messages as yellow text, and lots of other intermediate build step info as grey text.

There are lots of good blog posts around about TFS build subjects --
Aaron Hallberg's blog (blogs.msdn.com/b/aaronhallberg) would be a good place to start. Also, MSDN has lots of good info on TFS / MS Build - for example: Running Builds in Team Foundation Build

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文