问题
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?
回答1:
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 notAppX\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.
回答2:
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
.
回答3:
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.
来源:https://stackoverflow.com/questions/15497809/mstest-deploymentitem-attribute-unavailable-on-windows-store-test-project