Coverlet: Is it possible to generate code coverage for PowerShell cmdlets?

Created on 3 Sep 2018  路  6Comments  路  Source: coverlet-coverage/coverlet

I have PowerShell cmdlets written in C# that I am testing with Pester (i.e. the tests are written in PowerShell). Is there a way to use coverlet to track code coverage within the cmdlets while executing Pester tests?

All 6 comments

This is what I would try with coverlet, though no guarantees --

Given the coverlet doesn't instrument the assembly directly named, the test system would need to throw coverlet a bone by having a shim assembly project, linking to the one(s) of interest, that takes the place of the unit-test assembly. Then the command-line invocation

coverlet /path/to/shim-assembly.dll --target "pwsh" --targetargs "path/to/test/script.ps1 ..."

where script.ps1 would be something trivial like

param(...)
Import-Module "path/to/Pester.psm1"
Import-Module "$(ShimOutput)/RealCmdlets.dll" # not the shim itself, but the interesting assembly in its output folder

Invoke-Pester -Script @{ Path='.path/to/pester/tests'; Parameters = @{...}} -EnableExit -OutputFormat NUnitXml -OutputFile "PesterReport.xml"

should execute the instrumented code for coverlet to process.

I've had success with https://github.com/lucaslorentz/minicover, which does not require the concept of a "test assembly" (it just instruments the assemblies you pass it). Feel free to close this issue, or to keep it open as a feature request.

@felixfbecker glad minicover was able to help you achieve what you needed.

@felixfbecker I am a newbie trying to get code coverage report for my Cmdlet code written in C# and I am using Pester for unit tests. I tried looking at minicover based on your suggestion but couldn't get it working. Is there a reference that gives a gist of steps to incorporate minicover.

Sorry if this question is not relevant to this repo. Posting it here for the context.

@arunswarnam here's where I used it: https://github.com/felixfbecker/PSKubectl/blob/master/ci/Invoke-Tests.ps1
The coverage report gets uploaded to Codecov.

Thank you @felixfbecker. It helped!

Was this page helpful?
0 / 5 - 0 ratings