问题描述
我似乎无法理解应该如何配置 MSTest 部署项目.通过修改项目的测试设置文件,我已经能够让它们正常工作,但这不太理想——部署项配置与单个测试分开,文件路径似乎存储为绝对路径,除非文件是在解决方案文件夹下.
我不应该能够使用 [DeploymentItem]
属性在 [TestClass]
或 [TestMethod]
上添加部署项目吗?代码> 而不必创建/修改项目测试设置文件?我该如何实现?
(坦率地说,我不明白需要单独的部署项目配置——为什么不直接使用现有的复制到输出目录"设置应该是部署项目的项目文件?)
em>
好的女士们先生们 - 还和我在一起吗?维基百科.
当我们编译时,文件现在应该存在于 Bin 目录中....
第 3 步 - 现在使用 DeploymentItem 属性
好的,现在我们终于可以在我们的代码中使用 DeploymentItem
属性了.当我们这样做时,这会告诉 MSTest 将文件(从相对于 bin 目录的位置)复制到新的 MS Test 目录...
[测试方法][DeploymentItem(@Test Data100LogEntries.txt", Test Data")]public void Parsing100LogFileEntriesReturnsANewParsedLogEntriesWith100Items(){//安排.const string fileName = @Test Data100LogEntries.txt";ILogEntryService logEntryService = new PunkBusterLogEntryService();//行为.var parsedLogEntries = logEntryService.ParseLogFile(fileName, 0);//断言.Assert.IsNotNull(parsedLogEntries);Assert.AreEqual(100, parsedLogEntries.LogEntries.Count);//剪断剩余的断言以减少浪费时间.}
那么让我们分解一下..
[测试方法]
我们都知道那是什么.
[DeploymentItem(@"Test Data100LogEntries.txt", "Test Data")]
从bin目录开始,进入Test Data
文件夹,将100LogEntries.txt
文件复制到目标文件夹Test Data
,在 MS Test 在运行每个测试时创建的根 MS Test 输出目录中.
这就是我的输出文件夹结构的样子(请原谅所有的混乱).
瞧!我们有部署文件,以编程方式.
专业提示 #2 - 如果您不在 DeploymentItem
属性中使用第二个字符串参数,则该文件将被复制到当前 MS 测试的根 OUT 文件夹中.
const string fileName = @Test Data100LogEntries.txt";
现在文件的路径相对于当前 MS 测试的 OUT
文件夹.因此,我明确表示要将文件部署到一个名为 Test Data
的目录中......所以我需要确保在我想在代码中正确引用它时读入文件.
只是为了确认 ->该文件名的完整路径被转换为类似 C:lots of blah blah blahMy SolutionTestResultsPureKrome_PUREKROME-PC 2011-01-05 23_41_23OutTest Data
..当前的 MS 测试.
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?)
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)
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 Data100LogEntries.txt", "Test Data")]
public void Parsing100LogFileEntriesReturnsANewParsedLogEntriesWith100Items()
{
// Arrange.
const string fileName = @"Test Data100LogEntries.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 Data100LogEntries.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, 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 Data100LogEntries.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 blahMy SolutionTestResultsPureKrome_PUREKROME-PC 2011-01-05 23_41_23OutTest Data
.. for that current MS Test.
这篇关于MSTest 部署项是否仅在项目测试设置文件中存在时才有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!