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

Pester测试不反映脚本变更及函数加载失败问题求助

问题1:单文件测试断言失效的修复

核心问题分析

  1. 断言执行时机错误:你将Invoke-Sqlcmd调用放在It块外部(Pester的Discovery阶段),该阶段仅收集测试元数据,不会校验断言结果,导致表不存在时错误未被捕获。
  2. 数据库参数未生效:Push-Location/Pop-Location无法隔离SQL Provider的上下文,Database参数可能被会话缓存覆盖,修改数据库名后查询仍在原库执行。

修复后的单文件测试代码

BeforeAll{
    Import-Module sqlserver -DisableNameChecking
    Remove-Item -Path "$PSScriptRoot\testresults.xml" -ErrorAction SilentlyContinue
}

Describe "Test Table Existence" {
    $tableName = 'yyy'
    $instance = "myServer"
    $database = 'master'

    It "$tableName should exist in database $database" {
        $dbcmd = @"
SELECT [Name] AS TableName 
  FROM [sys].[tables] 
 WHERE [Name] = '$($tableName)'
"@
        $result = Invoke-Sqlcmd -Query $dbcmd `
                      -ServerInstance $instance `
                      -Database $database `
                      -SuppressProviderContextWarning `
                      -ErrorAction Stop

        $result | Should -Not -BeNullOrEmpty
        $result.TableName | Should -Be $tableName
    }
}

关键修改点

  • 将SQL查询逻辑移至It块内部,确保每次测试都重新执行并校验结果。
  • 添加-ErrorAction Stop,强制抛出数据库连接/查询错误,避免静默返回空值。
  • 增加非空断言,先验证查询返回结果,再检查表名匹配。
  • 移除无效的Push-Location/Pop-Location,直接在BeforeAll导入依赖模块。

问题2:拆分文件后函数加载失败的修复

核心问题分析

报错源于函数文件路径加载错误:$PSScriptRoot在BeforeAll中的取值可能与执行Invoke-Pester时的工作目录不匹配,导致无法找到Get-Table.ps1。

修复后的测试文件代码

BeforeAll{
    $functionPath = Join-Path -Path $PSScriptRoot -ChildPath "Get-Table.ps1"
    if (-not (Test-Path -Path $functionPath)) {
        throw "函数文件不存在:$functionPath"
    }
    . $functionPath
    Import-Module sqlserver -DisableNameChecking
}

Describe "Test Get-Table Function" {
    $tableName = 'clusters'
    $instance = "xxx"
    $database = "YYY"

    It "$tableName should exist in database $database on instance $instance" {
        $result = Get-Table -tableName $tableName -instance $instance -database $database -ErrorAction Stop
        $result | Should -Not -BeNullOrEmpty
        $result.TableName | Should -Be $tableName
    }
}

关键修改点

  1. 验证函数文件路径:用Join-Path拼接绝对路径,并用Test-Path确认文件存在,避免路径错误。
  2. 函数调用移至It块:避免在Discovery阶段执行数据库查询,确保测试独立性。
  3. 提前加载依赖模块:在BeforeAll中导入sqlserver,确保函数执行时依赖已就绪。

函数文件优化(Get-Table.ps1)

Function Get-Table
{  
    param(
        [Parameter(Mandatory=$true)]
        [string]$tableName,
        [Parameter(Mandatory=$true)]
        [string]$instance,
        [Parameter(Mandatory=$true)]
        [string]$database
    )

    $dbcmd = @"
SELECT [Name] AS TableName 
  FROM [sys].[tables] 
 WHERE [Name] = '$($tableName)'
"@

    Invoke-Sqlcmd -Query $dbcmd `
                  -ServerInstance $instance `
                  -Database $database `
                  -SuppressProviderContextWarning `
                  -ErrorAction Stop
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 21:30:40