LoginSignup
9
9

More than 5 years have passed since last update.

PowerShellプログラムのおすすめディレクトリ構成とユニットテストメモ

Last updated at Posted at 2016-03-22

ユニットテストリンク集

おすすめディレクトリ構成

この構成はPesterの実装からいただきました。

+---Invoke-MyCommmand.ps1
+---Invoke-MyCommands.Tests.ps1
\---Functions
    +---function1.ps1
    +---function1.Tests.ps1
    +---function2.ps1
    +---function2.Tests.ps1
    \---(続く……)

Invoke-MyCommand.ps1が本体プログラムです。

Functionsの下に関数を置き、本体プログラムから読み込みます。

ユニットテストは.Tests.ps1という拡張子をつけます。

プログラムのひな形

Template.ps1
<#
.SYNOPSIS
プログラムの短い説明

.DESCRIPTION
プログラムの詳しい説明
#>
Set-StrictMode -Version Latest

$moduleRoot = Split-Path -Parent $MyInvocation.MyCommand.Path

# 関数を読み込み
"$moduleRoot\Functions\*.ps1" |
Resolve-Path |
Where-Object { -not ($_.ProviderPath.ToLower().Contains(".tests.")) } |
ForEach-Object { . $_.ProviderPath }

ユニットテストのひな形

Template.Tests.ps1
Set-StrictMode -Version Latest

$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".")
. "$here\$sut"

Describe "function1" {
    It "returns true" {
        # テストコード
    }
}
9
9
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
9
9