Skip to content

Instantly share code, notes, and snippets.

@macrat
Created August 24, 2020 04:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save macrat/722f705118402507e024ec4dcf90425a to your computer and use it in GitHub Desktop.
Save macrat/722f705118402507e024ec4dcf90425a to your computer and use it in GitHub Desktop.
PowerShellスクリプトにユニットテストを埋め込むい
param(
[parameter(Mandatory, Position=0, ParameterSetName="Normal")]
[Int]$Number,
[parameter(ParameterSetName="Normal")]
[String]$OutputPath = "out.txt",
[parameter(ParameterSetName="Test")]
[switch]$Test = $false
)
function Get-Double([Int]$Number) {
$Number * 2
}
function Write-Result([String]$Path) {
$input > $Path
}
function Main {
Get-Double $Number | Write-Result $OutputPath
}
function Test {
Invoke-Pester -Script $MyInvocation.MyCommand.Path # 自分をテストスクリプトと見做してPesterを実行する
}
if ($Test) {
Test
} else {
Main
}
# ----- ここからテスト -----
Describe "Get-Double" {
It "2なら4" {
Get-Double 2 | Should Be 4
}
It "3なら6" {
Get-Double 3 | Should Be 6
}
}
Describe "Write-Result" {
It "2をoutに" {
echo 2 | Write-Result TestDrive:\out
Get-Content TestDrive:\out | Should Be 2
}
It "helloをworldに" {
echo hello | Write-Result TestDrive:\world
Get-Content TestDrive:\world | Should Be "hello"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment