Add code coverage report generation instructions#6515
Add code coverage report generation instructions#6515adityapatwardhan merged 10 commits intoPowerShell:masterfrom
Conversation
|
@rjmholt FYI - the assignees should only be maintainers. You can add people to reviewers. |
|
@SteveL-MSFT Understood |
| @@ -0,0 +1,127 @@ | |||
| Getting Code Coverage Analysis for PowerShell | |||
| === | |||
There was a problem hiding this comment.
I believe we've standardized on using hashes for headers
There was a problem hiding this comment.
I'll change it over, although my opinion is that the hashes are less readable in the raw view.
|
|
||
| **Note: Code coverage is currently only supported on Windows, since we use OpenCover** | ||
|
|
||
| The PowerShell code base is configured to build with code coverage support using [OpenCover](https://github.com/OpenCover/opencover). You can see the testing coverage of the current [`master`](https://github.com/PowerShell/PowerShell) branch build at any time at [codecov.io](https://codecov.io/gh/PowerShell/PowerShell). |
There was a problem hiding this comment.
Put the second sentence on a separate line (semantic linebreak)
| The PowerShell code base is configured to build with code coverage support using [OpenCover](https://github.com/OpenCover/opencover). You can see the testing coverage of the current [`master`](https://github.com/PowerShell/PowerShell) branch build at any time at [codecov.io](https://codecov.io/gh/PowerShell/PowerShell). | ||
|
|
||
| To run test coverage analysis of PowerShell on your own branch/machine | ||
| however, you will need to take the following steps (and be aware that running |
There was a problem hiding this comment.
"however," seems out of place and unnecessary
| Running tests with code coverage analysis | ||
| --- | ||
|
|
||
| **First**: Open PowerShell in an **Administrator** session. OpenCover |
There was a problem hiding this comment.
replace Administrator with elevated
| PS> Import-Module $PWD\test\tools\OpenCover | ||
|
|
||
| # Install OpenCover to a temporary directory | ||
| PS> Install-OpenCover -TargetDirectory $env:TEMP -Force |
There was a problem hiding this comment.
I thought we updated this cmdlet so it has a default TargetDirectory so it doesn't need to be specified?
There was a problem hiding this comment.
The original blog post gives an argument (except with no parameter, and Install-OpenCover's first positional argument is actually the URL from which to download OpenCover). I'm happy with using a default, but in that case I think we should write some documentation of build function parameters so that Get-Help has some description of what the parameters do.
There was a problem hiding this comment.
Looking at the file, the current default TargetDirectory is $HOME
There was a problem hiding this comment.
If the defaults work, I would recommend not adding additional parameters as it makes it more confusing to new users
| # If you want to run only the continuous integration tests, | ||
| # add -CIOnly, which will take less time | ||
| PS> Invoke-OpenCover ` | ||
| >> -OutputLog coverage.xml ` |
There was a problem hiding this comment.
I believe we also have reasonable defaults for these
There was a problem hiding this comment.
If we use the default OpenCoverPath in Install-OpenCover, we should do that here too.
I assume the function is designed to be run in a specific place, and in that case has sane defaults for TestPath and PowerShellExeDirectory (in which case we can take out the line defining $psDir).
But in the case of OutputLog I think we should keep that so anyone following along knows where the output will be.
In general, I'm sort of in favour of using explicit parameters in the documentation, only because there's more information to work with if something goes wrong. It's why PowerShell has named parameters after all.
There was a problem hiding this comment.
I say all this after having to read the scripts to work out what the parameters do
There was a problem hiding this comment.
The defaults here are:
OutputLog:"$home/Documents/OpenCover.xml"TestPath:"${script:psRepoPath}/test/powershell"OpenCoverPath:"$home/OpenCover"PowerShellExePath:"${script:psRepoPath}/src/powershell-win-core/bin/CodeCoverage/netcoreapp2.0/win7-x64/publish"
So I feel like TestPath and PowerShellExePath are pretty sane and we can omit them happily.
For OpenCoverPath we should only use the default path if Install-OpenCover does (I'm not a huge fan of installing test tools in $HOME, especially if the user has no knowledge of that, but we could change the default).
And OpenCoverPath I think is definitely better being explicit. Nobody will ever find OpenCover.xml in their $HOME directory.
| ```powershell | ||
| # Collect the coverage data using Get-CodeCoverage from the OpenCover | ||
| # module that was imported above | ||
| PS> $coverageData = Get-CodeCoverage .\coverage.xml |
There was a problem hiding this comment.
May be worth mentioning that this is an expensive operation so recommendation is to store it in a variable for reuse/re-viewing
|
|
||
| **Note: Code coverage is currently only supported on Windows, since we use OpenCover.** | ||
|
|
||
| The PowerShell code base is configured to build with code coverage support using [OpenCover](https://github.com/OpenCover/opencover). You can see the testing coverage of the current [`master`](https://github.com/PowerShell/PowerShell) |
There was a problem hiding this comment.
Would you be so kind as to switch to semantic linefeeds and use reference links throughout the document?
Example:
The PowerShell code base is configured to build with code coverage support using [OpenCover].
You can see the testing coverage of the current [`master`] branch build at any time at [codecov.io].
[OpenCover]: https://github.com/OpenCover/opencover
[`master`]: https://github.com/PowerShell/PowerShell
[codecov.io]: https://codecov.io/gh/PowerShell/PowerShellBecomes:
The PowerShell code base is configured to build with code coverage support using OpenCover. You can see the testing coverage of the current master branch build at any time at codecov.io.
Makes it easier to track changes.
There was a problem hiding this comment.
Ah that's a nice feature! Will do.
|
|
||
| ## Running tests with code coverage analysis | ||
|
|
||
| **First**: Open PowerShell in an **elevated** session. OpenCover needs elevated privileges to work. |
There was a problem hiding this comment.
Second sentence should be on newline. The renderer will put it back.
| PS> Import-Module $PWD\test\tools\OpenCover | ||
|
|
||
| # Install OpenCover to a temporary directory | ||
| PS> Install-OpenCover -TargetDirectory $env:TEMP -Force |
There was a problem hiding this comment.
If the defaults work, I would recommend not adding additional parameters as it makes it more confusing to new users
|
I've removed a couple of the parameters given. The rest are the coverage file itself and the OpenCover installation directory. In the case of the code coverage XML, there's no indication of where it is unless the user explicitly states it, so I think better to document it. In the case of the OpenCover installation directory, the default is |
|
@rjmholt separate from this PR, perhaps you can submit a new PR to have OpenCover installed to |
|
|
||
| # Build PowerShell. You may need to add other flags here like | ||
| # -ResGen, -Restore or -PSModuleRestore | ||
| PS> Start-PSBuild -Configuration CodeCoverage |
There was a problem hiding this comment.
I would add -PSModuleRestore in the example instead of comment, since it will be required for some tests. Maybe even -Clean.
| Microsoft.WSMan.Runtime 80.95 80.33 | ||
| Microsoft.PowerShell.Commands.Diagnostics 0 0 | ||
| ``` | ||
|
|
There was a problem hiding this comment.
Compare-CodeCoverage and Compare-FileCoverage might be worth mentioning for comparing two runs.
|
|
||
| # Finally, open the report in your browser | ||
| Invoke-Item C:\temp\Coverage\index.htm | ||
| ``` |
There was a problem hiding this comment.
Also mention Format-FileCoverage for a console visualizer as an alternative.
There was a problem hiding this comment.
I tried using Format-FileCoverage, but I'm not sure it does what the help message states. I read through the implementation and it currently only seems to compare two codebases (as in, I couldn't get it to just show me one codebase's test coverage, because the oldBase and newBase parameters not being provided cause the command to fail). I'll do some work on updating that and the other stuff that @SteveL-MSFT mentioned in another PR, and then update the documentation with it.
|
@adityapatwardhan Happy to add the extra documentation on the other coverage functions. My documentation is based on this blog post. Is there any documentation I can work off with respect to the other commands you mentioned?
|
|
I'll leave those for another PR unless it's preferred I fix them here. |
|
@rjmholt Just fix the errors introduced in this change. |
PR Summary
Adds a document to the repo that details how to obtain code coverage analysis for PowerShell using OpenCover.
Would be happy with any feedback on:
We should also update the code coverage blog posts to possibly point at this document.
PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headerWIP:to the beginning of the title and remove the prefix when the PR is ready.[feature]if the change is significant or affects feature tests