EmptyDirectory Constraint
EmptyDirectoryConstraint tests that a directory contains no files or subdirectories.
Usage
Is.Empty
Is.Not.Empty
Examples
[Test]
public void EmptyDirectoryConstraint_Examples()
{
var tempDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempDir);
try
{
// Test directory is empty
Assert.That(new DirectoryInfo(tempDir), Is.Empty);
// Add a file and verify not empty
File.WriteAllText(Path.Combine(tempDir, "test.txt"), "content");
Assert.That(new DirectoryInfo(tempDir), Is.Not.Empty);
}
finally
{
Directory.Delete(tempDir, recursive: true);
}
}
Notes
Is.Emptyis a polymorphic constraint that works on strings, collections, and directories.- When applied to a
DirectoryInfo, it checks for both files and subdirectories. - The directory must exist - passing a non-existent directory throws an exception.
See Also
- Empty Constraint - Polymorphic empty constraint
- FileOrDirectoryExists Constraint - Test existence