在MsTest中,如果我需要来自另一个项目的某个文件进行测试,我可以指定DeploymentItem属性.NUnit中有类似的东西吗?
Mat*_*hew 24
您应该查看另一个与NUnit和MSTest功能形成对比的线程.
这里接受的答案是误导性的.NUnit根本不提供[DeploymentItem("")]属性,这是@Idsa想要在NUnit中使用的等效解决方案.
我的猜测是,这种属性会违反NUnit作为"单元"测试框架的范围,因为在运行测试之前需要将项目复制到输出,这意味着它依赖于此资源可用.
我正在使用自定义属性来复制localdb实例,以便针对一些相当大的测试数据运行"单元"测试,而我每次都不会使用代码生成这些测试数据.
现在使用属性[DeploymentItem("some/project/file")]将该资源从文件系统复制到bin中,再次按照测试方法有效刷新源数据:
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Struct,
AllowMultiple = false,
Inherited = false)]
public class DeploymentItem : System.Attribute {
private readonly string _itemPath;
private readonly string _filePath;
private readonly string _binFolderPath;
private readonly string _itemPathInBin;
private readonly DirectoryInfo _environmentDir;
private readonly Uri _itemPathUri;
private readonly Uri _itemPathInBinUri;
public DeploymentItem(string fileProjectRelativePath) {
_filePath = fileProjectRelativePath.Replace("/", @"\");
_environmentDir = new DirectoryInfo(Environment.CurrentDirectory);
_itemPathUri = new Uri(Path.Combine(_environmentDir.Parent.Parent.FullName
, _filePath));
_itemPath = _itemPathUri.LocalPath;
_binFolderPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
_itemPathInBinUri = new Uri(Path.Combine(_binFolderPath, _filePath));
_itemPathInBin = _itemPathInBinUri.LocalPath;
if (File.Exists(_itemPathInBin)) {
File.Delete(_itemPathInBin);
}
if (File.Exists(_itemPath)) {
File.Copy(_itemPath, _itemPathInBin);
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后我们可以像这样使用:
[Test]
[DeploymentItem("Data/localdb.mdf")]
public void Test_ReturnsTrue()
{
Assert.IsTrue(true);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
10824 次 |
最近记录: |