< Back
New-FunctionPesterTest
Post
NAME New-FunctionPesterTest
SYNOPSIS
This Function Creates 2 Pester Test files for the Function being passed to it
SYNTAX
New-FunctionPesterTest -Function <String> -OutPath <String> [-WhatIf] [-Confirm] [<CommonParameters>]
New-FunctionPesterTest -Function <String> -ResolvedFunction <Object> -OutPath <String> [-PrivateFunction]
[-WhatIf] [-Confirm] [<CommonParameters>]
DESCRIPTION
This Function creates a skeleton pester test file for the Function that is being passed to it including whether the
Function has Parameters and creates some basic Pester Tests for these so that you can pull then in future have
oppottunity
to test the code you've written in more detail - This is a Non-Functional Pester Test called <Function>.Tests.ps1
The Function also creates a blank file in the same location for you to create your Functional Pester Tests and
this is created
called <Function>.Functional.Tests.ps1
PARAMETERS
-Function <String>
This Parameter takes a String input and is used in Both Parameter Sets
Required? true
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false
-ResolvedFunction <Object>
This should be passed the Function that you want to work with as an object making use of the following
$ResolvedFunction = Get-Command "Command"
Required? true
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false
-OutPath <String>
This is the location that you want to output all the module files to. It is recommended not to use the same
location as where the module is installed.
Also always check the files output what you expect them to.
Required? true
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false
-PrivateFunction [<SwitchParameter>]
This is a switch that is used to correctly export Private Functions and is used internally in
Export-AllModuleFunction
Required? false
Position? named
Default value False
Accept pipeline input? false
Accept wildcard characters? false
-WhatIf [<SwitchParameter>]
Required? false
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false
-Confirm [<SwitchParameter>]
Required? false
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false
<CommonParameters>
This cmdlet supports the common parameters: Verbose, Debug,
ErrorAction, ErrorVariable, WarningAction, WarningVariable,
OutBuffer, PipelineVariable, and OutVariable. For more information, see
about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).
INPUTS
OUTPUTS
-------------------------- EXAMPLE 1 --------------------------
PS C:\\>New-FunctionPesterTest -Function New-FunctionPesterTest -OutPath C:\\TextFile\\PesterHelpers\\
-------------------------- EXAMPLE 2 --------------------------
PS C:\\>Get-Command -Module MyModule | Select-Object -ExpandProperty Name | ForEach-Object { New-FunctionPesterTest
-Function $_ -OutPath C:\\TextFile\\PesterHelpers\\
-------------------------- EXAMPLE 3 --------------------------
PS C:\\>In this example we have a PrivateFunction that we need to export - In this case it is Get-CommonParameter
$Function = 'Get-CommonParameter'
$Module = 'PesterHelpers'
$ModuleData = Get-Module $module
However as it is a Private Function we need to run the following to essentially flush the function into our
available local scope
For this piece of code thanks goes to Bruce Payette @BrucePayette
$AllFunctions = & $moduleData {Param($modulename) Get-command -CommandType Function -Module $modulename} $module
$ResolvedFunction = $AllFunctions.Where{ $_.Name -eq $function}
New-FunctionPesterTest -Function $Function -ResolvedFunction $ResolvedFunction -PrivateFunction -OutPath $OutPath
-Verbose
However it is unlikely that you would need to run something similar to this example though this is added for
completeness and
should help in understanding the story of what happens under the hood
-------------------------- EXAMPLE 4 --------------------------
PS C:\\>New-FunctionPesterTest -Function 'New-FunctionPesterTest -OutPath C:\\TextFile -Verbose
This is useful for when you have created a Function and have no tests and could potentially be used with simple
scripts that really just encapsulated as 1 function
RELATED LINKS
SYNOPSIS
This Function Creates 2 Pester Test files for the Function being passed to it
SYNTAX
New-FunctionPesterTest -Function <String> -OutPath <String> [-WhatIf] [-Confirm] [<CommonParameters>]
New-FunctionPesterTest -Function <String> -ResolvedFunction <Object> -OutPath <String> [-PrivateFunction]
[-WhatIf] [-Confirm] [<CommonParameters>]
DESCRIPTION
This Function creates a skeleton pester test file for the Function that is being passed to it including whether the
Function has Parameters and creates some basic Pester Tests for these so that you can pull then in future have
oppottunity
to test the code you've written in more detail - This is a Non-Functional Pester Test called <Function>.Tests.ps1
The Function also creates a blank file in the same location for you to create your Functional Pester Tests and
this is created
called <Function>.Functional.Tests.ps1
PARAMETERS
-Function <String>
This Parameter takes a String input and is used in Both Parameter Sets
Required? true
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false
-ResolvedFunction <Object>
This should be passed the Function that you want to work with as an object making use of the following
$ResolvedFunction = Get-Command "Command"
Required? true
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false
-OutPath <String>
This is the location that you want to output all the module files to. It is recommended not to use the same
location as where the module is installed.
Also always check the files output what you expect them to.
Required? true
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false
-PrivateFunction [<SwitchParameter>]
This is a switch that is used to correctly export Private Functions and is used internally in
Export-AllModuleFunction
Required? false
Position? named
Default value False
Accept pipeline input? false
Accept wildcard characters? false
-WhatIf [<SwitchParameter>]
Required? false
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false
-Confirm [<SwitchParameter>]
Required? false
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false
<CommonParameters>
This cmdlet supports the common parameters: Verbose, Debug,
ErrorAction, ErrorVariable, WarningAction, WarningVariable,
OutBuffer, PipelineVariable, and OutVariable. For more information, see
about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).
INPUTS
OUTPUTS
-------------------------- EXAMPLE 1 --------------------------
PS C:\\>New-FunctionPesterTest -Function New-FunctionPesterTest -OutPath C:\\TextFile\\PesterHelpers\\
-------------------------- EXAMPLE 2 --------------------------
PS C:\\>Get-Command -Module MyModule | Select-Object -ExpandProperty Name | ForEach-Object { New-FunctionPesterTest
-Function $_ -OutPath C:\\TextFile\\PesterHelpers\\
-------------------------- EXAMPLE 3 --------------------------
PS C:\\>In this example we have a PrivateFunction that we need to export - In this case it is Get-CommonParameter
$Function = 'Get-CommonParameter'
$Module = 'PesterHelpers'
$ModuleData = Get-Module $module
However as it is a Private Function we need to run the following to essentially flush the function into our
available local scope
For this piece of code thanks goes to Bruce Payette @BrucePayette
$AllFunctions = & $moduleData {Param($modulename) Get-command -CommandType Function -Module $modulename} $module
$ResolvedFunction = $AllFunctions.Where{ $_.Name -eq $function}
New-FunctionPesterTest -Function $Function -ResolvedFunction $ResolvedFunction -PrivateFunction -OutPath $OutPath
-Verbose
However it is unlikely that you would need to run something similar to this example though this is added for
completeness and
should help in understanding the story of what happens under the hood
-------------------------- EXAMPLE 4 --------------------------
PS C:\\>New-FunctionPesterTest -Function 'New-FunctionPesterTest -OutPath C:\\TextFile -Verbose
This is useful for when you have created a Function and have no tests and could potentially be used with simple
scripts that really just encapsulated as 1 function
RELATED LINKS