Directory.CreateDirectory()返回“Accss to the path'.. \\ [folder] \\'被拒绝。”与SpecFlow测试一起使用时的异常

时间:2018-06-05 10:54:30

标签: c# unit-testing nunit specflow

我正在使用SpecFlow& amp;创建一些单元测试。 NUnit的。

我的一项测试要求是创建许多测试文件夹。但是,测试运行返回“Accss to the path'.. \ TestFolder1 \'被拒绝。”例外。我已检查并仔细检查我在父文件夹中是否具有写入权限,尝试共享父文件夹并确保父文件夹或其任何内容未标记为只读但错误仍然存​​在。

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步骤方法调用该方法仍会返回相同的异常。

我在努力理解这个问题。我错过了SpecFlow测试或NUnit特有的东西吗?

3 个答案:

答案 0 :(得分:1)

因此,事实证明,问题出在NUnit上。 使用MSTest执行的测试将测试项目的bin文件夹作为Environment.CurrentDirectory返回,“C:\ Code [test_project_folder] \ bin \ Debug”;使用NUnit执行的测试返回“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);

这个修正产生了我追逐的结果。

答案 1 :(得分:0)

尝试使用

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

答案 2 :(得分:0)

我认为它不像双点表示法,而是使用Environment.CurrentDirectory,或者只是执行以下操作:

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