You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何PowerShell Pester临时文件测试本地通过但GitHub Actions失败?

PowerShell模块Pester测试本地正常但GitHub Actions中4个失败

我正在搭建PowerShell模块,编写了5个Pester测试用例,本地运行全部通过,但在GitHub Actions环境中仅1个测试通过,其余4个失败。我怀疑这是由于GitHub Actions工作流中文件或文件夹创建的限制导致的。

测试中会创建临时.env文件和文件夹来模拟环境变量的加载与卸载,示例代码如下:

Describe "Import-DotEnv" {
  Context "Loading environment variables" {
      It "Loads environment variables from a .env file" {
          # Create a temporary .env file
          $tempDir = New-Item -ItemType Directory -Path "$env:TEMP\ImportDotEnvTest_$(Get-Random)"
          $envFilePath = Join-Path $tempDir.FullName ".env"

          Set-Content -Path $envFilePath -Value "TEST_VAR=123"

          try {
              # Change to the directory containing the .env file
              Set-Location -Path $tempDir

              # Assert that TEST_VAR is set to "123"
              $env:TEST_VAR | Should -Be "123"
          }
          finally {
              # Reset the working directory
              Set-Location -Path $env:TEMP

              # Clean up
              Remove-Item -Path $tempDir.FullName -Recurse -Force -ErrorAction SilentlyContinue
              Remove-Item -Path Env:\TEST_VAR -Force -ErrorAction SilentlyContinue
          }
      }
  }
}

GitHub Actions的工作流输出结果如下:

Run powershell -ExecutionPolicy Bypass -Command "Invoke-Pester -Path .\Tests\ -Output Detailed"
Pester v5.7.1

Starting discovery in 1 files.
Discovery found 5 tests in 850ms.
Running tests.

Running tests from 'ImportDotEnv.Tests.ps1'
Describing Import-DotEnv
 Context Loading environment variables
Error: [-] Loads environment variables from a .env file 1.5s (1.27s|232ms)
Message
 Context Unloading environment variables
Error: [-] Unloads environment variables from a .env file 12ms (9ms|3ms)
Message
 Context Handling multiple .env files
Error: [-] Loads and unloads multiple .env files correctly 24ms (14ms|10ms)
Message
 Context Edge cases
   [+] Does not throw an error if the .env file does not exist 115ms (112ms|3ms)
Error: [-] Handles empty .env files correctly 100ms (99ms|1ms)
Message
Tests completed in 5.83s
Tests Passed: 1, Failed: 4, Skipped: 0, Inconclusive: 0, NotRun: 0

想请教各位,为何测试在本地正常运行但在GitHub Actions中失败?

内容的提问来源于stack exchange,提问作者Pulsares

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 09:57:05