dsccommunity / FailoverClusterDsc

This module contains DSC resources for deployment and configuration of Windows Server Failover Cluster.
MIT License
60 stars 54 forks source link

How do I run Pester Unit tests locally before performing a Pull Request? #262

Closed dennisl68-castra closed 2 years ago

dennisl68-castra commented 3 years ago

I've cloned this project as I'd like to help out with a current Pull Request (#258).
However, when I try to run the Unit tests after a build in VS Code using the Extension Pester Test Explorer, I get a lot of errors as shown below (the build fails to run the tests as well):

Starting discovery in 7 files.
[-] Discovery in D:\UsbRepos\One\xFailOverCluster.GitHub\tests\Unit\MSFT_xCluster.Tests.ps1 failed with: System.IO.FileNotFoundException: The specified module 'xFailOverCluster' was not loaded because no valid module file was found in any module directory.
...
Discovery found 0 tests in 643ms.
...

dennisl68-castra commented 3 years ago

Ok. The tests couldn't be started due to another module dependency wasn't handled by the build script. Adding this module by hand made the tests runnable when invoked by .\build.ps1

Save-Module DscResource.Common -Path <mypath>\xFailOverCluster.GitHub\output\RequiredModules

But Pest Tester Explorer still fails... And the tests invoked by .\build.ps1 almost all fails...

Build FAILED. 14 tasks, 1 errors, 0 warnings 00:04:48.1276900
Assert-Build : Assertion failed. Failed 578 tests. Aborting Build
dennisl68-castra commented 3 years ago

I noticed that Pester 4 should be used. Now the tests is good :)

FailOverCluster_v1.17.0.Windows.PSv.5.1.17763.2090.xml
Code Coverage SUCCESS with value of 99,38 % (threshold 85 %)
Done /./test/Pester_if_Code_Coverage_Under_Threshold 00:00:00.1509999
Done /./test 00:05:04.1568971
Done /. 00:05:05.2619738
Build succeeded. 15 tasks, 0 errors, 0 warnings 00:05:05.5719682
dennisl68-castra commented 2 years ago

The test can only be run if .\build.ps1 -Tasks build is first run. If the PowerShell session is closed and a new one opened, Invoke-Pester run by itself will fail to start the tests...

Executing script D:\UsbRepos\One\xFailOverCluster.GitHub\tests\Unit\MSFT_xCluster.Tests.ps1
  [-] Error occurred in test script 'D:\UsbRepos\One\xFailOverCluster.GitHub\tests\Unit\MSFT_xCluster.Tests.ps1' 0ms
    FileNotFoundException: The specified module 'xFailOverCluster' was not loaded because no valid module file was found  
    in any module directory.
...
gaelcolas commented 2 years ago

Yes, that's intentional. Running build.ps1 will run the build, the tests, and possibly the hqrmtest if build yaml is correctly configured.

dennisl68-castra commented 2 years ago

Is that to ensure that the solution is built fresh before any new tests are run? But you are able to rerun the tests stand alone after the first build is done within the PoSH-session (just curious abut that design decision).

gaelcolas commented 2 years ago

Yeah, running build.ps1 changes your PSModulePath so that the built module is found. Fresh sessions means output/ is not in your PSModulePath, and we can't be sure the source has been built. You can call other tasks such as: build.ps1 -tasks test (only run tests) build.ps1 -tasks noop (only sets PSModulePath) You can also call test with parameter to only run one test file (-PesterScript your/pesterscript.tests.ps1) iirc As long as you understand that if you change the source you need to build it to test your latest version, how you call pester doesn't matter. Providing build.ps1 -tasks test means it's easier to run the same way on a CI agent than on your workstation.

dennisl68-castra commented 2 years ago

Thx for a really good answer :)