编写单元测试:如何以编程方式获取包含测试文件的文件夹

发布于 2024-10-08 22:02:05 字数 1166 浏览 0 评论 0原文

我正在 Visual Studio 2010 中编写单元测试。
为了测试某些功能,我添加了一个包含测试文件的文件夹。
我需要以编程方式获取此文件夹,而无需字符串中的硬路径。

该文件夹包含在 /TestFiles 中,

我尝试使用 AppDomain.CurrentDomain.BaseDirectory。 仅当我使用 resharper 运行单元测试时,这才有效。
结果是 /bin/debug 所以我可以轻松地转到 TestFiles。

如果我使用 Visual Studio 运行测试,则 BaseDirectory 为:
\TestResults\<用户名>_;\Out

我已将解决方案文件移动到另一个文件夹。所以我的项目与解决方案文件不在同一文件夹中。
示例:
= C:\SolutionFiles
<项目目录> = C:\Projects\MyProject

有人可以告诉我如何在不使用硬编码字符串的情况下获取测试文件的路径吗?

编辑
我还没有找到解决办法。
Visual Studio 正在使用另一个构建文件夹进行测试。 因此,通常构建到 bin 文件夹中的所有内容都将构建到另一个文件夹中以进行测试。

我的临时解决方案
我在我的测试项目中添加了一个 App.config 文件。 在此配置文件中,我添加了一个设置,其中包含测试文件所需的路径:

<appSettings>
  <add key="TestFiles" value="C:\Projects\MyProject\TestFiles"/>
</appSettings>

I am writing unit tests in visual studio 2010.
For test some functionality, I have added a folder with testfiles.
I need to get this folder programmatically without a hard path in a string.

The folder contains in <projectDirectory>/TestFiles

I have tried to use AppDomain.CurrentDomain.BaseDirectory.
This will only work if I run my unit tests with resharper.
result is <projectDirectory>/bin/debug so I can easily go to TestFiles.

If I am running test with visual studio, the BaseDirectory is:
<sameFolderAsSolutionFile>\TestResults\<username>_<pcname> <datatime>\Out

I have moved my solution file to another folder. So my projects aren't in the same folder as my solution file.
Example:
<sameFolderAsSolutionFile> = C:\SolutionFiles
<projectDirectory> = C:\Projects\MyProject

Can someone tell me how to get the path to my test-files without using a hardcoded string?

EDIT
I haven't found a solution yet.
Visual Studio is using another build folder for testing.
So everything what is normally builded into the bin folder will be builded into another folder for the test.

MY TEMP-SOLUTION
I have added a App.config file in my test project.
In this configuration file I have added a setting with the required path to the test files:

<appSettings>
  <add key="TestFiles" value="C:\Projects\MyProject\TestFiles"/>
</appSettings>

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

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

发布评论

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

评论(6

冰雪梦之恋 2024-10-15 22:02:05

您可以使用 DeploymentItem 属性。

You can copy this folder into the deployment folder of the unit tests with the DeploymentItem attribute.

蒗幽 2024-10-15 22:02:05

即使问题已经得到解答,我也想贡献一个对我有用的解决方案,而其他人却没有。
您需要将文件复制到测试项目的 bin 目录中,在其属性中标记该文件复制到输出目录=“始终复制”,然后在测试中您可以使用以下代码读取该文件:

var baseDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var testFile = Path.Combine(baseDir, "TestFiles", "file.txt");

var file = File.OpenRead(testFile))

Even the question is already answered, I want to contribute with a solution that worked for me, as did not the others.
You will need to copy the file to the bin directory of the test project marking the file in its properties Copy to ouput directory = "Copy always", then in the test you can read the file with this code:

var baseDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var testFile = Path.Combine(baseDir, "TestFiles", "file.txt");

var file = File.OpenRead(testFile))
七禾 2024-10-15 22:02:05

最可靠的方法是使用 TestContext。该对象具有部署路径、结果路径等的全部属性。请参阅 TestContext 属性

The most reliable way is to use TestContext. This object has whole bunch of properties for deployment path, results path etc. See TestContext Properties.

奢欲 2024-10-15 22:02:05

您可以使用 Path.Combine(Assembly.GetExecutingAssembly().Location,"TestFiles") 访问该文件夹,而无需进行任何硬编码。您必须确保将文件夹和文件添加到测试项目中,并正确设置其 CopyToOutput 目录。

You can use Path.Combine(Assembly.GetExecutingAssembly().Location,"TestFiles") to get to that Folder without hard coding anything. You have to make sure that you added the folder and files to your test project and have set its CopyToOutput directory appropriately.

雨巷深深 2024-10-15 22:02:05

我们这样做:

var assembly = Assembly.GetAssembly(this.GetType());
var codebase = assembly.CodeBase.Replace("file:///", "");
var baseDir = Path.GetDirectoryName(codebase);

这个 C# 代码片段来自单元测试设置例程,并检索单元测试代码的目录。从这里您可以导航...
我不认为它很优雅......但是:)

We do it this way:

var assembly = Assembly.GetAssembly(this.GetType());
var codebase = assembly.CodeBase.Replace("file:///", "");
var baseDir = Path.GetDirectoryName(codebase);

This C# code snippet comes from the unit test setup routine and retrieves the directory of the unit test code. From here you can navigate ...
I do not think its very elegant...but :)

倒带 2024-10-15 22:02:05

万一像我这样的人出现,如果您使用.testsettings文件并且类上的DeploymentItem属性不起作用甚至尽管您已将文件设置为内​​容并始终复制,但这是因为您的 .testsettings 文件中已经有 Deployment 部分,或者您需要在 .testsettings 文件中使用 DeploymentItem。我们现在的样子是这样的:

<Deployment>
    <DeploymentItem filename="Tests\Unit Tests\ConnectionStrings.config" />
    <DeploymentItem filename="Tests\Unit Tests\<project dir>\bin\Debug\TestFiles\" outputDirectory="TestFiles" />
</Deployment>

一个处理一个文件,另一个处理一个目录中的 13 个文件。请注意目录“文件名”的尾部斜杠!

解决方案和更多信息位于:

MsTest DeploymentItem OutputDirectory in testsettings

In case someone like me comes along, if you're using a .testsettings file and the DeploymentItem attribute on the class doesn't work even though you've set your files as Content and to Always Copy, it's because either you already have a Deployment section in your .testsettings file or you need to use DeploymentItem in the .testsettings file. Here's how ours looks now:

<Deployment>
    <DeploymentItem filename="Tests\Unit Tests\ConnectionStrings.config" />
    <DeploymentItem filename="Tests\Unit Tests\<project dir>\bin\Debug\TestFiles\" outputDirectory="TestFiles" />
</Deployment>

One does a file, the other does 13 files in a directory. Note the trailing slash for the directory's "filename"!

Solution and more info was found at:

MsTest DeploymentItem OutputDirectory in testsettings

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