r/PowerShell • u/nkasco • Dec 10 '25
Question How Often Do You Write Pester tests?
Topic, genuinely curious.
20
Upvotes
r/PowerShell • u/nkasco • Dec 10 '25
Topic, genuinely curious.
View all comments
Show parent comment
5
u/PanosGreg Dec 10 '25 edited Dec 10 '25
To expand on the above, few examples to give you the context when I wrote some tests:
Our DB team needed to Patch their SQL Server clusters, so I wrote a Pester test to make sure a cluster is ready to get patched without any data loss. That means for example check if the Availability Group is Healthy, that the Cluster service is running, that the DBs are not suspended, that there is enough available space, that the DB nodes can communicate with each other, that the AD account I'm using has adequate permissions, etc... If all those tests pass, then the cluster is ready to get patched, otherwise I may lose data during failover of the nodes, which means the DB team needs to come in and fix them so that the nodes can be patched.
I wrote a module which exposes a few functions, so I wrote a few Pester tests to check the functions if they work as expected. Let's say it's about a module that integrates with New Relic (an observability SaaS platform) The first function goes to New Relic and fetches some data (let's say it gets all the user accounts from NR), so I check both the Green and the Red paths. For the green path, if I get the expected data type back, if the filtering works on my functions, etc. For the red path, when I use an invalid API key if I get the expected error back and the same if the API key does not have the required permissions, when I miss a parameter or I provide an invalid input parameter, then again I check that I get the expected error back, etc.
The security team asked us that our S3 buckets in AWS must be compliant. So I wrote a Pester test where I go through each bucket and check if it has public access enabled, if the S3 versioning is enabled, if the name of the bucket follows a specific format, if the bucket is encrypted, if it has the expected tags, etc.
So these were some examples of integration testing (or acceptance tests also called), where I need the real system in order to run tests against it, as opposed to unit testing where you check if a function works in a specific way internally, which I do not do. And so I just wanted to point that out, because I believe it's important when talking about testing.
Admittedly two of the three examples above are essentially Infrastructure Validation (as it's usually called), through Pester (no matter if the infra is on-prem like SQL Server or in the cloud like S3 bucket).
Lately I'm thinking I need to double-down on Pester tests, and put that as a standard practice especially when the team writes functions. Because we need to make sure if a function works in let's say PowerShell 7.4 but also in the current one 7.5 or the next one 7.6. Another one is if the function works with the current dependent DLL libraries (ex yaml library v.X or an AWS .dll library) but if it will also work on the next version. So essentially integration testing in regards to compatibility to make sure our code continues to work with the latest versions, which is very important for dependent (external) modules or dependent (again external) .dll libraries (from nuget usually). (mainly because the external functions may change input params or change output types, or because the types from a library may change properties or change methods or the way the methods get invoked, as-in the method overloads, or finally because a new PS version may change some native functions)
Apologies for the wall of text, but as you can tell there's a lot of background in testing.
Hope you got some value out of this post.