Skip to content

Instantly share code, notes, and snippets.


Jakub Jareš nohwnd

View GitHub Profile
nohwnd / UnitTest1.cs
Last active Apr 8, 2022
MSTest parallel output demo
View UnitTest1.cs
using System.Diagnostics;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[assembly: Parallelize(Workers = 0, Scope = ExecutionScope.MethodLevel)]
namespace mstest;
public class UnitTest1
nohwnd / Internal.cs
Created Aug 6, 2021
Find internal classes
View Internal.cs
using System;
using System.Linq;
using System.Reflection;
namespace ConsoleApp28
class Program
static void Main(string[] args)
nohwnd / script-block-local.ps1
Created Jul 2, 2021
scriptblock local storage
View script-block-local.ps1
# create a scriptblock that has scriptblock local data attached (data common to all invocation of the scriptblock)
# but is still bound to the same session state, this is differnt from closure, because closures are their own modules
$sb = {
$self = $MyInvocation.MyCommand.ScriptBlock
$sb.PSObject.Properties.Add([System.Management.Automation.PSNoteProperty]::new("___data", @{ Count = 1}))
nohwnd / get-testparents.ps1
Last active Jun 21, 2021
View get-testparents.ps1
function Get-TestParents {
Returns any parents not already known, top-down first, so that a hierarchy can be created in a streaming manner
param (
#Test to fetch parents of. For maximum efficiency this should be done one test at a time and then stack processed
[HashSet[Pester.Block]]$KnownParents = [HashSet[Pester.Block]]::new()
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 / .gitignore
Last active Oct 29, 2021
Use Pester code coverage with CoverageGutters in VSCode
View .gitignore
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 / 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 / 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 / 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 = @{}