Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在哪里部署用于单元测试的大型数据?_C#_Visual Studio 2010_Unit Testing - Fatal编程技术网

C# 在哪里部署用于单元测试的大型数据?

C# 在哪里部署用于单元测试的大型数据?,c#,visual-studio-2010,unit-testing,C#,Visual Studio 2010,Unit Testing,在测试配置中使用部署会在每次运行单元测试时创建一个副本,这非常耗时。数据是一组位图,只有在构建之后才可能更改 部署如此大的测试数据的惯例是什么?我刚刚在这里写了一篇关于文件依赖性和测试的博客文章 您正在执行的测试是集成测试,因为您要访问文件系统。我将使用文章中的每类示例方法来说明您试图实现的目标 帖子内容 免责声明 在许多情况下,开发人员测试需要使用需要在文件系统上的特定文件/文件集执行。许多“纯粹主义者”认为这是一个坏主意,你应该以一种不需要的方式“模仿”你的系统。在某些情况下,这可能是正确

在测试配置中使用部署会在每次运行单元测试时创建一个副本,这非常耗时。数据是一组位图,只有在构建之后才可能更改


部署如此大的测试数据的惯例是什么?

我刚刚在这里写了一篇关于文件依赖性和测试的博客文章

您正在执行的测试是集成测试,因为您要访问文件系统。我将使用文章中的每类示例方法来说明您试图实现的目标

帖子内容

免责声明

在许多情况下,开发人员测试需要使用需要在文件系统上的特定文件/文件集执行。许多“纯粹主义者”认为这是一个坏主意,你应该以一种不需要的方式“模仿”你的系统。在某些情况下,这可能是正确的,但我是一个“现实主义者”,并且理解这样做的复杂性要求可以而且很多时候远远超过仅利用文件系统进行测试的好处。然而,这确实将测试从真正的“单元”测试转移到了“集成”测试。我对此感到满意,因为我也相信集成测试比单元测试提供更多的价值。如果安装和运行方式正确–此测试可以通过MS build和命令行在visual studio中本地运行,或者在运行Team City等构建代理的构建服务器上运行

您可以在这里下载源代码:TestClass.zip

本试验的要求

测试运行程序(我喜欢使用TestDriven.Net,因为它可以让我在线执行和调试)。 一个文件类,它包装了允许您实现IDisposable的一些system.IO函数。这对于创建一个曾经超出范围的“沙盒”非常有用,它会删除所有使用的临时文件,以便自动完成清理(示例中附带了一个示例类)。 NUnit或MSTest。我还是喜欢NUnit。 使用选项 测试文件的使用要求将决定如何以及何时设置和清理(删除)

Per Test–每次对其运行测试时都会重新生成文件 每个测试类–每个测试类生成一次文件 在这两种情况下,FileSandBox类都用于为文件创建一个临时位置,以便在测试完成后将其删除

每类使用

[测试夹具] 公共类PerClass { 私有文件sandbox\u沙盒; 私有字符串_tempFileLocation

    public PerClass() {}

    /// <summary>
    /// Setup class - runs once per class
    /// </summary>
    [TestFixtureSetUp]
    public void SetupClass()
    {
        _sandbox = new FileSandbox();

        // Getting Temp file name to use
        _tempFileLocation = _sandbox.GetTempFileName("txt");
        // Get the current executing assembly (in this case it's the test dll)
        Assembly myassembly = Assembly.GetExecutingAssembly();
        // Get the stream (embedded resource) - be sure to wrap in a using block
        using (Stream stream = myassembly.GetManifestResourceStream("TestClass.TestFiles.TextFile1.txt"))
        {
            // In this case using an external method to write the stream to the file system
            _tempFileLocation = TestHelper.StreamToFile(stream, _tempFileLocation);
        }
    }

    /// <summary>
    /// Tear down class (cleanup)
    /// </summary>
    [TestFixtureTearDown]
    public void TearDownClass()
    {
        _sandbox.Dispose();
    }

    [Test, Description("Testing doing something with files on the filesystem")]
    public void MyFileSystemTest()
    {
        string[] lines = File.ReadAllLines(_tempFileLocation);
        Assert.IsTrue(lines.Length > 0);
    }
}
    public PerEachTest() { }

    /// <summary>
    /// Setup class - runs once per class
    /// </summary>
    [TestFixtureSetUp]
    public void SetupClass()
    {
        // NOOP
    }

    /// <summary>
    /// Tear down class (cleanup)
    /// </summary>
    [TestFixtureTearDown]
    public void TearDownClass()
    {
        // NOOP
    }

    [SetUp]
    public void Setup()
    {
        _sandbox = new FileSandbox();

        // Getting Temp file name to use
        _tempFileLocation = _sandbox.GetTempFileName("txt");
        // Get the current executing assembly (in this case it's the test dll)
        Assembly myassembly = Assembly.GetExecutingAssembly();
        // Get the stream (embedded resource) - be sure to wrap in a using block
        using (Stream stream = myassembly.GetManifestResourceStream("TestClass.TestFiles.TextFile1.txt"))
        {
            // In this case using an external method to write the stream to the file system
            _tempFileLocation = TestHelper.StreamToFile(stream, _tempFileLocation);
        }
    }

    [TearDown]
    public void Teardown()
    {
        _sandbox.Dispose();
    }

    [Test, Description("Testing doing something with files on the filesystem")]
    public void MyFileSystemTest()
    {
        string[] lines = File.ReadAllLines(_tempFileLocation);
        Assert.IsTrue(lines.Length > 0);
    }
}
public PerClass(){}
/// 
///安装类-每个类运行一次
/// 
[TestFixtureSetUp]
公共类()
{
_sandbox=新文件sandbox();
//正在获取要使用的临时文件名
_tempFileLocation=_sandbox.GetTempFileName(“txt”);
//获取当前正在执行的程序集(在本例中是测试dll)
Assembly myassembly=Assembly.getExecutionGassembly();
//获取流(嵌入式资源)-确保包装在using块中
使用(Stream Stream=myassembly.GetManifestResourceStream(“TestClass.TestFiles.TextFile1.txt”))
{
//在这种情况下,使用外部方法将流写入文件系统
_tempFileLocation=TestHelper.StreamToFile(流,_tempFileLocation);
}
}
/// 
///拆卸类(清理)
/// 
[测试固定器拆卸]
public void TearDownClass()
{
_sandbox.Dispose();
}
[测试,描述(“测试文件系统上的文件”)]
public void MyFileSystemTest()
{
string[]lines=File.ReadAllLines(_tempFileLocation);
Assert.IsTrue(lines.Length>0);
}
}
每次测试使用(选项1)

