首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Pester 5.3.3进行测试

用Pester 5.3.3进行测试
EN

Stack Overflow用户
提问于 2022-06-28 10:37:55
回答 1查看 108关注 0票数 1

我编写了一个函数,用于将函数中发生的错误写入csv文件中,该函数在函数的catch块中调用。我想用Pester写一个测试来检查我的函数是否正常工作,但老实说,我不知道从哪里开始,我尝试了一些东西,但它们对我不起作用,我也一直在阅读文档,但我仍然不清楚,我会感谢任何帮助/评论。

这里是我想用Pester:编写一个测试的函数

代码语言:javascript
复制
function Write-Logs {
    param (
        [ValidateSet("Error")]$MessageType,
        [string][Parameter(Mandatory=$true)]$Message,
        $LogFilePath,
        $Source
    )
    $CSVLogPath = Join-Path -Path $PSScriptRoot -ChildPath ".\errorslog.csv"
    $CSVLogObject = [PSCustomObject] @{
        Date = Get-Date
        Message = $Message
        MessageType = $MessageType
        Source = $Source
    }
    $CSVLogObject | Export-Csv -Path $CSVLogPath -NoTypeInformation -Encoding UTF8 -Append
}

因此,我在catch块中调用函数:

代码语言:javascript
复制
    catch {
        Write-Logs -LogFilePath:$CSVLogPath -Message:$Error[0].Exception.Message `
        -Source:"FunctionName()" -MessageType:"Error"
        return
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-28 12:09:27

继续我的评论,这是一些代码。

首先,通过实际使用-LogFilePath参数,使函数可测试。这样,您就可以在测试期间将日志写入临时文件。由于默认值,在从普通代码调用它时,仍然可以不使用-LogFilePath使用它。

代码语言:javascript
复制
function Write-Logs {
    param (
        [ValidateSet("Error")]$MessageType,
        [string][Parameter(Mandatory=$true)]$Message,
        $LogFilePath = (Join-Path -Path $PSScriptRoot -ChildPath ".\errorslog.csv"),
        $Source
    )
    $CSVLogObject = [PSCustomObject] @{
        Date = Get-Date
        Message = $Message
        MessageType = $MessageType
        Source = $Source
    }
    $CSVLogObject | Export-Csv -Path $LogFilePath -NoTypeInformation -Encoding UTF8 -Append
}

测试代码:

代码语言:javascript
复制
BeforeAll {
    . $PSCommandPath.Replace('.Tests.ps1','.ps1')
}

Describe "Write-Logs" {
    BeforeEach{
        # Mock Get-Date so it returns a constant value suitable for testing
        $expectedDate = [DateTime]::new( 2022, 06, 28, 12, 36, 21 )
        Mock Get-Date { return $expectedDate }
    }   

    It "writes the expected CSV" {
    
        # You might read this from a file using Import-Csv
        $expectedCsv = [PSCustomObject]@{
            Date = $expectedDate
            Message = 'test message'
            MessageType = 'Error'
            Source = 'test source'
        }

        # Write log to temp file (Pester cleans it automatically, when It block ends)
        $testLogPath = "TestDrive:\test.log"
        Write-Logs -LogFilePath $testLogPath -MessageType $expectedCsv.MessageType -Message $expectedCsv.Message -Source $expectedCsv.Source

        $actualCsv = Import-Csv $testLogPath

        # Test if $expectedCsv equals $actualCsv
        Compare-Object $expectedCsv $actualCsv -Property Date, Message, MessageType, Source | Should -BeNullOrEmpty
    }
}
  • TestDrive:是Pester为每个脚本块创建的临时驱动器。编写临时文件非常方便,因为当脚本块结束时,Pester会自动清除它。见烦扰医生
  • 一旦一些基本测试正常运行,您可能希望通过使用数据驱动的测试来改进测试代码。这避免了重复,因为您只需要一个测试,该测试可以从不同的数据集中输入。请参阅烦扰医生,特别是“向测试提供外部数据”一节。
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72784944

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档