PowerShell Pester テストでタグを使用する方法

PowerShell Pester テストでタグを使用する方法

投稿者: Adam Bertram
投稿日: 2019年2月4 0 Comments

Pester の便利な機能の1つはタグ機能です。タグを使用すると、さまざまな基準に基づいてテストを整理し、それに従って実行できます。タグに一致する特定のテストのみを実行することが可能になり、実行に数時間かかることがある大規模なテスト群を整理立てて管理できます。

このブログでは、Pester テストでのタグの使い方を説明します。Pester は PowerShell の事実上のユニットテストのフレームワークです。開発者だけに限らず、信頼性の高いテスト済みコードを PowerShell で作成することができます。

前提として、Windows サーバーの状態をテストする Pester テストが作成済みだとします。新しいテストラボを作成する PowerShell スクリプトも作成されているとします。このスクリプトは、仮想マシンを作成し、Windows の機能をインストールし、複数の Active Directory ユーザーを作成します。シナリオは実際は何でも構いませんが、重要なのは、確認したい状況が1つ以上あるという点です。

関連ブログ: Azure DevTest Labs でラボを作成する方法

TEST-DCという仮想マシンがオンラインのとき、そのVMには Windows 機能の AD-Domain-Services がインストールされていて、ドメインには ”abertram” というユーザーと “dtrump” というユーザーが作成されていることを確認済みだと仮定して話を進めましょう。このシナリオでは、タグを使って Pester テストを作成します。

まず、describeit ブロック構文を使って Pester テストを作成します。以下のダミーテストは、一般的なインフラストラクチャテストの例です。テストには3つの「レベル」があります。 VMの作成に対するテスト、VMの Active Directory のセットアップに対するテスト、そして Active Directory に意図したユーザーが作成されていることを確認するためのテストです。テストが何であるかは重要ではなく、異なる「レベル」のテストがあることが重要です。

describe 'VM Creation' {

    it 'the TEST-DC VM should exist' {
        Get-VM -Name TEST-DC -ErrorAction Ignore | should not benullorempty
    }
}

describe 'Forest Provisioning' {

    it 'the Active Directory Windows feature should be installed on TEST-DC' {
        Get-WindowsFeature -ComputerName TEST-DC -Name 'AD-Domain-Services' | where { $_.Installed } | should not benullorempty
    }

}

describe 'Domain User Creation' {

    it 'the Active Directory forest should have the expected users created' {
        $users = Invoke-Command -ComputerName TEST-DC  -ScriptBlock {
            Get-AdUser -Filter * | where {$_.Name -in @('abertram','dtrump')}
        }
        $users.Count | should be 2
    }
}

すべてのテストが C:.Tests.ps1 というファイルに作成されているとしましょう。これらのテストを Invoke-Pester コマンド (Invoke-Pester -Path C:\TestDomain.Tests.ps1) を使用して実行すると、すべてのテストが同時に実行されます。この例では、それぞれの describe ブロックに単一のテストがあるだけなので大きな問題ではありませんが、もし何百ものテストがあったとしたらどうでしょうか。すべて同時にではなく、特定のテストだけを実行できるようにする必要があります。タグを使えば、それが可能になります。

テストが多岐にわたれば、複数の人が異なるテストフェーズを担当する場合もあるでしょう。そういった状況を想定すると、VM作成テストやドメインユーザー作成テストのみを単独で実行できると便利です。それぞれの describe ブロックにタグを割り当てて、実行するテストを選択することができます。

describe -Tag 'VM' 'VM Creation' {

    it 'the TEST-DC VM should exist' {
        Get-VM -Name TEST-DC -ErrorAction Ignore | should not benullorempty
    }
}

describe -Tag 'Active Directory Set Up' 'Forest Provisioning' {

    it 'the Active Directory Windows feature should be installed on TEST-DC' {
        Get-WindowsFeature -ComputerName TEST-DC -Name 'AD-Domain-Services' | where { $_.Installed } | should not benullorempty
    }

}

describe -Tag 'Active Directory Object Creation' 'Domain User Creation' {

    it 'the Active Directory forest should have the expected users created' {
        $users = Invoke-Command -ComputerName TEST-DC  -ScriptBlock {
            Get-AdUser -Filter * | where {$_.Name -in @('abertram','dtrump')}
        }
        $users.Count | should be 2
    }
}

それぞれのブロックにタグを追加したので、VM作成のテストだけを呼び出すには、Invoke-Pester -Path C:\TestDomain.Tests.ps1 -Tag 'VM' を使い、Active Directory セットアップのテストには、‘VM’ を ‘Active Directory Set Up’ に置き換えて呼び出すことができます。また、特定のタグのみを除外してテストを実行するには、ExcludeTag パラメータを使って、Invoke-Pester -Path C:\TestDomain.Tests.ps1 -ExcludeTag 'VM' とします。

pester

Pester タグはシンプルな概念ですが、大規模なテスト群を整理立てて管理するには最適です。利用者の視点から自由にテストを分類して分離でき、複数の利用者が同じスクリプトからお互いに干渉することなく独自にテストを実行できるので、全体的なテスト進行も効率化します。

Adam Bertram

Adam Bertram is a 20-year veteran of IT. He’s currently an automation engineer, blogger, independent consultant, freelance writer, author, and trainer. Adam focuses on DevOps, system management, and automation technologies as well as various cloud platforms. He is a Microsoft Cloud and Datacenter Management MVP and efficiency nerd that enjoys teaching others a better way to leverage automation.

コメント

Comments are disabled in preview mode.
トピック

Sitefinityトレーニングと認定を開始

クラス最高のSitefinityの機能を使って、魅力的なデジタル体験を提供する方法をエキスパートがお教えします。

さらに詳しく

より優れた業務アプリケーションやウェブサイトの開発に役立つ、ニュース、情報、チュートリアルをご案内します。