MSTest部署项目仅在项目测试设置文件中存在时才起作用吗? [英] Do MSTest deployment items only work when present in the project test settings file?

查看:82
本文介绍了MSTest部署项目仅在项目测试设置文件中存在时才起作用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我不应该能够使用[TestClass][TestMethod]上的[DeploymentItem]属性添加部署项目,而不必创建/修改项目测试设置文件吗?我该怎么做?

(坦白说,我不理解需要单独的部署项目配置-为什么不对应作为部署项目的项目文件使用现有的复制到输出目录"设置?)

解决方案

好-此处的帖子帮助我弄清楚我需要做什么,而不必手动将项目添加到.testsettings文件中.

步骤1-启用"MS测试" DeploymentItem属性.

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

转到 TEST-> EDIT TEST SETTINGS->当前活动设置..例如::: Local(local.testsettings)

现在转到 DEPLOYMENT ,并确保选中 Enable Deployment . (默认情况下,它是关闭的.)

第2步-检查文件的属性

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

例如,...当前配置是 Debug (与Release相对).

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

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

专业提示:复制始终"也将起作用,但始终将源文件复制到目标文件上,即使它们相同.这就是为什么我更喜欢如果较新则复制"的原因,但是无论漂浮在船上的东西

好吧,女士们和男士们-还和我在一起吗? Wikid.

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

步骤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创建.

这就是我的输出文件夹结构. (打扰一下...)

瞧!我们有以编程方式提供的部署文件.

PRO TIP#2-如果您未在DeploymentItem属性中使用第二个字符串参数,则文件将被复制到当前MS测试的根OUT文件夹中.

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

现在,文件的路径相对于当前MS Test的OUT文件夹.因此,我明确地说要部署文件到名为Test Data的目录中...因此,当我想读入文件时,我需要确保在我的代码中正确引用了该文件. /p>

只需确认->就当前MS测试而言,该文件名的完整路径将转换为C:\lots of blah blah blah\My Solution\TestResults\PureKrome_PUREKROME-PC 2011-01-05 23_41_23\Out\Test Data ..之类的东西.

HTH.

现在有一张独角兽的照片,可以阅读很多:)

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

解决方案

Ok - 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.

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

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

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

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

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

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....

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 lets 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...)

and voila! we have deployment files, programatically.

PRO TIP #2 - if u 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 explicity 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.

HTH.

Now have a picture of a Unicorn, for reading so much :)

这篇关于MSTest部署项目仅在项目测试设置文件中存在时才起作用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