[TestFixture]
公开课考试
{
公共PerTest(){}
/// 
///安装类-每个类运行一次
/// 
[TestFixtureSetUp]
公共类()
{
//努普
}
/// 
///拆卸类(清理)
/// 
[测试固定器拆卸]
public void TearDownClass()
{
//努普
}
[测试,描述(“测试文件系统上的文件”)]
public void MyFileSystemTest()
{
使用(FileSandbox sandbox=newfilesandbox())
{
//正在获取要使用的临时文件名
字符串tempfile=sandbox.GetTempFileName(“txt”);
//获取当前正在执行的程序集(在本例中是测试dll)
Assembly myassembly=Assembly.getExecutionGassembly();
//获取流(嵌入式资源)-确保包装在using块中
使用(Stream Stream=myassembly.GetManifestResourceStream(“TestClass.TestFiles.TextFile1.txt”))
{
//在这种情况下,使用外部方法将流写入文件系统
tempfile=TestHelper.StreamToFile(流,tempfile);
string[]lines=File.ReadAllLines(tempfile);
Assert.IsTrue(lines.Length>0);
}
}
}
}
每次测试使用(选项2)

[测试夹具] 公开课考试 { 私有文件sandbox\u沙盒; 私有字符串_tempFileLocation

    public PerClass() {}

    /// <summary>
    /// Setup class - runs once per class
    /// </summary>
    [TestFixtureSetUp]
    public void SetupClass()
    {
        _sandbox = new FileSandbox();

        // Getting Temp file name to use
        _tempFileLocation = _sandbox.GetTempFileName("txt");
        // Get the current executing assembly (in this case it's the test dll)
        Assembly myassembly = Assembly.GetExecutingAssembly();
        // Get the stream (embedded resource) - be sure to wrap in a using block
        using (Stream stream = myassembly.GetManifestResourceStream("TestClass.TestFiles.TextFile1.txt"))
        {
            // In this case using an external method to write the stream to the file system
            _tempFileLocation = TestHelper.StreamToFile(stream, _tempFileLocation);
        }
    }

    /// <summary>
    /// Tear down class (cleanup)
    /// </summary>
    [TestFixtureTearDown]
    public void TearDownClass()
    {
        _sandbox.Dispose();
    }

    [Test, Description("Testing doing something with files on the filesystem")]
    public void MyFileSystemTest()
    {
        string[] lines = File.ReadAllLines(_tempFileLocation);
        Assert.IsTrue(lines.Length > 0);
    }
}
    public PerEachTest() { }

    /// <summary>
    /// Setup class - runs once per class
    /// </summary>
    [TestFixtureSetUp]
    public void SetupClass()
    {
        // NOOP
    }

    /// <summary>
    /// Tear down class (cleanup)
    /// </summary>
    [TestFixtureTearDown]
    public void TearDownClass()
    {
        // NOOP
    }

    [SetUp]
    public void Setup()
    {
        _sandbox = new FileSandbox();

        // Getting Temp file name to use
        _tempFileLocation = _sandbox.GetTempFileName("txt");
        // Get the current executing assembly (in this case it's the test dll)
        Assembly myassembly = Assembly.GetExecutingAssembly();
        // Get the stream (embedded resource) - be sure to wrap in a using block
        using (Stream stream = myassembly.GetManifestResourceStream("TestClass.TestFiles.TextFile1.txt"))
        {
            // In this case using an external method to write the stream to the file system
            _tempFileLocation = TestHelper.StreamToFile(stream, _tempFileLocation);
        }
    }

    [TearDown]
    public void Teardown()
    {
        _sandbox.Dispose();
    }

    [Test, Description("Testing doing something with files on the filesystem")]
    public void MyFileSystemTest()
    {
        string[] lines = File.ReadAllLines(_tempFileLocation);
        Assert.IsTrue(lines.Length > 0);
    }
}
public PerEachTest(){}
/// 
///安装类-每个类运行一次
/// 
[TestFixtureSetUp]
公共类()
{
//努普
}
/// 
///拆卸类(清理)
/// 
[测试固定器拆卸]
public void TearDownClass()
{
//努普
}
[设置]
公共作废设置()
{
_sandbox=新文件sandbox();
//正在获取要使用的临时文件名
_tempFileLocation=_sandbox.GetTempFileName(“txt”);
//获取当前正在执行的程序集(在本例中是测试dll)
Assembly myassembly=Assembly.getExecutionGassembly();
//获取流(嵌入的res)