Skip to content
This repository has been archived by the owner on Feb 24, 2021. It is now read-only.

Commit

Permalink
adding new templates
Browse files Browse the repository at this point in the history
  • Loading branch information
mbreakey3 committed Aug 3, 2016
1 parent 4d37595 commit 14868d0
Show file tree
Hide file tree
Showing 2 changed files with 234 additions and 0 deletions.
99 changes: 99 additions & 0 deletions Tests.Template/unit_template1.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<#
.Synopsis
A sample template for creating DSC Resource Unit Tests mainly for smaller resources
.DESCRIPTION
To Use:
1. Copy to \Tests\Unit\ folder and rename <ResourceName>.tests.ps1 (e.g. MSFT_xFirewall.tests.ps1)
2. Customize TODO sections.
3. Delete all of the template instructional comments before pushing to git repository
.NOTES
Code in HEADER and FOOTER regions are standard and may be moved into DSCResource.Tools in
Future and therefore should not be altered if possible.
#>


# TODO: Customize these parameters...
$script:DSCModuleName = '<ModuleName>' # Example xNetworking
$script:DSCResourceName = '<ResourceName>' # Example MSFT_xFirewall
# /TODO

#region HEADER

# Unit Test Template Version: 1.1.0
[String] $script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot)
if ( (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests'))) -or `
(-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1'))) )
{
& git @('clone','https://github.com/PowerShell/DscResource.Tests.git',(Join-Path -Path $script:moduleRoot -ChildPath '\DSCResource.Tests\'))
}

Import-Module (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1') -Force
$TestEnvironment = Initialize-TestEnvironment `
-DSCModuleName $script:DSCModuleName `
-DSCResourceName $script:DSCResourceName `
-TestType Unit

#endregion HEADER

# TODO: Other Optional Init Code Goes Here...

# Begin Testing
try
{
#region Pester Test Initialization

# TODO: Optionally create any variables here for use by your tests
# See https://github.com/PowerShell/xNetworking/blob/dev/Tests/Unit/MSFT_xDhcpClient.Tests.ps1

#endregion Pester Test Initialization

# TODO: Common DSC Resource describe block structure
# The following three Describe blocks are included as a common test pattern.
# If a different test pattern would be more suitable, then test describe blocks
# may be completely replaced. The goal of this pattern should be to describe
# the potential states a system could be in for each function.


#region Function Get-TargetResource
Describe "$($Global:DSCResourceName)\Get-TargetResource" {
# TODO: Complete Get-TargetResource Tests...
}
#endregion Function Get-TargetResource

#region Function Set-TargetResource
Describe "$($Global:DSCResourceName)\Set-TargetResource" {
# TODO: Complete Set-TargetResource Tests...
}
#endregion Function Set-TargetResource

#region Function Test-TargetResource
Describe "$($Global:DSCResourceName)\Test-TargetResource" {
# TODO: Complete Test-TargetResource Tests...
}
#endregion Function Test-TargetResource



#region Non-Exported Function Unit Tests

# TODO: Pester Tests for any non-exported Helper Cmdlets
# If the resource does not contain any non-exported helper cmdlets then
# this block may be safetly deleted.
InModuleScope $script:DSCResourceName {
# The InModuleScope command allows you to perform white-box unit testing
# on the internal (non-exported) code of a Script Module.

}
#endregion Non-Exported Function Unit Tests
}
finally
{
#region FOOTER

Restore-TestEnvironment -TestEnvironment $TestEnvironment

#endregion

# TODO: Other Optional Cleanup Code Goes Here...
}
135 changes: 135 additions & 0 deletions Tests.Template/unit_template2.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<#
.Synopsis
A sample template for creating DSC Resource Unit Tests mainly for larger resources
.DESCRIPTION
To Use:
1. Copy to \Tests\Unit\ folder and rename <ResourceName>.tests.ps1 (e.g. MSFT_xFirewall.tests.ps1)
2. Customize TODO sections.
3. Delete all of the template instructional comments before pushing to git repository
.NOTES
Code in HEADER and FOOTER regions are standard and may be moved into DSCResource.Tools in
Future and therefore should not be altered if possible.
#>


# TODO: Customize these parameters...
$script:DSCModuleName = '<ModuleName>' # Example xNetworking
$script:DSCResourceName = '<ResourceName>' # Example MSFT_xFirewall
# /TODO

#region HEADER

# Unit Test Template Version: 1.1.0
[String] $script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot)
if ( (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests'))) -or `
(-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1'))) )
{
& git @('clone','https://github.com/PowerShell/DscResource.Tests.git',(Join-Path -Path $script:moduleRoot -ChildPath '\DSCResource.Tests\'))
}

Import-Module (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1') -Force
$TestEnvironment = Initialize-TestEnvironment `
-DSCModuleName $script:DSCModuleName `
-DSCResourceName $script:DSCResourceName `
-TestType Unit

#endregion HEADER

# TODO: Other Optional Init Code Goes Here...

# Begin Testing
try
{
#region Pester Test Initialization

# TODO: Optionally create any variables here for use by your tests
# See https://github.com/PowerShell/xNetworking/blob/dev/Tests/Unit/MSFT_xDhcpClient.Tests.ps1

#endregion Pester Test Initialization

# TODO: Common DSC Resource describe block structure
# The following two Describe blocks are included as a common test pattern.
# If a different test pattern would be more suitable, then test describe blocks
# may be completely replaced. The goal of this pattern should be to describe
# the potential states a system could be in so that the get/test/set cmdlets
# can be tested in those states. Any mocks that relate to that specific state
# can be included in the relevant describe block. For a more detailed description
# of this approach please review https://github.com/PowerShell/DscResources/issues/143

# Add as many of these example 'states' as required to simulate the scenarions that
# the DSC resource is designed to work with, below a simple "is in desired state" and
# "is not in desired state" are used, but there may be more complex combinations of
# factors, depending on how complex your resource is.

#region Example state 1
Describe 'The system is not in the desired state' {
#TODO: Mock cmdlets here that represent the system not being in the desired state

#TODO: Create a set of parameters to test your get/test/set methods in this state
$testParameters = @{
Property1 = 'value'
Property2 = 'value'
}

#TODO: Update the assertions below to align with the expected results of this state
It 'Get method returns something' {
Get-TargetResource @testParameters | Should Be 'something'
}

It 'Test method returns false' {
Test-TargetResource @testParameters | Should be $false
}

It 'Set method calls Demo-CmdletName' {
Set-TargetResource @testParameters

#TODO: Assert that the appropriate cmdlets were called
Assert-MockCalled Demo-CmdletName
}
}
#endregion Example state 1

#region Example state 2
Describe 'The system is in the desired state' {
#TODO: Mock cmdlets here that represent the system being in the desired state

#TODO: Create a set of parameters to test your get/test/set methods in this state
$testParameters = @{
Property1 = 'value'
Property2 = 'value'
}

#TODO: Update the assertions below to align with the expected results of this state
It 'Get method returns something' {
Get-TargetResource @testParameters | Should Be 'something'
}

It 'Test method returns true' {
Test-TargetResource @testParameters | Should be $true
}
}
#endregion Example state 2

#region Non-Exported Function Unit Tests

# TODO: Pester Tests for any non-exported Helper Cmdlets
# If the resource does not contain any non-exported helper cmdlets then
# this block may be safetly deleted.
InModuleScope $script:DSCResourceName {
# The InModuleScope command allows you to perform white-box unit testing
# on the internal (non-exported) code of a Script Module.

}
#endregion Non-Exported Function Unit Tests
}
finally
{
#region FOOTER

Restore-TestEnvironment -TestEnvironment $TestEnvironment

#endregion

# TODO: Other Optional Cleanup Code Goes Here...
}

0 comments on commit 14868d0

Please sign in to comment.