[英]MSTest - DeploymentItem attribute unavailable on windows store test project
就像標題所說的那樣-我找不到在Windows Store測試項目中包含文件的方法。 (標准的.NET測試項目可以正常工作)
右鍵單擊解決方案並執行:添加新項目-> C#-> Windows應用商店->單元測試庫(Windows應用商店應用)
您將獲得以下樣板,在該樣板上添加了DeploymentItem屬性:
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()
{
}
}
}
那我想念什么呢? 是否根本不可能在Windows存儲單元測試中包含數據,還是需要其他方法?
我一直在部署數據文件的方式是使用構建后副本。 我的數據位於項目“ TestData”下的項目中,構建后將其復制到輸出中。
在我的測試項目的屬性的“構建事件”選項卡中設置了以下幾行:
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\"
一些注意事項:
TestData
下的數據文件,而不是AppX\\TestData
。 DeploymentItem包含在您沒有引用的Microsoft.VisualStudio.TestTools.UnitTesting
命名空間中。
您必須添加對Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll
的引用。
使用@ chue-x構建事件,然后使用[TestInitialize]類似:
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);
}
現在,TestData可用於被測試的應用程序。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.