Skip to main content
 首页 » 编程设计

c#之使用NUnit从指定目录删除多个文件的单元测试方法

2024年02月13日12yxwkf

我是单元和集成测试的新手。我有一个类,其中包含一些用于删除指定目录中的多个文件并将文件移动到不同目录的方法。总共有3种方法。完整类代码:

    public class FilesUtility : IFilesUtility 
{ 
    public void TidyUpXmlFiles(bool isDebug, string XmlFileDirectory) 
    { 
        if (isDebug) 
        { 
            MoveXmlFiles(XmlFileDirectory); 
        } 
        else 
        { 
            DeleteXmlFiles(XmlFileDirectory); 
        } 
    } 
 
    private static void MoveXmlFiles(string XmlFileDirectory) 
    { 
        var di = new DirectoryInfo(XmlFileDirectory); 
        // Create a subdirectory in the parent directory called XmlArchive. 
        DirectoryInfo destinationDirectory = di.CreateSubdirectory("XmlArchive"); 
        string destinationDir = destinationDirectory.ToString(); 
        if (!Directory.Exists(destinationDir)) 
        { 
            Directory.CreateDirectory(destinationDir); 
        } 
        foreach (string file in Directory.GetFiles(XmlFileDirectory, 
                                                                "*.Xml")) 
        { 
            // Use static Path method to extract only the file name from the path. 
            string fileName = Path.GetFileName(file); 
            if (fileName != null) 
            { 
                string destFile = Path.Combine(destinationDir, fileName); 
                //if the same file exists in the destination folder, delete the file and move the new file. 
                if (File.Exists(file)) 
                { 
                    File.Delete(destFile); 
                    File.Move(file, destFile); 
                } 
            } 
        } 
    } 
 
    private static void DeleteXmlFiles(string XmlFileDirectory) 
    { 
        foreach (var file in Directory.GetFiles(XmlFileDirectory, 
                                                                "*.Xml")) 
        { 
            File.Delete(file); 
        } 
    } 
 
    public void MoveFaultyXml(string XmlFileDirectory, string fileFullPath) 
    { 
        var di = new DirectoryInfo(XmlFileDirectory); 
        // Create a subdirectory in the parent directory called XmlArchive. 
        var destinationDirectory = di.CreateSubdirectory("FaultyXml"); 
        var faultyXmlFileName = Path.GetFileName(fileFullPath); 
        var destinationDir = destinationDirectory.ToString(); 
        if (!Directory.Exists(destinationDir)) 
        { 
            Directory.CreateDirectory(destinationDir); 
        } 
 
        if (faultyXmlFileName != null) 
        { 
            string destFile = Path.Combine(destinationDir, faultyXmlFileName); 
            //if the same file exists in the destination folder, delete the file and move the new file. 
            if (!File.Exists(fileFullPath)) return; 
            File.Delete(destFile); 
            File.Move(fileFullPath, destFile); 
        } 
    } 

我已经为这个方法创建了一个接口(interface)来使用 Moq 来模拟,如果这是我需要做的我不确定。 接口(interface)代码为:

public interface IFilesUtility 
{ 
    void TidyUpXbrlFiles(bool isDebug, string xbrlFileDirectory); 
    void MoveFaultyXbrl(string xbrlFileDirectory, string fileFullPath); 
} 

现在我希望能够测试上述方法以确保它们实际上可以从目录中获取 XML 文件列表并删除/移动所有这些文件。 我一直在阅读有关最小起订量和 mock 的内容。我也一直在阅读有关依赖注入(inject)的内容,但我的问题是我已经阅读了太多,现在想不出从哪里开始。

我希望有人可以使用上面发布的示例代码向我解释我如何为这段代码构建一个成功的测试。 我已经在 Stackoverflow 上阅读了很多关于此的问题,我希望您不要将其作为重复项关闭,因为就像我说的那样,我的问题是我已经阅读了太多关于此的内容。 如果有人可以使用我示例中的其中一种方法向我解释,我想我可能会理解它。

我不知道如何执行正确的 [TestFixtureSetup],我认为在我的情况下我需要它,我可以获得要在每个测试中使用的文件列表。

感谢所有花时间回答的人:)

