简体   繁体   English

如何获取测试项目的构建目录?

[英]How to get build directory of a test project?

I have a test project in which I needs to load an XLSX file. 我有一个测试项目,我需要加载一个XLSX文件。 For that, I added a file with copy-always so that it ends up in the build directory, but all of the following return a wrong path: 为此,我添加了一个带有copy-always的文件,以便它最终出现在build目录中,但以下所有内容都返回了错误的路径:

  1. System.Reflection.Assembly.GetAssembly(typeof(testclass)).Location;
  2. AppDomain.CurrentDomain.BaseDirectory
  3. Directory.GetCurrentDirectory();

They all give me: 他们都给了我:

"C:\\\\Users\\\\username\\\\Documents\\\\visual-studio-projecten\\\\projectname\\\\TestResults\\\\username_ICT003 2012-06-20 12_07_06\\\\Out"

and I need 我需要

"C:\\\\Users\\\\username\\\\Documents\\\\visual-studio-projecten\\\\projectname\\\\TestProject\\\\bin\\\\Debug\\\\SupportFiles\\\\"

How do I accomplish that? 我该如何做到这一点?

Use the DeploymentItemAttribute attribute. 使用DeploymentItemAttribute属性。 To quote MSDN: 引用MSDN:

This attribute identifies files and directories that contain files that are used by the deployed test to run. 此属性标识包含要部署的测试运行的文件的文件和目录。 The test engine makes a copy of the deployment items and places them in test deployment directory based upon the OutputDirectory specified or the default directory. 测试引擎制作部署项的副本,并根据指定的OutputDirectory或缺省目录将它们放在测试部署目录中。

For example: 例如:

[TestClass]
public class MyUnitTest
{
    [TestMethod()]
    [DeploymentItem("myfile.txt")]
    public void MyTestMethod()
    {          
        string file = "myfile.txt";           
        Assert.IsTrue(File.Exists(file), "deployment failed: " + file +
            " did not get deployed");
    }
}

Of course assuming you are using MSTest as your testing framework. 当然假设您使用MSTest作为测试框架。

Try this: 尝试这个:

string dir = Path.Combine(
    Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
    "SupportFiles");

Don't use Directory.GetCurrentDirectory() because current directory could not be your exe dir and may change during program execution. 不要使用Directory.GetCurrentDirectory()因为当前目录不能是您的exe目录,并且可能在程序执行期间更改。

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. 如果你有像我一样的人,如果你正在使用.testsettings文件并且类DeploymentItem属性 不起作用,即使你已经将文件设置为内​​容并且总是复制,那是因为你已经有了.testsettings文件中的部署部分,或者您需要在.testsettings文件中使用DeploymentItem。 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. 一个人做一个文件,另一个做一个目录中的13个文件。 Note the trailing slash for the directory's "filename"! 注意目录的“文件名”的尾部斜杠!

Solution and more info was found at: 解决方案和更多信息发现于:

MsTest DeploymentItem OutputDirectory in testsettings 测试集中的MsTest DeploymentItem OutputDirectory

Switch to xUnit or NUnit . 切换到xUnitNUnit

Then both option 2., 3. and Environment.CurrentDirectory works as needed, returning the build-output-diretory. 然后选项2.,3和Environment.CurrentDirectory可以根据需要工作,返回build-output-diretory。 Remove "bin\\Debug" or "bin\\Release" and files can be left at 'Do not copy'. 删除“bin \\ Debug”或“bin \\ Release”,文件可以保留为“不要复制”。

Also see the GetFileFromMethod -method and usage in ElasticSearch for a nice way to have one file per test. 另请参阅ElasticSearch中的GetFileFromMethod -method和用法 ,以获得每次测试一个文件的好方法。

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

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