1
0

PowerShellのテスト Pesterのインストール

Posted at

PowerShellのテスト用フレームワーク、Pesterをインストールする。

Quick Start
https://pester.dev/docs/quick-start

今回のインストール先はMacのPowerShell。

$ pwsh
PowerShell 7.4.2
PS /mogemoge> Install-Module -Name Pester
PS /mogemoge> Import-Module Pester -PassThru                                 

ModuleType Version    PreRelease Name                                ExportedCo
                                                                     mmands
---------- -------    ---------- ----                                ----------
Script     5.6.0                 Pester                              {Add-Shou…

インストールが成功したので、ドキュメントの導入部分を触ってみる。
https://pester.dev/docs/quick-start

ドキュメントを「Splitting to tests and function」まで読み進めて、テストとテスト対象のスクリプトを分けて作成し、テストを走らせる。

Get-Planet.ps1
function Get-Planet ([string]$Name = '*') {
    $planets = @(
        @{ Name = 'Mercury' }
        @{ Name = 'Venus'   }
        @{ Name = 'Earth'   }
        @{ Name = 'Mars'    }
        @{ Name = 'Jupiter' }
        @{ Name = 'Saturn'  }
        @{ Name = 'Uranus'  }
        @{ Name = 'Neptune' }
    ) | ForEach-Object { [PSCustomObject] $_ }

    $planets | Where-Object { $_.Name -like $Name }
}
Get-Planet.Tests.ps1

BeforeAll {
    . $PSScriptRoot/Get-Planet.ps1
}

Describe 'Get-Planet' {
    It 'Given no parameters, it lists all 8 planets' {
        $allPlanets = Get-Planet
        $allPlanets.Count | Should -Be 8
    }
}
PS /Users/xxx/powershell> Invoke-Pester -Output Detailed Get-Planet.Tests.ps1
Pester v5.6.0

Starting discovery in 1 files.
Discovery found 1 tests in 2ms.
Running tests.

Running tests from '/Users/xxx/powershell/Get-Planet.Tests.ps1'
Describing Get-Planet
  [+] Given no parameters, it lists all 8 planets 4ms (3ms|1ms)
Tests completed in 18ms
Tests Passed: 1, Failed: 0, Skipped: 0, Inconclusive: 0, NotRun: 0

テストは成功。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0