简体   繁体   中英

How to unit test a C# library that runs PowerShell scripts

I have a C# library that runs a couple of PowerShell scripts to manipulate Windows Hypervisor. (eg 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:

  1. When the code you test does multiple things (units) it is, by definition, an integration test, since it tests the integration of multiple units (the comment of @Xerillio).
  2. When the code you tests interacts with infrastructure (file system, database, in your case 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.

  • Find the most outer point of your application with what you can interact from a test, so you test the biggest part of the stack you can.
  • Check which parts you can automatically test without causing havoc (you mention "irreversible methods")
  • Use a unit test framework for setting up the tests in 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM