1

POV: I'm generating Powershell test code coverage with pester.

Problem: The generated Coverage.xml file remains empty.

Question What am I doing wrong?

Some technicalities:

    Write-Verbose -Message "Running Pester Tests"
    $conf = New-PesterConfiguration
    $conf.Run.Path= ".\Tests\"
    $conf.Run.TestExtension = '.Unit.Test.ps1'
    $conf.Output.CIFormat = "AzureDevops"
    $conf.TestResult.Enabled = $true
    $conf.TestResult.OutputPath = './TestResults/Testresult.xml'
    $conf.CodeCoverage.Enabled = $true
    $conf.CodeCoverage.OutputFormat = 'JaCoCo'
    $conf.CodeCoverage.CoveragePercentTarget = 70
    $conf.CodeCoverage.RecursePaths = $true
    $conf.CodeCoverage.OutputPath="./TestResults/Coverage.xml"
    $conf.CodeCoverage.Path=".\Tests\*.Unit.Test.ps1"
        
    Invoke-Pester -Configuration $conf
> Get-Module Pester | Where-Object {$_.Version -gt '5.0.0'}

ModuleType Version    PreRelease Name                                ExportedCommands
---------- -------    ---------- ----                                ----------------
Script     5.4.0                 Pester                              {Add-ShouldOperator, AfterAll, AfterEach, Assert-MockCalled…}
> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.3.1
PSEdition                      Core
GitCommitId                    7.3.1
OS                             Microsoft Windows 10.0.22621
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
JDC
  • 1,569
  • 16
  • 33
  • 2
    I believe `$conf.CodeCoverage.Path=` should be path to code files and not to the test files – Daniel Jan 18 '23 at 04:16

1 Answers1

1

Changing this

$conf.CodeCoverage.Path="./Tests/*.Unit.Test.ps1"

to

 $conf.CodeCoverage.Path="./Source/**/*.ps1"

made it work.


The documentation is really obscure about this: https://pester.dev/docs/commands/New-PesterConfiguration

Run:

Path: Directories to be searched for tests, paths directly to test files, or combination of both. Default value: @('.')

CodeCoverage:

Path: Directories or files to be used for code coverage, by default the Path(s) from general settings are used, unless overridden here.
Default value: @()

The general settings say: path to test files. CodeCoverage says: path from general settings is used..

This made me believe CodeCoverage should point to the test files.

Yet their default settings don't even work.

This put me on the wrong track.

Thanks to Daniel for putting me on the correct track.


Correction

The file is generated, yet the coverage is incorreclty always 0%

JDC
  • 1,569
  • 16
  • 33
  • "Yet their default settings don't even work." - That's wrong. It says codecoverage will use the same as `Run.Path`, but your tests are in a separate folder structure = no source code found. I agree that the description isn't good though. Feel free to [submit an issue @ github](https://github.com/pester/Pester/issues/new/choose) or a [PR if you have a suggestion](https://github.com/pester/Pester/blob/d81fdbc46318d6c3486c241fa82d9749df5c2031/src/csharp/Pester/CodeCoverageConfiguration.cs#L48) :-) – Frode F. Feb 05 '23 at 13:55