DEV Community

Alexey Melezhik
Alexey Melezhik

Posted on • Edited on

Testing Powershell code with Tomty framework.

There is more then one way to test it ...

How to test Powershell modules using Perl6 and Tomty

Welcome Tomty

Tomty is a Sparrow6 based testing framework, written in Perl6. We use it to write tests.

Install

zef install Tomty
Enter fullscreen mode Exit fullscreen mode

Usage

Write Powershell module

modules/hello/hello.ps1

function Hello {

  param(
    [Parameter(Mandatory=$true)]
    [String]$name
  )

  Write-Host Hello $name
}
Enter fullscreen mode Exit fullscreen mode

Write wrapper

mkdir -p .tomty/tasks/test-hello
Enter fullscreen mode Exit fullscreen mode

.tomty/tasks/test-hello/task.ps1:

Import-Module hello
$name = config 'name'
Hello -name $name
Enter fullscreen mode Exit fullscreen mode

.tom/tasks/test-hello/task.check:

generator: print "Hello ".( config()->{name} )
Enter fullscreen mode Exit fullscreen mode

Write test

tomty --edit test-hello

task-run ".tomty/tasks/test-hello", %( name => "Sparrow6" )
Enter fullscreen mode Exit fullscreen mode

Run test

PSModulePath=$PWD/modules tomty test-hello

02:47:26 07/27/2019 [repository] index updated from file:///home/melezhik/repo/api/v1/index
02:47:26 07/27/2019 [.tom/tasks/test-hello] Hello Sparrow6
[task check] stdout match <Hello Sparrow6> True
Enter fullscreen mode Exit fullscreen mode

Run more tests

Once you have more tests, you run them all:

tomty --all

See Tomty documentation for other options.


Thank you for reading. I'd appreciate comments.

Top comments (0)