MSTest - DeploymentItem attribute unavailable on w

2019-07-28 18:05发布

As the title says - I can't find a way of including a file with a windows store test project. (A standard .NET test project works fine)

Right click on the solution and execute : Add new project -> C# -> Windows Store -> Unit test library (Windows store apps)

You get this boilerplate, to which I have added the DeploymentItem attribute:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;

namespace UnitTestLibrary1
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        // this does not compile; DeploymentItem attribute not found
        [DeploymentItem("wibble.txt")]
        public void TestMethod1()
        {
        }
    }
}

So what am I missing? Is it simply not possible to include data in windows store unit tests, or do I need a different approach?

3条回答
SAY GOODBYE
2楼-- · 2019-07-28 19:04

Use @chue-x Build Events, then [TestInitialize] something like:

        var site = "TestData";
        var appx = Package.Current.InstalledLocation;
        var reposTestFolder = await appx.GetFolderAsync(site);
        var testFiles = await reposTestFolder.GetFilesAsync();

        var localfolder = ApplicationData.Current.LocalFolder;
        var reposFolder = await localfolder.CreateFolderAsync(site, CreationCollisionOption.OpenIfExists);
        foreach (var file in testFiles)
        {
            await file.CopyAsync(reposFolder);
        }

Now the TestData is available to the app under test.

查看更多
\"骚年 ilove
3楼-- · 2019-07-28 19:08

The way I have been deploying data files is by using a post-build copy. I have my data in my project under a directory called "TestData", which gets copied to the output after a build.

The following lines are set in the "Build Events" tab of the my test project's properties:

if not exist "$(TargetDir)AppX" mkdir "$(TargetDir)AppX"
if not exist "$(TargetDir)AppX\TestData" mkdir "$(TargetDir)AppX\TestData"
copy /Y "$(TargetDir)TestData\*.*" "$(TargetDir)AppX\TestData\"

A couple of notes:

  • my unit tests refer to the data files under the directory TestData, and not AppX\TestData.
  • the post-build action above will only copy files that have been set in your project as 1) a content file and 2) as being copied to output.
查看更多
来,给爷笑一个
4楼-- · 2019-07-28 19:08

DeploymentItem is contained at Microsoft.VisualStudio.TestTools.UnitTesting namespace in which you are not having a reference.

You have to add a reference to Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll.

查看更多
登录 后发表回答