本文介绍了NUnit的DeploymentItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MSTest的,如果我需要从其他项目的一些文件,我的测试,我可以指定DeploymentItem属性。有没有什么NUnit的相似?

In MsTest if I need some file from another project for my test, I can specify DeploymentItem attribute. Is there anything similar in NUnit?

推荐答案

您应该检查出对比的NUnit的和MSTest的的能力。

在这里接受的答案是误导性的。 NUnit的不提供[DeploymentItem()]属性在所有这就是@Idsa想在NUnit的同等解决方案。

The accepted answer here is misleading. NUnit does not offer the [DeploymentItem("")] attribute at all which is what @Idsa wanted an equivalent solution for in NUnit.

我的猜测是,这种属性会违反NUnit的范围为单位测试框架为要求的项目将被复制到输出运行测试意味着它有这个资源是可用的依赖了。

My guess is that this kind of attribute would violate the scope of NUnit as a "unit" testing framework as requiring an item to be copied to the output before running a test implies it has a dependency on this resource being available.

我使用的是自定义属性复制过的LocalDB实例运行对一些相当大的测试数据,我宁愿不与code,每次产生单位测试。

I'm using a custom attribute to copy over a localdb instance for running "unit" tests against some sizeable test data that I'd rather not generate with code everytime.

现在使用属性[DeploymentItem(有些/项目/文件)将在文件系统这一资源有效地复制到垃圾桶再每次测试的方法刷新我的源数据:

Now using the attribute [DeploymentItem("some/project/file")] will copy this resource from file system into the bin again effectively refreshing my source data per test method:

[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);
        }
    }
}

然后我们可以使用像这样:

Then we can use like so:

[Test]
[DeploymentItem("Data/localdb.mdf")]
public void Test_ReturnsTrue()
{
    Assert.IsTrue(true);
}

这篇关于NUnit的DeploymentItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 23:38