Skip to content

Instantly share code, notes, and snippets.

@nohwnd
nohwnd / run.ps1
Last active May 16, 2021
Compare CC in Pester
View run.ps1
$c = New-PesterConfiguration
# your Path
$c.Run.Path = "."
$c.Run.PassThru = $true
# detailed prints the CC report, but that is very verbose
# use only for tiny codebases
# $c.Output.Verbosity = "Detailed"
@nohwnd
nohwnd / .gitignore
Last active Jun 8, 2021
Use Pester code coverage with CoverageGutters in VSCode
View .gitignore
coverage.xml
View defining-mocks-across-modules.ps1
# 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 Apr 16, 2021
Bunching expensive setup in Pester
View discover-setup-run.ps1
## 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 Apr 15, 2021
Writing output per test in MSTest
View UnitTest1.cs
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 Apr 9, 2021
Get parameter value from Mock without using $script: scope
View get-parmeter-value.tests.ps1
# 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 Apr 8, 2021
Get and set variable into the caller scope from a module scope
View get-set-variable.ps1
## 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()]
View get-filter-closure.ps1
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.
& {
View Pester bug report
$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 Sep 21, 2020
Mocking static property getter using Harmony
View Mocking static property getter
$harmony = "$PSScriptRoot/0Harmony.dll"
Import-Module $harmony
$Script:Patches = @()
function Set-StaticPropertyGetter {
param (
[Parameter(Mandatory)]
[Type] $Type,