Skip to content

Temporary Files and Resources

Jon Wagner edited this page Feb 18, 2013 · 2 revisions

Temporary Files and Resources

This is testing with PowerShell, so there is going to be a lot of file processing, registry manipulation, etc. You can use TestSetup and TestTearDown to clean up your mess, but we made it easier for you.

New-TestFolder

New-TestFolder creates a new temporary folder and returns it to you. When the test context goes through TearDown, the folder and its contents are automatically removed for you.

Describing "FileStuff" {
    Given "nothing" {

        It "can create a test folder" {
            # create a folder
            $folder = New-TestFolder

            # do something with it
        }

		# the folder is cleaned up for you!
    }
}

New-TestFile

Like New-TestFolder, New-TestFile creates a new temporary file and returns it to you, and also cleans it up.

Describing "FileStuff" {
    Given "nothing" {

        It "can create a test file" {
            # create a file
            $file = New-TestFile

            # do something with it
        }

		# the file is cleaned up for you!
    }
}

By default, it creates a file in the temp folder.

There are a few more options:

  • Name - specifies the name of the file (a guid is the default)
  • Path - specifies the path of the file (the temp folder is the default)

Other Resources

PSate can automatically clean up anything that Remove-Item can remove, like registry entries. Just pass the object to Register-TestCleanup.

Describing "RegistryStuff" {
    Given "nothing" {

        It "can create a test registry key" {
            # create a file
            $reg = new-item -path hkcu:\Environment\TestNew | Register-TestCleanup -PassThru

            # do something with it
        }

		# the registry entry is cleaned up for you
    }
}