DEV Community

Alexey Melezhik
Alexey Melezhik

Posted on • Edited on

2

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.

Reinvent your career. Join DEV.

It takes one minute and is worth it for your career.

Get started

Top comments (0)

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay