What is the relative path root of DeploymentItemAt

2020-05-26 14:49发布

Using MSTest, what is the relative path root of the DeploymentItemAttribute.

3条回答
贪生不怕死
2楼-- · 2020-05-26 15:19

So I'm gonna add my experience.

So if you're using .testrunconfig file, that will trump (override) what I'm saying below.

I figured there were a few options.

Relative to the current .csproj

Relative to the .sln

Relative to the current .cs file

I finally got my build system working by using the "relative to the current .cs file" approach.

In my example, I needed a Xsd from a different project copied over for my UnitTest. But the type of file does not matter.

Example:

c:\myfolder\MyXsdProject\XSDs\MyCoolXsd.xsd

c:\myfolder\MyCsharpUnitTestProject\MySubFolder1\MySubFolder2\MyUnitTestClass.cs

namespace MyCsharpUnitTestProject.MySubFolder1.MySubFolder2
{

    [TestClass]
    [DeploymentItem(@"..\..\..\MyXsdProject\XSDs\MyCoolXsd.xsd")]

    public class MyUnitTestClass
    {
    }
}
查看更多
走好不送
3楼-- · 2020-05-26 15:20

Per the MSDN page...

Relative paths are relative to the RelativePathRoot setting found in the .testrunconfig file.

That setting is, by default, the Solution directory. So, if you have this project structure

SecretProject\
    ComponentA\
    ComponentA.Test\
        Resources\
            required.xml
        ComponentA.Test.csproj
        Tests.cs
    SecretProject.sln

And you want to deploy required.xml, you're going to create a DeploymentItemAttribute like this

[TestClass]
public class Tests
{
    [TestMethod]
    [DeploymentItem("ComponentA.Test\Resources\required.xml")]
    public void Test() 
    {

    }
}

It seems the file properties need to be set to 'Content' and 'Copy always' or 'Copy if newer'. There are advanced examples on this MSDN page.

查看更多
我命由我不由天
4楼-- · 2020-05-26 15:31

To assume that the RelativePathRoot default is the dir where your solution resides was not correct in my case, nor was RelativePathRoot defined within my .testrunconfig file. I found the RelativePathRoot default to be the /bin/debug dir for the solution.

Walking back from that point, then walking up to my file that I was attempting to deploy for the unit test worked fine.

查看更多
登录 后发表回答