我正在使用Linux Mint上的Visual Studio Code处理powershell port of Lesspass。
截至今天,测试在IDE中运行良好。
现在,当我在测试文件上并点击F5运行测试时,我得到了:
PS ~/projects/Lesspass/Lesspass> ~/projects/Lesspass/Lesspass/src/Password.tests.ps1
Unable to find type [Pester.OutputTypes].
At ~/.local/share/powershell/Modules/Pester/4.6.0/Functions/PesterState.ps1:8 char:9
+ [Pester.OutputTypes]$Show = 'All',
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (Pester.OutputTypes:TypeName) [], RuntimeException
+ FullyQualifiedErrorId : TypeNotFound
The Describe command may only be used from a Pester test script.
At ~/.local/share/powershell/Modules/Pester/4.6.0/Functions/Describe.ps1:234 char:9
+ throw "The $CommandName command may only be used from a Peste ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (The Describe comman\u2026Pester test script.:String) [], RuntimeException
+ FullyQualifiedErrorId : The Describe command may only be used from a Pester test script.
但是当用make test
运行我的测试时,它可以工作。任务是:
.PHONY: test
test:
pwsh -Command 'Invoke-Pester -EnableExit (Get-childItem -Recurse *.tests.ps1).fullname'
我认为您的问题很可能是因为您试图通过它自己而不是通过Invoke-Pester
命令调用pester测试脚本。
我想如果你改变你对Invoke-Pester -Script ~/projects/Lesspass/Lesspass/src/Password.tests.ps1
的号召,你的错误可能就会消失。
原因是* .tests.ps1文件本身不知道如何设置处理测试运行所需的所有后台管道。 Invoke-Pester
在运行测试文件之前做了很多设置,并且直接使用F5
调用测试脚本会跳过该设置。
如果你想能够按F5
开始测试运行,许多PowerShellers在VSCode中做的是在本地系统上创建一个debug_entry.ps1
文件,并在该文件中输入命令Invoke-Pester -Script ~/projects/Lesspass/Lesspass/src/Password.tests.ps1
。然后,当您想要开始运行时,将选项卡切换到debug_entry.ps1
文件并点击F5
,您的调试脚本会为您拨打正确的电话。它的另一个好处是,您在测试文件中设置的任何调试断点,或者您正在测试的代码中都应该受到尊重。
我还想我也应该指出,在你的make test
脚本中,你正在使用Get-ChildItem
手动显式地获取所有测试文件路径并将它们传递给Invoke-Pester
。这不是必需的。默认情况下,Invoke-Pester
将始终搜索您当前的工作目录,或者以递归方式查找所有可用的测试文件的路径。
例如,Get-Help Invoke-Pester
的输出是以下片段
默认情况下,Invoke-Pester以递归方式运行当前目录和所有子目录中的所有* .Tests.ps1文件。您可以使用其参数按文件名,测试名称或标记选择测试。
来自Get-Help Invoke-Pester -Examples
输出的这个片段演示了Invoke-Pester
搜索给定目录的子目录的能力,不一定是运行测试的当前工作目录
--------------------------例11 ---------------------- ----
PS> Invoke-Pester -Script C:\ Tests -Tag UnitTest,最新-ExcludeTag Bug
此命令在C:\ Tests及其子目录中运行* .Tests.ps1文件。在这些文件中,它只运行具有UnitTest或最新标记的测试,除非测试也有Bug标记。
因此,在您的情况下,将make
调用更改为pwsh -Command 'Invoke-Pester -EnableExit
可能更容易,更清晰
假设您的构建系统将当前工作目录设置为项目的根文件夹。