MSTest 部署项是否仅在项目测试设置文件中存在时才起作用?

发布于 2024-09-19 23:38:58 字数 327 浏览 2 评论 0原文

我似乎无法理解 MSTest 部署项目应该如何配置。我已经能够通过修改项目的测试设置文件使它们正常工作,但这不太理想——部署项目配置与单独的测试分开,并且文件路径似乎存储为绝对路径,除非文件是在解决方案文件夹下。

我是否不应该能够在 [TestClass][TestMethod] 上使用 [DeploymentItem] 属性添加部署项目,而无需必须创建/修改项目测试设置文件?我该如何实现这个目标?

(坦率地说,我不明白需要单独的部署项目配置 - 为什么不只使用现有的“复制到输出目录”设置来作为部署项目的项目文件?)

I can't seem to grasp how MSTest deployment items are supposed to be configured. I have been able to get them working properly by modifying the project's test settings file, but this is less then ideal -- the deployment item configuration is separated from individual tests, and the file paths appear to be stored as absolute paths unless the files are under the solution folder.

Am I not supposed to be able to add a deployment item using the [DeploymentItem] attribute on either a [TestClass] or [TestMethod] without having to create/modify a project test settings file? How do I accomplish this?

(Frankly, I don't understand the need for a separate deployment item configuration -- why not just use the existing 'Copy to Output Directory' settings for project files that should be deployment items?)

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

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

发布评论

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

评论(7

不美如何 2024-09-26 23:38:58

这篇文章帮助我弄清楚我需要做什么,而无需手动添加项目到 .testsettings 文件。

步骤 1 - 启用 MS Test DeploymentItem 属性。

首先,我们需要打开/启用 DeploymentItem 属性。

转到测试 ->编辑测试设置 ->当前活动设置 .. 例如:: Local (local.testsettings)

alt text

现在转到部署 并确保启用部署已勾选为“开”。 (默认情况下,它是关闭的)。

alt text

步骤 2 - 检查文件的属性

现在我们需要确定您希望在单元测试中使用的文件,设置为编译时复制到BIN目录。只有 BIN 目录中的文件才能在 MS Test 单元测试中使用。为什么?因为每次运行 MS 测试时,它都必须复制源代码...这意味着它会复制当前 BIN 目录文件(对于当前配置)。

例如...当前配置是调试(而不是发布)。
alt text

然后我添加我的文件...(记下项目中的文件夹结构)...

alt text

然后确保在编译项目时始终将此文件复制到 bin 目录。

alt text

专业提示:“始终复制”也可以,但始终将源文件复制到目标文件上。即使它们是相同的。这就是为什么我更喜欢“如果较新就复制”...但是无论什么都让你的船漂浮

好吧,女士们先生们——还在我身边吗?维基百科。

当我们编译时,该文件现在应该存在于 Bin 目录中...

alt text

步骤 3 - 现在使用 DeploymentItem好的

,现在我们终于可以在代码中使用 DeploymentItem 属性了。当我们这样做时,这会告诉 MSTest 将文件(从相对于 bin 目录的位置)复制到新的 MS Test 目录......

[TestMethod]
[DeploymentItem(@"Test Data\100LogEntries.txt", "Test Data")]
public void Parsing100LogFileEntriesReturnsANewParsedLogEntriesWith100Items()
{
    // Arrange.
    const string fileName = @"Test Data\100LogEntries.txt";
    ILogEntryService logEntryService = new PunkBusterLogEntryService();

    // Act.
    var parsedLogEntries = logEntryService.ParseLogFile(fileName, 0);

    // Assert.
    Assert.IsNotNull(parsedLogEntries);
    Assert.AreEqual(100, parsedLogEntries.LogEntries.Count);
    // Snipped the remaining asserts to cut back on wasting your time.
}

所以让我们分解一下......

[TestMethod]

我们都知道那是什么。

[DeploymentItem(@"Test Data\100LogEntries.txt", "Test Data")]

从 bin 目录开始,进入 Test Data 文件夹并将 100LogEntries.txt 文件复制到根目录中的目标文件夹 Test Data MS Test 输出目录是 MS Test 在运行每个测试时创建的。

这就是我的输出文件夹结构的样子(请原谅所有的混乱)。

alt text

瞧!我们以编程方式拥有部署文件。

专业提示 #2 - 如果您在 DeploymentItem 属性中不使用第二个字符串参数,则该文件将被复制到当前 MS 测试的根 OUT 文件夹中。

const string fileName = @"Test Data\100LogEntries.txt";

现在,该文件的路径是相对于当前 MS 测试的 OUT 文件夹的。因此,我明确表示将文件部署到名为 Test Data 的目录中...因此,当我想要时,我需要确保在代码中正确引用该文件读入文件。

只是为了确认 ->该文件名的完整路径被转换为类似 C:\lots of blah blah blah\My Solution\TestResults\PureKrome_PUREKROME-PC 2011-01-05 23_41_23\Out\Test Data ..当前的 MS 测试。

This post here helped me figure out what I needed to do WITHOUT having to manually add items to the .testsettings file.

Step 1 - Enable the MS Test DeploymentItem attribute.

First up, we need to turn on / enable the DeploymentItem attribute.

Go to TEST -> EDIT TEST SETTINGS -> Current Active settings .. eg :: Local (local.testsettings)

alt text

Now goto DEPLOYMENT and make sure Enable Deployment is ticked ON. (By default, it's off).

alt text

Step 2 - Check the File's properties

Now we need to make sure the file which you wish to use in the unit test, is setup to be copied to the BIN directory when you compile. Only files that are in the BIN directory can be used in an MS Test unit test. Why? Because each time an MS Test is ran, it has to make a copy of the sources ... and this means it makes a copy of the current BIN directory files (for the current Configuration).

For example... Current Configuration is Debug (as opposed to Release).
alt text

I then add my file ... (take note of the folder structure in the Project)...

alt text

and then make sure this file is ALWAYS copied over to the bin directory when the project is compiled.

alt text

PRO TIP: Copy Always will also work, but always copy the source file over the destination file .. even if they are identical. This is why I prefer Copy if Newer ... but whatever floats your boat

Ok ladies and gents - still with me? Wikid.

When we compile, the file should now exist in the Bin dir....

alt text

Step 3 - Now use the DeploymentItem attribute

Ok, now we can finally use the DeploymentItem attribute in our code. When we do this, this tells the MSTest to copy the file (from the location relative to the bin directory) to the new MS Test directory...

[TestMethod]
[DeploymentItem(@"Test Data\100LogEntries.txt", "Test Data")]
public void Parsing100LogFileEntriesReturnsANewParsedLogEntriesWith100Items()
{
    // Arrange.
    const string fileName = @"Test Data\100LogEntries.txt";
    ILogEntryService logEntryService = new PunkBusterLogEntryService();

    // Act.
    var parsedLogEntries = logEntryService.ParseLogFile(fileName, 0);

    // Assert.
    Assert.IsNotNull(parsedLogEntries);
    Assert.AreEqual(100, parsedLogEntries.LogEntries.Count);
    // Snipped the remaining asserts to cut back on wasting your time.
}

So let's break this down..

[TestMethod]

We all know what that is.

[DeploymentItem(@"Test Data\100LogEntries.txt", "Test Data")]

Starting in the bin directory, go into the Test Data folder and copy the 100LogEntries.txt file to a destination folder Test Data, in the root MS Test output directory which MS Test creates when each and every test is ran.

So this is what my output folder structure looks like (excuse all the mess).

alt text

and voila! we have deployment files, programmatically.

PRO TIP #2 - if you don't use a 2nd string argument in the DeploymentItem attribute, then the file will be copied to the root OUT folder, of the current MS Test.

const string fileName = @"Test Data\100LogEntries.txt";

Now the path to the file is relative to the OUT folder for the current MS Test. As such, I explicitly said to deploy the file into a directory called Test Data ... so I need to make sure I reference that correctly in my code when I want to read in the file.

Just to confirm -> the full path of that filename is translated to something like C:\lots of blah blah blah\My Solution\TestResults\PureKrome_PUREKROME-PC 2011-01-05 23_41_23\Out\Test Data .. for that current MS Test.

厌倦 2024-09-26 23:38:58

我想分享一下我遇到 MSTest 和部署项目问题的方法。如果您从“测试结果”窗口第二次或更多次调试/运行测试,它将使用上次运行的设置。但是,如果您从“测试视图”窗口调试/运行相同的测试,它将使用最新的设置。当我不断从“测试结果”窗口对同一测试启动调试时,我花了一个小时试图弄清楚为什么没有使用对 Local.testsettings 的更改。

这是“测试结果”窗口(在更改 Local.testsettings 后不要从此处[重新]启动测试):

测试结果Window

这是测试视图窗口(在更改 Local.testsettings 后从这里开始测试):

测试视图Window

我希望这可以避免将来有人头痛。

I thought I'd share a way I ran into problems with MSTest and deployment items. If you Debug/Run your test a 2nd time or more from the "Test Results" window, it uses the settings from a previous run. However, if you Debug/Run the same test from the "Test View" window, it uses the latest settings. I lost an hour to trying to figure out why changes to Local.testsettings weren't being used when I kept starting Debug on the same test from the "Test Results" window.

This is the Test Results window (do not [re]start tests from here after making changes to Local.testsettings):

The Test Results Window

And this is the Test View window (DO start tests from here after making changes to Local.testsettings):

The Test View Window

I hope this saves someone a headache in the future.

风启觞 2024-09-26 23:38:58

在 Visual Studio 2012 中,输出目录是工作目录,这意味着一般情况下不需要 DeploymentItem 属性(在这种情况下,您没有特定的每个测试或每个类部署项)。这意味着,如果您希望所有测试都使用一组文件,或者您对每个具有单独部署依赖项的 TestClass/TestMethod 不太挑剔,则不需要使用 DeploymentItem 属性。

您只需单击项目|显示所有文件,并将子文件夹和文件包含在 Visual Studio 中,并将“始终复制”或“如果较新则复制”属性添加到项目中,文件将被复制到层次结构完整的输出目录中。

从命令行运行 vstest.console.exe 时也是如此。有关详细信息,请参阅此处

更新

在某些情况下,默认目录不是输出目录。具体来说,当您选择“运行所有测试”时,默认路径将位于“TestResults\Deploy_...”下,这同样适用于使用“runsettings”文件或测试时使用 DeploymentItems

当您不使用 DeploymentItems 且满足以下条件时,输出目录将是默认目录: -

  • 在 Visual Studio 中右键单击测试并选择运行/调试,或者
  • 从命令运行使用 vstest.console.exe 的行。

In Visual Studio 2012 the output directory is the working directory which means that the DeploymentItem attribute isn't needed for the general case (where you don't have specific per-test or per-class deployment items). This means that if you don't need to use the DeploymentItem attribute if there is a set of files that you want all your tests to use or you are not too pernickety about each TestClass/TestMethod having separate deployment dependencies.

You can simply click Project | Show All Files and include the subfolders and files in Visual Studio with the 'Copy always' or 'Copy if newer' attribute to your project and the files will be copied to your output directory with hierarchy intact.

The same applies when running vstest.console.exe from the command line. See here for the details.

Update

There are cases where the default directory will not be the output directory. Specifically when you choose Run All Tests the default path would be under TestResults\Deploy_... The same applies when using a runsettings file or tests use DeploymentItems

The output directory will be the default directory when you are not using DeploymentItems and: -

  • You right click the test(s) in Visual Studio and choose to run / debug, or
  • You run from the command line using vstest.console.exe.
放赐 2024-09-26 23:38:58

如果您需要在各个测试用例中单独部署项目,请继续并在每个测试用例中使用 [DeploymentItem("string file path")] 属性。

If you need separate deployment item in individual test cases then please go ahead and use [DeploymentItem("string file path")] attribute in each test cases.

野生奥特曼 2024-09-26 23:38:58

对于我的情况,我添加了 DeploymentItem 属性,它不能立即生效。我必须关闭并重新打开解决方案,然后配置的 DeploymentItem 属性才会生效。

For my case, I add the DeploymentItem attribute, it can't take effect immediately. I have to close and re-open the solution, then the configured DeploymentItem attributes will take effect.

画中仙 2024-09-26 23:38:58

看起来这在 VS.NET 2012 中默认有效

Looks like this works by default in VS.NET 2012

在 VS 2012 中,您所需要做的就是复制 log4net.properties (或 log4net 的任何配置文件)文件(如果较新)。 (右键单击 log4net.properties 文件以显示属性并进行配置)

In VS 2012, All you need is to make the log4net.properties (or whatever config file for log4net ) file Copy if newer. ( right click log4net.properties file to bring up the properties and configure it )

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