Problems with DeploymentItem attribute

2019-01-13 01:58发布

I'm currently maintaining an "old" system written in C#.net, removing some obsolete features and doing some refactoring. Thanks god, the previous guy wrote some unit tests (MSTests). I quite comfortable with JUnit tests, but didn't do yet much with MSTests.

The test methods have a DeploymentItem attribute, specifying a text file which is parsed by the business logic method that is being tested and a 2nd DeploymentItem where just a path has been specified containing a bunch of TIF files that have to be deployed too.

[TestMethod()]
[DeploymentItem(@"files\valid\valid_entries.txt")]
[DeploymentItem(@"files\tif\")]
public void ExistsTifTest()
{
   ...
}

The tests worked before, but now I had to change the names of the TIF files contained in the \files\tif directory. According to a rule, the TIF filenames have to match a certain pattern which is also checked by the ExistsTifTest() method. Now I had to change the filenames in order to adapt them to the new requirements and suddently the TIF files are no more being deployed as before.

Can someone give me a hint why this happens or what may be the cause? The same thing happens also if I add a new text-file say "my2ndTest.txt" beside the "valid_entries.txt" in the \files\valid\ directory with the according DeploymentItem attribute on the test method. The file doesn't get deployed?

I got the images now deployed by defining the deployment path directly in the testrunconfig, but I'd like to understand why these things happen or why for instance my new file "my2ndTest.txt" doesn't get deployed while the others do.

20条回答
姐就是有狂的资本
2楼-- · 2019-01-13 02:30

Since I always found the DeploymentItem attribute a mess, I do the deployment of such files by using the post-build script. - Make sure the files you wanna copy has the Copy Always property set. - Modify your test project post-build script to copy the files from build target folder(Bin\Debug) to the location where your test is expecting them.

查看更多
孤傲高冷的网名
3楼-- · 2019-01-13 02:33

This probably doesn't relate to your exact problem, but here's a couple of tips I found with the [DeploymentItem] attribute.

  1. Copy to output directory should be set to Copy Always.

It does NOT work when used with the [TestInitialize] attribute

[TestInitialize]
[DeploymentItem("test.xlsx")]
public void Setup()
{

It should be on your [TestMethod], e.g.

    [TestInitialize]
    public void Setup()
    {
        string spreadsheet = Path.GetFullPath("test.xlsx");
        Assert.IsTrue(File.Exists(spreadsheet));
        ...
    }

    [TestMethod]
    [DeploymentItem("test.xlsx")]
    public void ExcelQuestionParser_Reads_XmlElements()
    {
        ...
    }
查看更多
SAY GOODBYE
4楼-- · 2019-01-13 02:33

I had Deployment flag disabled first. But even after I enabled it, for some unknown reason nothing even target DLLs would still be copied. Accidentally I opened Test Run window and killed all the previous runs and magically I found all the DLLs and files I needed in the test folder the very next run... Very confusing.

查看更多
欢心
5楼-- · 2019-01-13 02:34

If you go into your .testrunconfig file and under deployment uncheck "Enable Deployment", the tests will run in their normal location, and everything will work like it does when running the app outside a unit test.

查看更多
ゆ 、 Hurt°
6楼-- · 2019-01-13 02:36

I have also faced similar problems but i found easy 3 step solution for this:

Assuming your folder structure looks like this: SolutionFolder\ TestProjectFolder\ SubFolder\

  1. Go to "Solutions Items/Local.testsettings" > "Deployment" > Check "Enable Deployment"
  2. If you are using VS2010, make sure any files you want to deploy have their "Copy To Output Folder" property set to "Copy Always" or "Copy if Newer"
  3. Attribute your TestMethod with either one of:
    • [DeploymentItem(@"TestProjectFolder\SubFolder")] to deploy all contents of <SubFolder> to the Test Run directory
    • [DeploymentItem(@"TestProjectFolder\SubFolder", "TargetFolder")] to deploy all contents of <SubFolder> to <TargetFolder> in the Test Run directory

One final note about MSTest (at least for VS2010):

If you want the <TargetFolder> to have the same name as the <SubFolder>, using [DeploymentItem(@"SubFolder", @"SubFolder")] will fail silently as the MSTest runner hits a silly edge case. This is why you should prefix the <SubFolder> with the <TestProjectFolder> as so: [DeploymentItem(@"TestProjectFolder\SubFolder", @"SubFolder")]

查看更多
走好不送
7楼-- · 2019-01-13 02:40

DeploymentItem is a bit of a mess.

Each file in your solution will have a "Copy To Output Folder" setting in VS.NET. You need this to be "Copy Always" (or similar) in order to get the files into the output folder.

Check that you've got this set for the new files. If you don't have this set then the files won't get copied to the output folder, and then they can't be deployed from the output folder to the folder where MSTest does it stuff.

Personally, if I have files that I need for my unit tests I've found that embedding those files as resources into an assembly, and having that assembly "unpack" itself during the tests is a more predictable way of doing things. YMMV.

note: These comments are based upon my experience with VS2010. Comments to my answer would suggest that this is not problem with VS2012. I still stand by comments that using embedded resources involves less "magic" and, for me, makes the "arrange" stage of my unit tests much more explicit.

查看更多
登录 后发表回答