You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Code Coverage will report functions, but the samples and parameters to configure CodeCoverage on that page isn't valid for Pester 5. Try:
CoverageTest.ps1:
functionFunctionOne ([switch] $SwitchParam)
{
if ($SwitchParam)
{
return'SwitchParam was set'
}
else
{
return'SwitchParam was not set'
}
}
functionFunctionTwo
{
return'I get executed'return'I do not'
}
CoverageTest.Tests.ps1:
BeforeAll {
.$PSCommandPath.Replace('.Tests.ps1','.ps1')
}
Describe 'Demonstrating Code Coverage' {
It 'Calls FunctionOne with no switch parameter set' {
FunctionOne | Should -Be 'SwitchParam was not set'
}
It 'Calls FunctionTwo' {
FunctionTwo | Should -Be 'I get executed'
}
}
Then invoke like this:
$c=New-PesterConfiguration# Just hardcoding a single test path to keep it simple. Can use wildcards etc as normal$c.Run.Path="C:\Path\To\Tests\CoverageTest.Tests.ps1"$c.CodeCoverage.Enabled=$true# Just hardcoding a single test path to keep it simple. Can use wildcards etc as normal$c.CodeCoverage.Path="C:\Path\To\Source\CoverageTest.ps1"# Not required, but will print the report automatically$c.Output.Verbosity='Detailed'# Not required, but will let you access the report in the result-object, see below$c.Run.PassThru=$true$res=Invoke-Pester-Configuration $c# Output:
Pester v5.3.3
Starting discovery in1 files.
Discovery found 2 tests in 4ms.
Starting code coverage.
Code Coverage preparation finished after 4 ms.
Running tests.
Running tests from'C:\Path\To\Tests\CoverageTest.Tests.ps1'
Describing Demonstrating Code Coverage
[+] Calls FunctionOne with no switch parameter set 12ms (1ms|10ms)
[+] Calls FunctionTwo 2ms (1ms|1ms)
Tests completed in 58ms
Tests Passed: 2, Failed: 0, Skipped: 0 NotRun: 0
Processing code coverage result.
Code Coverage result processed in8 ms.
Covered 60%/75%.5 analyzed Commands in1 File.
Missed commands:
File ClassFunction Line Command
----------------------------
CoverageTest.ps1 FunctionOne 5return'SwitchParam was set'
CoverageTest.ps1 FunctionTwo 16return'I do not'# Access the report as a string if you want# $res.CodeCoverage.CoverageReport
I'm going to book mark this and try it when I can get back to my rig.
Verbosity is probably the option I was struggling to find.
when I tried this locally by placing the files in the following tree structure:
(Removed typo didn't have S after CoverageTest)
c:\temp\powershell
\source\CoverageTest.ps1
\tests\CoverageTest.Tests.ps1
run from the powershell folder was giving me an error with the following output:
Pester v5.3.3
Starting discovery in 1 files.
Discovery found 2 tests in 11ms.
Starting code coverage.
Code Coverage preparation finished after 28 ms.
Running tests.
Running tests from 'C:\temp\powershell\tests\CoverageTest.Tests.ps1'
[-] C:\temp\powershell\tests\CoverageTest.Tests.ps1 failed with:
CommandNotFoundException: The term 'C:\temp\powershell\tests\CoverageTest.ps1' is not recognized as the name of a cmdlet, function, sc
ript file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try aga
in.
at <ScriptBlock>, C:\temp\powershell\tests\CoverageTest.Tests.ps1:2
Tests completed in 86ms
Tests Passed: 0, Failed: 2, Skipped: 0 NotRun: 0
Container failed: 1
- C:\temp\powershell\tests\CoverageTest.Tests.ps1
Processing code coverage result.
Code Coverage result processed in 27 ms.
Covered 0% / 75%. 5 analyzed Commands in 1 File.
Missed commands:
File Class Function Line Command
---- ----- -------- ---- -------
CoverageTest.ps1 FunctionOne 3 if ($SwitchParam)...
CoverageTest.ps1 FunctionOne 5 return 'SwitchParam was set'
CoverageTest.ps1 FunctionOne 9 return 'SwitchParam was not set'
CoverageTest.ps1 FunctionTwo 15 return 'I get executed'
CoverageTest.ps1 FunctionTwo 16 return 'I do not'
Using this as the config setup:
$c = New-PesterConfiguration
# Just hardcoding a single test path to keep it simple. Can use wildcards etc as normal
$c.Run.Path = "C:\temp\powershell\tests\CoverageTest.Tests.ps1"
$c.CodeCoverage.Enabled = $true
# Just hardcoding a single test path to keep it simple. Can use wildcards etc as normal
$c.CodeCoverage.Path = "C:\temp\powershell\source\CoverageTest.ps1"
# Not required, but will print the report automatically
$c.Output.Verbosity = 'Detailed'
# Not required, but will let you access the report in the result-object, see below
$c.Run.PassThru = $true
Everything else basically identical to what was written in the previous post.
On Sat, Jul 2, 2022 at 2:46 AM Frode Flaten ***@***.***> wrote:
Code Coverage will report functions, but the samples and parameters to
configure CodeCoverage on that page isn't valid for Pester 5. Try:
CoverageTest.ps1:
function FunctionOne ([switch] $SwitchParam)
{
if ($SwitchParam)
{
return 'SwitchParam was set'
}
else
{
return 'SwitchParam was not set'
}
}
function FunctionTwo
{
return 'I get executed'
return 'I do not'
}
CoverageTest.Tests.ps1:
BeforeAll {
. $PSCommandPath.Replace('.Tests.ps1','.ps1')
}
Describe 'Demonstrating Code Coverage' {
It 'Calls FunctionOne with no switch parameter set' {
FunctionOne | Should -Be 'SwitchParam was not set'
}
It 'Calls FunctionTwo' {
FunctionTwo | Should -Be 'I get executed'
}
}
Then invoke like this:
$c = New-PesterConfiguration# Just hardcoding a single test path to keep it simple. Can use wildcards etc as normal$c.Run.Path = "C:\Path\To\Tests\CoverageTest.Tests.ps1"$c.CodeCoverage.Enabled = $true# Just hardcoding a single test path to keep it simple. Can use wildcards etc as normal$c.CodeCoverage.Path = "C:\Path\To\Source\CoverageTest.ps1"# Not required, but will print the report automatically$c.Output.Verbosity = 'Detailed'# Not required, but will let you access the report in the result-object, see below$c.Run.PassThru = $true
$res = Invoke-Pester -Configuration $c
# Output:
Pester v5.3.3
Starting discovery in 1 files.
Discovery found 2 tests in 4ms.
Starting code coverage.
Code Coverage preparation finished after 4 ms.
Running tests.
Running tests from 'C:\Path\To\Tests\CoverageTest.Tests.ps1'
Describing Demonstrating Code Coverage
[+] Calls FunctionOne with no switch parameter set 12ms (1ms|10ms)
[+] Calls FunctionTwo 2ms (1ms|1ms)
Tests completed in 58ms
Tests Passed: 2, Failed: 0, Skipped: 0 NotRun: 0
Processing code coverage result.
Code Coverage result processed in 8 ms.
Covered 60% / 75%. 5 analyzed Commands in 1 File.
Missed commands:
File Class Function Line Command---- ----- -------- ---- -------
CoverageTest.ps1 FunctionOne 5 return 'SwitchParam was set'
CoverageTest.ps1 FunctionTwo 16 return 'I do not'
# Access the report as a string if you want# $res.CodeCoverage.CoverageReport
—
Reply to this email directly, view it on GitHub
<#196 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAROLKSOIKM2Q7TNJLQUQRTVR7QVRANCNFSM5YOSCTYQ>
.
You are receiving this because you commented.Message ID:
***@***.***>
Okay, finally figured out what was happening on my end.
When I had the paths setup, I think it was looking in the wrong folders all along. I had the relative paths there, but it thought they were in C:\ by referencing the source and test folders specifically down to c drive I started getting the feedback this way.
I had a very frustrating day trying to setup code coverage, verbosity and other stuff. I volunteer to update the code coverage page, the options mentioned there are already deprecated. You could assign this to me or not, but I will create a PR soon.
@fflaten ignore the "Deprecated" section for now, I am thinking in something like this, early feedback? how much do we want to keep from the deprecated section? how much from the configuration do we want to mention, considering that if we duplicate the same information than in the config reference, we have now two places to maintain?
Great start! I'm not able to view it rendered on mobile, so just some quick notes:
Pester 5 is PSv3 minimum, not just CC
Duplicate introduction? First lines + Understanding... section
TestResult is not necessary for CC, so I would skip it in example
Should the steps in config example be a single code block with comments to explain why? What looks best rendered? Single line codeblocks might waste vertical space on margins.
We support CC without breakpoints also, so maybe remove breakpoint-reference? We'll probably have to add a section about when to use which mode later. @nohwnd knows this better than me.
how much do we want to keep from the deprecated section?
I personally like the structure it has, ex:
Why use code coverage?
Simple console sample (with output)
How to integrate in CI examples
The last section about analyzed commands might be nice as a <details> element or a last section "How to read the results", but we could add it later.
how much from the configuration do we want to mention, considering that if we duplicate the same information than in the config reference, we have now two places to maintain?
The parts used in the examples at least. The hidden docs in New-PesterConfiguration is just the code help which is really limited in length and formatting (bare minimum). This Code Coverage page is the "real docs" and marketing for the feature.
Activity
Veretax commentedon Jul 1, 2022
Is this related to why when I run a basic code coverage of one test to one file I no longer get the missed methods listing that this page shows?
fflaten commentedon Jul 2, 2022
Code Coverage will report functions, but the samples and parameters to configure CodeCoverage on that page isn't valid for Pester 5. Try:
CoverageTest.ps1:
CoverageTest.Tests.ps1:
BeforeAll { . $PSCommandPath.Replace('.Tests.ps1','.ps1') } Describe 'Demonstrating Code Coverage' { It 'Calls FunctionOne with no switch parameter set' { FunctionOne | Should -Be 'SwitchParam was not set' } It 'Calls FunctionTwo' { FunctionTwo | Should -Be 'I get executed' } }Then invoke like this:
bravo-kernel commentedon Jul 2, 2022
That's awesome man 👍
Veretax commentedon Jul 2, 2022
Veretax commentedon Jul 5, 2022
Okay, finally figured out what was happening on my end.
When I had the paths setup, I think it was looking in the wrong folders all along. I had the relative paths there, but it thought they were in C:\ by referencing the source and test folders specifically down to c drive I started getting the feedback this way.
Thank you.
mahomedalid commentedon Oct 23, 2023
I had a very frustrating day trying to setup code coverage, verbosity and other stuff. I volunteer to update the code coverage page, the options mentioned there are already deprecated. You could assign this to me or not, but I will create a PR soon.
fflaten commentedon Oct 24, 2023
@mahomedalid That would be great 👏
mahomedalid commentedon Oct 26, 2023
@fflaten ignore the "Deprecated" section for now, I am thinking in something like this, early feedback? how much do we want to keep from the deprecated section? how much from the configuration do we want to mention, considering that if we duplicate the same information than in the config reference, we have now two places to maintain?
https://github.com/pester/docs/blob/d348c5ed308355dc4908fe8bab8f0b2bac428b67/docs/usage/code-coverage.mdx
fflaten commentedon Oct 26, 2023
Great start! I'm not able to view it rendered on mobile, so just some quick notes:
I personally like the structure it has, ex:
The last section about analyzed commands might be nice as a
<details>element or a last section "How to read the results", but we could add it later.The parts used in the examples at least. The hidden docs in
New-PesterConfigurationis just the code help which is really limited in length and formatting (bare minimum). This Code Coverage page is the "real docs" and marketing for the feature.mahomedalid commentedon Nov 2, 2023
@fflaten thanks for the comments, I will submit a PR now that I have more clear structure and we can continue discussing there.
mahomedalid commentedon Nov 2, 2023
@fflaten #286 was created
fflaten commentedon Nov 6, 2023
Closing as fixed by #286. 🥳
Will create new issues to track any further improvements/ideas. 🙂