Table of Contents – The TestDrive Drive
Today we’re going to discuss the final chapter in Part I of The Pester Book. It’s time to discuss the TestDrive. But before doing that, let me point out that you may want to go back and review the Mocks chapter. While it’s not directly tied to the TestDrive, that entire chapter covered a good deal of information — know that stuff. Those last few sections were intense! And after that sentence, I would like to mention that I rarely use exclamation points.
Okay, so moving on. Remember that in unit tests, we want to be dependent of all entities outside of our tests. This includes databases, Active Directory, and anything else that has to be in place for a function to properly execute. This includes the file system, too. If it has to be there, and it’s separate from our test and the code that we’re testing, then it needs to removed mocked.
We don’t really want to actually modify the file system of the system on which our tests are running. Again, we don’t want to require any dependence on our system; it’s an important distinction of unit tests. We have a “drive” we can use in our testing without any real disturbance to the underlying operating system.
See the book for some solid examples of how the TestDrive is scoped. As many things in Pester do, the TestDrive works differently here and there, and it’s best to learn it now, instead of later, in that stuffing some of this in your mind now, might save time when you’re having to revisit this alone. A couple pointers I do want to write down myself, however: One, tests cannot access a TestDrive in a different Describe block, other than the one in which they are executing. Two, Files created directly in a Describe block can be accessed by Context blocks contained in that same Describe block. I think I got it right.
Oh yeah, there’s also a variable created that you can use. Take a look at that, as well as what values are displayed when I return all my FileSystem PSDrives, from within a test (yes, using Write-Host).
Clear-Host Describe 'TestDrive system location:' { Write-Host $TestDrive Write-Host (Get-PSDrive -PSProvider FileSystem) } Describing TestDrive system location: C:\Users\tommymaynard\AppData\Local\Temp\b41d3954-dece-3a2c-9b14-e0206a3a5d63 C D S TestDrive X
Time to start Part II. See you then.