请您参考如下方法:

这是我测试文件处理方法的方法:

  1. 在本地系统上创建文件。有时我在 MasterFiles 文件夹中手动创建这些,然后将它们作为源的一部分提交,然后调用 UseTestMasterFile 将其复制到测试文件夹中。有时我会在测试中创建它们并将它们保存到测试文件夹中。
  2. 运行我的测试。
  3. 清理测试文件。

这是测试代码...

using System; 
using System.Collections.Generic; 
using System.Linq; 
using NUnit.Framework; 
using System.IO; 
 
public class FilesUtilityTests { 
    [Test] 
    public void TidyUpXmlFilesDeletesXmlFilesWhenNotDebugging() { 
        string testFilePath = TestFileHelper.UseTestMasterFile("YourXmlFile.xml"); 
        FilesUtility util = new FilesUtility(); 
 
        util.TidyUpXmlFiles(false, TestFileHelper.TestDirectoryName); 
 
        Assert.IsFalse(File.Exists(testFilePath)); 
        // any other tests here. 
    } 
} 
 
// Obviously, this could be in a separate dll that you reference on all test projects. 
public static class TestFileHelper { 
    public const string TestFolderName = @"Testing\UnitTests"; 
    public const string DefaultMasterFilesFolderName = "MasterFiles"; 
 
    public static string DefaultTestDirectoryName { 
        get { return Path.Combine(Path.GetDirectoryName(System.Environment.CurrentDirectory), TestFolderName); } 
    } 
    public static string TestDirectoryName { 
        get { return testDirectoryName ?? DefaultTestDirectoryName; } 
        set { testDirectoryName = value; } 
    } 
    private static string testDirectoryName; 
 
    public static string MasterFilesFolderName { 
        get { return masterFilesFolderName ?? DefaultMasterFilesFolderName; } 
        set { masterFilesFolderName = value; } 
    } 
    private static string masterFilesFolderName; 
 
    public static string TestFileExtension { get; set; } 
 
    public static string BuildTestFileName(string fileName) { 
        if (String.IsNullOrWhiteSpace(Path.GetPathRoot(fileName))) 
            fileName = Path.Combine(TestDirectoryName, fileName); 
        if (String.IsNullOrEmpty(Path.GetExtension(fileName))) 
            fileName = Path.ChangeExtension(fileName, TestFileExtension); 
        return fileName; 
    } 
 
    public static string BuildTestMasterFileName(string fileName) { 
        if (Path.IsPathRooted(fileName)) 
            return Path.Combine(Path.Combine(Path.GetDirectoryName(fileName), MasterFilesFolderName), Path.GetFileName(fileName)); 
        else 
            return Path.Combine(Path.Combine(TestDirectoryName, MasterFilesFolderName), fileName); 
    } 
 
    public static string UseTestMasterFile(string fileName) { 
        string dest = BuildTestFileName(fileName); 
        string source = BuildTestMasterFileName(dest); 
        DeleteTestFile(dest); 
        ClearReadOnlyAttributes(source); 
        File.Copy(source, dest, true); 
        return dest; 
    } 
 
    public static void DeleteTestFile(string filePath) { 
        if (String.IsNullOrWhiteSpace(filePath)) { return; } 
        if (!File.Exists(filePath)) { return; } 
 
        ClearReadOnlyAttributes(filePath); 
        File.Delete(filePath); 
    } 
 
    public static void ClearReadOnlyAttributes(string filePath) { 
        if (String.IsNullOrWhiteSpace(filePath)) { return; } 
        if (!File.Exists(filePath)) { return; } 
 
        FileAttributes attributes = File.GetAttributes(filePath); 
        if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) 
            File.SetAttributes(filePath, attributes ^ FileAttributes.ReadOnly); 
    } 
    public static void SetReadOnlyAttributes(string filePath) { 
        if (String.IsNullOrWhiteSpace(filePath)) { return; } 
        if (!File.Exists(filePath)) { return; } 
 
        FileAttributes attributes = File.GetAttributes(filePath); 
        if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) 
            File.SetAttributes(filePath, attributes | FileAttributes.ReadOnly); 
    } 
}