Reasons to re-run these tests could be if you run the code under other circumstances like by example with PowerShell Core instead of Windows PowerShell or on Windows Server 2019 instead of Windows Server 2012 R2. By example if you’ve created an Operating System deployment or configured DSC, you can check if the configuration has been applied (correctly) or not.Īnother advantage of having Pester tests is that you can easily re-run tests without spending much time. Instead of using it to test your code, you can also use it to test if a configuration is the way it should be/you want it to be. This way you can verify if your code works as expected. Then the result of the test either passes or fails. So basically you use it to run scripts/functions/code with a specific input and tell Pester what the expected output should be. “Pester provides a framework for running Unit Tests to execute and validate PowerShell commands”. I have been playing around a bit already with parallelization and Github and the next thing on my list was Pester.įor those that do not know it, the Pester Wiki describes Pester as follows: These include, but are not limited to parallelization, GitHub, DSC, PowerShell Core, JEA, Plaster, DBAtools and DBAchecks. The test is done directly inside a Pester code block not using a function like I did previously.Ĭheck that my Windows Server Power Plan is set to High Performance.On my list I have a couple of PowerShell related subjects I want/need to learn more about. Your environment changes frequently and you need to be sure that your infrastructure is aligned with your standard. Pester is often used by sysadmin to do infrastructure testing. What is great is all the details (in red color) we can get when a test fails. I set the expected value that I should get and I compare it to the actual value returned by the function. Many more are available like -BeFalse, -BeGreaterOrEqual, -BeLike, -Contain, etc. If the assertion is not met the test fails and an exception is thrown up. The It block should have an expressive phrase describing the expected test outcome.įinally, the Should command defines the test condition to be met. The It block is the one that actually contains the test. They are optional and are useful to organize your test code. Context blocks are like Describe, they contain It blocks. You will often have one Describe block for each function you want to test. The main commands with Pester are Describe, It, Context, and Should.ĭescribe is a block that contains tests. You can ignore the first 3 rows in the Tests file, they came from the New-Fixture function and just dot sources the function to test into the PowerShell session. Pester is very declarative and easy to read. This is a very basic function that reverses the string characters. Let’s say I want to write a Pester test for the following PowerShell function. I will now show you a very basic Pester test. You need to use the following command to get the latest version: PS C:\> Install-Module -Name Pester -Force -SkipPublisherCheckĥ.1.0 Pester PSGallery Pester provides a framework for. If you have the 3.4 version installed and would like to update it you will face errors with Update-Module. The latest version is available in the PSGallery. Pester is shipped by default with Windows 10 and Windows Server 2016. In this blog post, I’ll do a short introduction to Pester with Installation and basic checks examples. Pester is used for example to test PowerShell Core and Pester itself. It’s a framework for writing and running unit tests, integration tests, and also infrastructure checks as we will see in a moment. If you never heard of it, Pester is a PowerShell module, written in PowerShell.
0 Comments
Leave a Reply. |