当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 加速 Pester 测试开发

作者:精品下载站 日期:2024-12-14 08:05:19 浏览:14 分类:玩电脑

加速 Pester 测试开发


[玩转系统] 加速 Pester 测试开发

前几天,我分享了我的 2022 年 PowerShell 计划。不用说,我迫不及待地想深入研究。我正在开发一个新模块,由于它要到下个月才会发布,所以我继续并标记了它仅作为核心。我还开始为其编写一组 Pester 5.x 测试。当然,这迫使我重新访问 Pester 文档并重新学习如何构建 Pester 测试。

在这个过程中,我决定需要帮助自己加快测试编写阶段。我有一组标准的测试,我喜欢将其用于模块中的函数。但复制和粘贴代码片段是很乏味的。我知道我可以创建一组 VS Code 片段,但这感觉有限制,我必须确保这些片段在我可能运行 VS Code 的所有系统上可用。相反,我编写了一个 PowerShell 函数来加速开发 Pester 5.x 测试。

我的函数采用一个模块并提取所有公共导出函数。对于每个函数,它都会创建一组标准 Pester 断言。这些是我总是想为每个函数运行的基线或样板测试。每个函数都包装在一个Describe 块中。不过,我可以选择 Context 块。该命令还将插入标签。请注意,我的标签插入代码依赖于 PowerShell 7 中的三元运算符。

$($tags ? "-tag $($tags -join ',')" : $null)

如果你想在Windows PowerShell中使用我的函数,你需要修改它。

该函数将字符串写入管道。

[玩转系统] 加速 Pester 测试开发

我可以运行 New-PesterBlock 并通过管道传输到 Out-File 或 Set-Clipboard。

这是带有该函数的脚本文件。

#requires -version 7.1

Function New-PesterBlock {
    <#
.SYNOPSIS
    Create a Pester 5.x test block
.DESCRIPTION
    Create a Pester test block for exported functions from a given module.
    The default output is a Describe block for each function but you can
    create a Context block as an alternative.

    The default behavior is to add a 'Function' tag to each block. You
    can specify your own comma separated list of tags. Or use a parameter
    value of $Null to not insert any tags.

    The output is written for Pester 5.x.
.EXAMPLE
    PS C:\> New-PesterBlock psteachingtools

    Describe Get-Vegetable {
    It "Should have help documentation" {
        (Get-Help Get-Vegetable).Description | Should -Not -BeNullOrEmpty
    }
    It "Should have a defined output type" {
        (Get-Command -CommandType function -name Get-Vegetable).OutputType | Should -not -BeNullOrEmpty
    }
    It "Should run without error" {
        #mock and set mandator parameters as needed
        {Get-Vegetable} | Should -Not -Throw
    } -pending

} -tag function

Describe New-Vegetable {
    It "Should have help documentation" {
        (Get-Help New-Vegetable).Description | Should -Not -BeNullOrEmpty
    }
    It "Should have a defined output type" {
        (Get-Command -CommandType function -name New-Vegetable).OutputType | Should -not -BeNullOrEmpty
    }
    It "Should run without error" {
        #mock and set mandator parameters as needed
        {New-Vegetable} | Should -Not -Throw
    } -pending

} -tag function
...

        Create a Describe block for each function in the PSTeachingTools module.
. EXAMPLE
    PS C:\> New-Pesterblock psteachingtools -BlockType Context | Set-Clipboard

    Create a context block for each function and copy the output to the Windows clipboard.
.INPUTS
    None
.OUTPUTS
    [System.String]
    #>
    [cmdletbinding()]
    [alias("npb")]
    [OutputType([System.String])]
    Param(
        [Parameter(Position = 0, Mandatory,HelpMessage = "The name of a PowerShell module.")]
        [ValidateNotNullOrEmpty()]
        [string]$ModuleName,
        [Parameter(Position = 1,HelpMessage = "What kind of Pester test block do you want to create?")]
        [ValidateSet("Describe", "Context")]
        [string]$BlockType = "Describe",
        [Parameter(HelpMessage = "Specify tags separated by commas. Use `$null to not insert any tags.")]
        [string[]]$Tag = "function"
    )
    Begin {
        Write-Verbose "[$((Get-Date).TimeofDay) BEGIN  ] Starting $($myinvocation.mycommand)"
        #Put blocktype in proper case to make it pretty
        $BlockType = [System.Globalization.CultureInfo]::CurrentUICulture.TextInfo.ToTitleCase($BlockType)
        $Tags = $tag -join ","
    } #begin

    Process {
        Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Getting commands from $ModuleName"
        Try {
            $cmds = Get-Command -Module $ModuleName -CommandType Function -ErrorAction Stop
        }
        Catch {
            Throw $_
        }
        if ($cmds) {
            Foreach ($cmd in $cmds) {
                Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Defining tests for $($cmd.name)"
                @"

$BlockType $($cmd.name) {
    It "Should have help documentation" {
        (Get-Help $($cmd.name)).Description | Should -Not -BeNullOrEmpty
    }
    It "Should have a defined output type" {
        (Get-Command -CommandType function -name $($cmd.name)).OutputType | Should -Not -BeNullOrEmpty
    }
    It "Should run without error" {
        <#
        mock and set mandatory parameters as needed
        this test is marked as pending since it
        most likely needs to be refined
        #>
        {$($cmd.name)} | Should -Not -Throw
    } -pending
    #insert additional command-specific tests

} $($tags ? "-tag $($tags -join ',')" : $null)
"@
            } #foreach cmd

        } #if cmds
        else {
            Write-Warning "No functions found in the module $Modulename or the module itself doesn't exist."
        }
    } #process

    End {
        Write-Verbose "[$((Get-Date).TimeofDay) END    ] Ending $($myinvocation.mycommand)"
    } #end

} #close New-PesterBlock

该函数为每个测试块创建一个此处字符串。这些是我的默认断言。您可能想要编辑该函数的版本。我可能会将此命令集成到 VS Code 任务中。我希望你能让我知道你最终如何使用它。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