-
Notifications
You must be signed in to change notification settings - Fork 54
Update Group and GroupSet tests to meet Pester 4 standards #135
Conversation
Codecov Report
@@ Coverage Diff @@
## dev #135 +/- ##
===================================
Coverage 83% 83%
===================================
Files 19 19
Lines 2760 2760
Branches 4 4
===================================
Hits 2305 2305
Misses 451 451
Partials 4 4 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 7 of 8 files at r1.
Reviewable status: 7 of 8 files reviewed, 2 unresolved discussions (waiting on @mhendric and @PlagueHO)
a discussion (no related file):
Non blocker for this one, but it looks like a lot of these functions need comment based help. Do you think that's worth tracking in a new issue?
Tests/Unit/MSFT_GroupResource.Tests.ps1, line 103 at r1 (raw file):
Describe 'Get-TargetResource' {
I don't think it's necessary for Pester to function, but I noticed you removed all Context blocks (and replaced them with Describe blocks). So now they all just go straight from Describe to It. Is that an acceptable Pester pattern?
Also, if we're trying to follow the test template, should we change this to something like?:
Describe 'MSFT_GroupResource\Get-TargetResource' -Tag 'Get'
Throughout.
I'm having a bit of a think about this one - the problem I'm seeing is that the unit tests for GroupResource don't match the pattern of all the other resources in this module. Normally we use the Describe block to define a fixture for the function under test - but for this resource the Context block was used. I can see why though: There needed to be an outer Describe block within the InModuleScope to contain the BeforeAll/AfterAll blocks that must run before each function test. However, I think the code that runs in the BeforeAll/AfterAll can just be place in-line and therefore enables us to adopt a better pattern (like MsiPackage):
However, some resources have adopted a different pattern (see MsiPackage) - which I think is better. An outer Describe block is used (rather than the older try/catch/finally) pattern. Inner describe blocks are then used to define the function:
But the big problem I see is there just weren't any Context blocks defined, with the It blocks containing all the assertions in one big "test". Either way, I'm going to try and rework to create proper contexts in the tests. |
It is painful doing all the refactoring on this one, but it has highlighted some incorrect tests and some omissions. So it'll be worthwhile I think. I'm aiming to have this completed tonight, but I've got to spin up a Nano Server to complete testing. |
Cool, thanks @PlagueHO . Let me know once you're ready for another review. |
Thanks @mhendric - should be later on today. And just for info, here is the command I use to create the Nano VHDX to run tests on: I'd like to add a helper script that could be used to create a Nano Hyper-V VM, inject the module code and execute tests. Otherwise it is a lot of manual work getting the Nano VM stood up ready for testing. Will raise a separate issue. |
Nearly there - I found a few bugs in the Nano tests that must have been there a while. Not sure when the tests were last run on Nano. |
@mhendric - It is good to go now. I've finished refactoring all the GroupResource unit tests and fixed the Nano ones so that they all work: There are additional refactorings that could be made (based on what you did over in xPSDesiredStateConfguration), but we can address that at a later date now that these are closer to Pester standards. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: 6 of 9 files reviewed, 2 unresolved discussions (waiting on @mhendric)
a discussion (no related file):
Previously, mhendric (Mike Hendrickson) wrote…
Non blocker for this one, but it looks like a lot of these functions need comment based help. Do you think that's worth tracking in a new issue?
Yes - definitely.
However, I think we should possibly examine the rule around comment based help on every function. Sometimes it is actually not helpful. E.g. if the function is very small, obeys single responsibility principle, has a very clear expressive function name, is private/internal - then perhaps the comment based help works against us (e.g. the help may actually not be true - only code is guaranteed to be true). E.g. We should try and adopt principles from "Clean Code" - make our code the source of truth, not the comments. But that is a discussion for another day.
Tests/Unit/MSFT_GroupResource.Tests.ps1, line 103 at r1 (raw file):
Previously, mhendric (Mike Hendrickson) wrote…
Describe 'Get-TargetResource' {
I don't think it's necessary for Pester to function, but I noticed you removed all Context blocks (and replaced them with Describe blocks). So now they all just go straight from Describe to It. Is that an acceptable Pester pattern?
Also, if we're trying to follow the test template, should we change this to something like?:
Describe 'MSFT_GroupResource\Get-TargetResource' -Tag 'Get'
Throughout.
Done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 2 of 3 files at r3.
Reviewable status: 8 of 9 files reviewed, 1 unresolved discussion (waiting on @PlagueHO)
Tests/Unit/MSFT_GroupResource.Tests.ps1, line 237 at r4 (raw file):
Describe 'GroupResource\Assert-GroupNameValid' {
Should this (and all other helper functions) use -Tag 'Helper' ?
Looking good @PlagueHO . Just one more comment on my part. Up to you if you want to integrate or not. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks again @mhendric! Have made suggested changes.
Reviewable status: 8 of 9 files reviewed, 1 unresolved discussion (waiting on @mhendric)
Tests/Unit/MSFT_GroupResource.Tests.ps1, line 237 at r4 (raw file):
Previously, mhendric (Mike Hendrickson) wrote…
Describe 'GroupResource\Assert-GroupNameValid' {
Should this (and all other helper functions) use -Tag 'Helper' ?
Good idea. Done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 1 of 1 files at r5.
Reviewable status: complete! all files reviewed, all discussions resolved
Pull Request (PR) description
This PR refactors Group and GroupSet tests to meet Pester 4 standards. Also made some tweaks to the tests to use Pester It block
-TestCases
for some tests.I also tweaked the README.MD style to remove : after resource titles as this is the standard (I had been doing it wrong myself 😁).
This Pull Request (PR) fixes the following issues
Task list
Entry should say what was changed, and how that affects users (if applicable).
and comment-based help.
This change is