简体   繁体   English

如何对运行 PowerShell 脚本的 C# 库进行单元测试

[英]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.我有一个 C# 库,它运行几个 PowerShell 脚本来操作 Windows 管理程序。 (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. (例如打开和关闭VM,使用vhds创建VM,获取开关等)很难模拟环境并控制脚本的output。 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.我为 PowerShell 找到了一个很好的单元测试框架 Pester。 But my code consist a great amount of C# code.但是我的代码包含大量的 C# 代码。 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).当您测试的代码执行多项操作(单元)时,根据定义,它是一个集成测试,因为它测试多个单元的集成(@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.当您测试的代码与基础设施(文件系统、数据库,在您的情况下Powershell和 VM 等)交互时,根据定义,它是一个集成测试。

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).您可以尝试在C#中的单独函数中提取少量代码并对它们进行单元测试,从而确定这些工作并在Powershell中执行相同的操作(使用 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.)使用单元测试框架在C# (如xUnit )中设置测试,表示明确的SetupTeardown阶段,分别在测试前设置所需的一切并在测试后清理(请记住,测试可能默认并行运行,并且这不 go 与文件系统上的共享资源等)

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.另一方面,提取纯单元可以更轻松地测试这些特定单元。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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