BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Testing
.NET 1.1+

NUnit Directory Assertions

NUnit includes several assertion classes in addition to the commonly used, Assert class. One is the DirectoryAssert type, which contains methods that validate the equality of folder paths, confirm folder structures are as expected and verify the contents of directories.

DirectoryAssert

When creating tests that work with folder paths or the content of directories, you can simplify some of your code with NUnit's DirectoryAssert class. This static class contains assertions that examine either a path or a folder on a disk. For test-driven development, the path assertions are very useful because they do not access any disk or shared network folder, allowing you to create fast-running unit tests. The assertions that access folder contents are more suited to integration tests, which are generally much slower to execute.

In this article we'll see the six assertion methods for use with folder paths. The article describes the DirectoryAssert methods present in NUnit 2.6. Other versions may have different members.

AreEqual and AreNotEqual

The first two static methods to consider are AreEqual and AreNotEqual. You can call these assertions with two string arguments. The first contains the expected path and the second should hold the actual path. AreEqual will pass if the two paths match. AreNotEqual passes if the paths differ.

The following example shows a single test that contains an assertion that passes and one that fails:

[Test]
public void TwoFoldersAreEqual2()
{
    string dir1 = @"d:\temp";
    string dir2 = @"d:\temp";

    DirectoryAssert.AreEqual(dir1, dir2);    // Pass
    DirectoryAssert.AreNotEqual(dir1, dir2); // Fail
}

If you are passing folder paths to the assertions as strings, there is no real benefit over using the basic Assert class's methods. However, when using DirectoryAssert you can provide the two folders using DirectoryInfo instances. This can make your tests simpler if your code is already working with this type of object.

[Test]
public void TwoFoldersAreEqual()
{
    DirectoryInfo dir1 = new DirectoryInfo(@"d:\temp");
    DirectoryInfo dir2 = new DirectoryInfo(@"d:\temp");

    DirectoryAssert.AreEqual(dir1, dir2);    // Pass
    DirectoryAssert.AreNotEqual(dir1, dir2); // Fail
}

NB: This method does not access the actual folders on disk. This means that you can compare folders that do not exist.

IsWithin and IsNotWithin

IsWithin and IsNotWithin allow you to compare two paths and determine whether one is a subfolder of the other. This can be a direct subfolder, or one that is several levels deeper into the file structure. The directories are not accessed, so it is possible to determine the potential directory structure for items that do not exist on disk.

As with the previous assertions, you pass two paths, either as strings or instances of the DirectoryInfo class. The first argument is the higher level directory and the second is the lower level item. The following code demonstrates the use of IsWithin. The first assertion passes but the second fails because subfolder2 does appear beneath the temp folder.

[Test]
public void Subfolder1IsInTemp()
{
    DirectoryInfo dir1 = new DirectoryInfo(@"d:\temp\subfolder\subfolder2");
    DirectoryInfo dir2 = new DirectoryInfo(@"d:\temp");

    DirectoryAssert.IsWithin(dir2, dir1); // Pass
    DirectoryAssert.IsWithin(dir1, dir2); // Fail
}

IsNotWithin is used in the same manner but passes when the second directory is not a direct or indirect child of the first. This time the paths are defined in strings.

[Test]
public void TempIsNotInSubfolder1()
{
    string dir1 = @"d:\temp\subfolder";
    string dir2 = @"d:\temp";

    DirectoryAssert.IsNotWithin(dir1, dir2); // Pass
    DirectoryAssert.IsNotWithin(dir2, dir1); // Fail
}

IsEmpty and IsNotEmpty

The third pair of assertion methods allow you to verify that a folder is either empty or not empty. The IsEmpty and IsNotEmpty methods do access the disk to perform the checks, so are potentially slower to execute. The folder to check is passed as the only argument, using a string or a DirectoryInfo object.

The following example assumes the existence of a folder named, "d:\temp", which contains no subfolders or files.

[Test]
public void FolderIsEmpty()
{
    string dir = @"d:\temp";

    DirectoryAssert.IsEmpty(dir);    // Pass
    DirectoryAssert.IsNotEmpty(dir); // Fail
}
19 November 2015