1/1/2024 0 Comments Pester testingThis means that some functions might be private. The Export-ModuleMember command and the FunctionsToExport key used in the manifest file can limit the functions that are exported. Now Pester can read the functions I defined in the module, and I can build the tests accordingly.īut, as you might know, every function declared in the module isn't necessarily available by default. Import-Module -Name "$ThisModule.psm1" -Force -ErrorAction Stop If there's a problem importing the module, I want to throw a terminating error immediately and not proceed with the tests. Also, notice that I'm using an ErrorAction of Stop here. Once I know that I’ve removed all versions from the session, I'll run Import-Module and use the -Force switch just in case. Get-Module -Name $ThisModuleName -All | Remove-Module -Force -ErrorAction Ignore Once I have the name of the module I'm testing, I can remove it by using Get-Module –Name $ThisModule –All and then piping the results to Remove-Module, because if you have more than one version installed, not using –All will leave lingering versions, and Pester will complain. $ThisModuleName = $ThisModule | Split-Path -Leaf $ThisModule = $MyInvocation.M圜ommand.Path -replace '\.Tests\.ps1$' Below, I'm using the $ MyInvocation automatic variable to figure out the name of the test script inside the script itself and inferring the module name. Because I have a standard naming convention, I can figure out what the PSM1 file path is from within the test script itself. So, for example, if I have a module called SQLServer, my test file will be 1, and I will include it in the same folder as the module itself. I name all my Pester tests files after the module name and end the file name with.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |