Skip to content

Instantly share code, notes, and snippets.

# ModuleName defines in which session state the mock will be effective, not in which module the function was defined.
# This is important when mocking functions exported from a module either when calling them from a script, or when calling
# them from a different module. In that case you want to define the mock in the place where the function is called from,
# not in the module where the function is defined.
# The only time you want to define the mock in the module where the function is defined is when you are testing an internal
# functions of the module (not shown here).
Invoke-Pester -Container (
New-PesterContainer -ScriptBlock {
BeforeAll {
Get-Module m, n, o | Remove-Module
@nohwnd
nohwnd / discover-setup-run.ps1
Last active April 16, 2021 10:36
Bunching expensive setup in Pester
## Run only discovery, select tests that need really expensive setup that takes
# long time to be present in the target system. E.g. 30 minutes per file to be updated
# in a remote resource. And run just the discovery, setup all tests that would run across
# all files (we use only one here but it does not matter).
# And then run the tests, communicating back which resources were created so we get failures
# in their respective tests.
Import-Module Pester -RequiredVersion 5.2.0 -Force
$configuration = [PesterConfiguration]::Default
@nohwnd
nohwnd / UnitTest1.cs
Created April 15, 2021 08:31
Writing output per test in MSTest
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Linq;
using System.Collections.Generic;
using System.IO;
@nohwnd
nohwnd / get-parmeter-value.tests.ps1
Created April 9, 2021 09:17
Get parameter value from Mock without using $script: scope
# Keeping a value from a mock call, or some other place
# while avoiding $script: to keep script scope clean
Describe "a" {
It 'Test' {
function Get-GitHubRelease ($Release) { }
# use a reference object instead of directly assiging to a
# variable to collect the values from a mock call
$container = @{}
@nohwnd
nohwnd / get-set-variable.ps1
Last active April 8, 2021 09:32
Get and set variable into the caller scope from a module scope
## Getting and setting variables from within a module into the calling scope
# make sure we re-import the module when experimenting with this
# to get our functions updated
Get-Module m | Remove-Module
New-Module m -ScriptBlock {
function Get-CallerVariable {
[CmdletBinding()]
@nohwnd
nohwnd / get-filter-closure.ps1
Last active April 7, 2021 15:22
Generating filter
function Get-Filter ($Predicate) {
# gets the property hashtable of an object
$properties = $Predicate.PSObject.Properties
# runs the code below in a scriptblock to ensure that we only capture
# the desired $Properties variable in case we would have more variables
# in this function. This is not strictly necessary, but closure only captures
# local variables so it is useful trick to limit the variables that we capture.
& {
$m = Get-Command Invoke-Pester -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Module
$psv = $PSVersionTable.PSVersion
$pre = $m.PrivateData -and $m.PrivateData.PSData -and $m.PrivateData.PSData.PreRelease
$pv = if ($pre) { "$($m.Version)-$($m.PrivateData.PSData.PreRelease)" } else { $m.Version }
"Pester version : " + $pv + " " + $m.Path
"PowerShell version : " + $psv
"OS version : " + [System.Environment]::OSVersion.VersionString
@nohwnd
nohwnd / Mocking static property getter
Created September 21, 2020 13:09
Mocking static property getter using Harmony
$harmony = "$PSScriptRoot/0Harmony.dll"
Import-Module $harmony
$Script:Patches = @()
function Set-StaticPropertyGetter {
param (
[Parameter(Mandatory)]
[Type] $Type,
@nohwnd
nohwnd / passing-values-from-discovery.tests.ps1
Last active May 27, 2020 07:52
Passing values from Discovery to Run when generating tests
Describe "a" {
$Sources = @(
[PSCustomObject]@{
Advanced = @{
Enabled = $true
}
}
[PSCustomObject]@{
Advanced = @{
Enabled = $true
@nohwnd
nohwnd / add-beforeall.ps1
Created April 4, 2020 16:05
Migrate from Pester v4 to v5
# Adds BeforeAll at the top of Tests file to make it follow Pester v5 recommendation of putting
# all code into Pester controlled blocks.
# DO this:
# BeforeAll {
# . $PSScriptRoot/Code.ps1
# }
# DO this:
# BeforeAll {