I have a C# library that runs a couple of PowerShell scripts to manipulate Windows Hypervisor. (e.g. turning VM on and off, create a VM with vhds, getting switches and etc.) It's hard to mock the environment and control the output of the scripts. Scripts for checking/validating stuff could be easier, but the scripts for operational purpose could be a headache because most of these methods could be irreversible.
I found a good unit test framework Pester for PowerShell. But my code consist a great amount of C# code. Is there any good way to handle this unit test problem gracefully?
Thanks in advance!
There are several points to make here:
Powershell
and VM's etc.), it is, by definition, an integration test.You could try to extract small amounts of code in seperate functions in C#
and unit test those, giving you certainty those work and do the same in Powershell
(using Pester). But the real question is; what do you want to achieve? I would assume you want to be sure you're application works now and keeps working in the future (does not regress), in which case you could look more into a more end to end approach to testing.
C#
(like xUnit
) expressing clear Setup
and Teardown
phases that, respectively, setup everything needed before a test and clean up after a test (keep in mind that tests might run parallel by default and this doesn't go well with shared resources on the file system etc.)This avoids having to rewrite code just because you want to unit test it. On the other hand, extracting pure units makes testing those specific units way easier.