Closed IvanovOleg closed 7 years ago
Because that is how dot sourcing works. By saying . "$here\$sut" you instruct powershell to run the script. It runs almost the same way as if you just invoked it normally (for example by the & operator). The difference is that instead of creating a new scope, it runs in the current scope, leaving behind all the defined fumctions and variables. We use this feature to import the functions and variables from the tested script to the test script, but it also runs all the other statements in the script. That's why your script outputs the number 5.
There is no built in way to run just the functions of a script (at least none that I know of), so you either have to put the main executable code in "if ($underTest)" and provide the parameter when testing, or do a variation on the same thing but using a different condition, such as checking if the code is being dot-sourced.
My web is down, but here is a copy of an aricle that I wrote on the topic a while ago. http://web.archive.org/web/20160112151907/http://jakubjares.com/blog/2015/01/10/test-powershell-scripts-end-to-end-with-pester/
@nohwnd , thank you!
Hi, Source.ps1
Source.Tests.ps1
Output:
Why does pester show "5"? I don't want to execute $test = 5 and $test, I just want to test the clean function and thats all. If I disable . "$here\$sut" than it can't find a function. Is it possible to test such script partially?