-
Notifications
You must be signed in to change notification settings - Fork 77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Code Coverage page is outdated #196
Comments
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? |
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 |
That's awesome man 👍 |
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. Thank you. |
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. |
@mahomedalid That would be great 👏 |
@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:
I personally like the structure it has, ex:
The last section about analyzed commands might be nice as a
The parts used in the examples at least. The hidden docs in |
@fflaten thanks for the comments, I will submit a PR now that I have more clear structure and we can continue discussing there. |
Closing as fixed by #286. 🥳 |
Checklist
Link to page
https://pester.dev/docs/usage/code-coverage
What is the issue?
Docs for using code coverage is not updated for Pester 5 yet.
Examples:
-CodeCoverage
Suggested fix?
Should be updated to cover Pester 5.3 status with new configuration, breakpoint vs profiler etc.
The text was updated successfully, but these errors were encountered: