linux - Powershell 无法在 VSCode Linux 上找到类型 [Pester.OutputTypes]

标签 linux powershell visual-studio-code pester

我正在开发 powershell port of Lesspass在 Linux Mint 上使用 Visual Studio Code。

截至今天,测试在 IDE 中运行良好。

来自 VSCode

现在,当我在测试文件上并按 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.

来自 makefile

但是,当使用 make test 运行我的测试时,它可以工作。任务是:

.PHONY: test
test:
    pwsh -Command 'Invoke-Pester -EnableExit (Get-childItem -Recurse *.tests.ps1).fullname'

最佳答案

我认为您的问题可能是您尝试单独调用 pester 测试脚本,而不是通过 Invoke-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 的输出是以下代码段

By default, Invoke-Pester runs all *.Tests.ps1 files in the current directory and all subdirectories recursively. You can use its parameters to select tests by file name, test name, or tag.

来自 Get-Help Invoke-Pester -Examples 输出的这段代码演示了 Invoke-Pester 搜索给定目录的子目录的能力,不一定是要运行的测试的当前工作目录

-------------------------- EXAMPLE 11 --------------------------

PS > Invoke-Pester -Script C:\Tests -Tag UnitTest, Newest -ExcludeTag Bug

This command runs *.Tests.ps1 files in C:\Tests and its subdirectories. In those files, it runs only tests that have UnitTest or Newest tags, unless the test also has a Bug tag.

因此,在您的情况下,将 make 调用更改为可能会更容易、更清晰 pwsh -命令'Invoke-Pester -EnableExit

假设您的构建系统将当前工作目录设置为项目的根文件夹。

关于linux - Powershell 无法在 VSCode Linux 上找到类型 [Pester.OutputTypes],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55722061/

相关文章:

linux - 用于将文件从服务器传输到本地计算机的 Ksh 脚本

c++ - 是否可以测试中止例程不返回?

powershell - Switch-AzureMode AzureResourceManager 无法识别

powershell - Win 7 Scheduler无法启动Powershell脚本-怎么办?

html - 在 VS Code 中创建自定义 HTML 片段

php - 服务器不从 rss xml feed 导入图片

linux - sed命令在多行搜索后插入多行字符串

powershell - 在远程服务器中安装软件时出现 Invoke-Command 问题

angularjs - Visual Studio Code 调试中未实现的控制台 API : undefined, 错误

fonts - 如何在 Visual Studio Code 中启用递归 (OpenType) 字体代码连字?