Search Results for

    Show / Hide Table of Contents

    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

    1. Is.Empty is a polymorphic constraint that works on strings, collections, and directories.
    2. When applied to a DirectoryInfo, it checks for both files and subdirectories.
    3. The directory must exist - passing a non-existent directory throws an exception.

    See Also

    • Empty Constraint - Polymorphic empty constraint
    • FileOrDirectoryExists Constraint - Test existence
    • Edit this page
    In this article
    Back to top Generated by DocFX | Copyright (c) 2018- The NUnit Project - Licensed under CC BY-NC-SA 4.0