Directory.CreateDirectory() returns "Accss to the path '..\[folder]\' is denied." 与 SpecFlow 测试一起使用时出现异常

Directory.CreateDirectory() returns "Accss to the path '..\\[folder]\\' is denied." exception when used with SpecFlow test

我正在使用 SpecFlow 和 NUnit 创建一些单元测试。

我的一项测试的要求是创建多个测试文件夹。但是,测试 运行 是 returning "Accss to the path '..\TestFolder1\' is denied." 异常。我已经检查并仔细检查了我在父文件夹中的写入权限,尝试共享父文件夹并确保父文件夹或其任何内容未标记为只读,但错误仍然存​​在。

public static string[] CreateTestFolders()
    {
        _testFolders = new string[] { @"..\TestFolder1\", @"..\TestFolder2\", @"..\TestFolder3\", @"..\TestFolder4\", @"..\TestFolder5\" };

        for (int i = 0; i < _testFolders.Length - 1; i++)
        {
            try
            {
                Directory.CreateDirectory(_testFolders[i]);
            }
            catch(Exception ex)
            {
                string exm = Environment.UserName + " " + ex.Message;
            }
        }
        return _testFolders;
    }

此应用程序已经使用 MSTest 应用了单元测试;此方法在 [ClassInitialize] 测试设置中调用并且执行没有问题,但是尝试从 SpecFlow 步骤方法调用方法仍然 return 相同的异常。

我很难理解这里的问题。是否有我错过的特定于 SpecFlow 测试或 NUnit 的内容?

尝试使用

public static string[] CreateTestFolders()
{
    _testFolders = new string[] { @"TestFolder1", @"TestFolder2", @"TestFolder3", @"TestFolder4", @"TestFolder5" };

    for (int i = 0; i < _testFolders.Length - 1; i++)
    {
        try
        {
            // Also try with Application.StartupPath instead of Directory.GetCurrentDirectory()
            string currentPath = Path.Combine(Directory.GetCurrentDirectory(), _testFolders[i]);
            if (!Directory.Exists(currentPath)
            {
                Directory.CreateDirectory(currentPath);
            }
            //at this point your folder should exist
        }
        catch(Exception ex)
        {
            string exm = Environment.UserName + " " + ex.Message;
        }
    }
    return _testFolders;
}

尝试使用 MSDN 中提到的 DirectorySecurity。创建目录时我遇到了同样的问题。我使用了 DirectorySecurity,如下所示:

// Define Security Rules
var securityRules = new DirectorySecurity();
securityRules.AddAccessRule(new 
FileSystemAccessRule(@"Domain\AdminAccount1", FileSystemRights.Read, AccessControlType.Allow));
// ...

// ...
// Use the security rules while creating directory
var directoryInfo = Directory.CreateDirectory(_testFolders[i], securityRules);

我认为它只是不喜欢双点符号,请改用 Environment.CurrentDirectory,或者只需执行以下操作:

DirectoryInfo cd = new DirectoryInfo(Environment.CurrentDirectory);
cd.CreateDirecory("TestFolder1"); // creates a relative Directory

所以,事实证明,问题出在 NUnit 上。 用MSTest执行的测试 return 测试项目的bin文件夹为 Environment.CurrentDirectory, "C:\Code[test_project_folder]\bin\Debug";使用 NUnit return "C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO 14.0\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\TESTWINDOW" 执行的测试。 由于 CurrentDirectory 是 "Program Files (x86)" 文件夹的子目录,导致访问被拒绝。

为了解决这个问题,我在辅助方法中添加了一个 testDirectory 字符串参数...

public static string[] CreateTestFolders(string testDirectory)
{
    _testFolders = new string[] { @"TestFolder1", @"TestFolder2", @"TestFolder3", @"TestFolder4", @"TestFolder5" };

    for (int i = 0; i < _testFolders.Length - 1; i++)
    {
        try
        {
            _testFolders[i] = Directory.CreateDirectory(testDirectory + _testFolders[i]).FullName;
        }
        catch(Exception ex)
        {
            string exm = Environment.UserName + " " + ex.Message;
        }
    }
    return _testFolders;
}

... 并更新了我的方法调用以将 AppDomain.CurrentDomain.BaseDirectory 作为参数传递...

_testFileFolders = StepHelper.CreateTestFolders(AppDomain.CurrentDomain.BaseDirectory);

这个修复产生了我想要的结果。