简体   繁体   English

如何告诉单元ms测试从子文件夹而不是root拾取CSV文件?

[英]How to tell unit ms test to pick up CSV file from a subfolder and not root?

I have written a unit test that uses a CSV file to read in data using the "DataSource" and "DeploymentItem" Attributes pointing to a csv file in a subfolder. 我编写了一个单元测试,该单元测试使用CSV文件来读取数据,该数据使用指向子文件夹中csv文件的“ DataSource”和“ DeploymentItem”属性。 However, when initially building the project the files are deployed in two different locations, one in the root and the other in the subfolder. 但是,在最初构建项目时,文件部署在两个不同的位置,一个位于根目录,另一个位于子文件夹中。 When I then later change the CSV file with different data and save and re-build the project (having the CSV property "Copy to Output" set to "Copy always", just to make sure), it copies the changes to the correct output subfolder but not the root. 然后,当我稍后使用不同的数据更改CSV文件并保存并重新生成项目时(将CSV属性“复制到输出”设置为“始终复制”,以确保进行复制),它将更改复制到正确的输出中子文件夹,但不是根文件夹。 This is what I would expect but the problem is, that when running the tests, it still reads in old data because it picks the CSV file up from the root and not the subfolder. 这是我所期望的,但问题是,在运行测试时,它仍会读取旧数据,因为它从根目录而不是子文件夹中拾取CSV文件。 So each time I have to go to the output folder and copy the CSV file from the subfolder into the root folder for the test to pick up the changes. 因此,每次我必须转到输出文件夹并将CSV文件从子文件夹复制到根文件夹中以进行测试,以进行更改。

Does anybody know how to fix this so the test looks in the subfolder to pick up the CSV file and not the root? 有人知道如何解决此问题,以便测试在子文件夹中查找以获取CSV文件而不是根文件吗?

[TestMethod()]
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", @"|DataDirectory|\\Data\MyData.csv", "MyData#csv", DataAccessMethod.Sequential)]
[DeploymentItem("MyApp\\MyTest\\MyTests.Tests\\Data\\MyData.csv")]  

You can add a TestSettings.settings file to your solution by selecting the solution and then clicking New... and then selecting TestSettings . 您可以通过选择解决方案,然后单击“ New... ,然后选择“ TestSettings将TestSettings.settings文件添加到解决方案中。 if you double click that fie it will give you a dialog with some options, one of those is Deployment . 如果您双击该文件,将显示一个对话框,其中包含一些选项,其中之一是Deployment If you enable that, you can add files and directories that get deployed to your test running directory. 如果启用该功能,则可以添加已部署到测试运行目录的文件和目录。

This link explains it a bit better. 链接对其进行了更好的说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM