Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a Try-Finally construct as a standard for Pester tests #84

Closed
lucdekens opened this issue Feb 3, 2019 · 2 comments
Closed

Use a Try-Finally construct as a standard for Pester tests #84

lucdekens opened this issue Feb 3, 2019 · 2 comments
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@lucdekens
Copy link
Contributor

A common best practice for Pester tests seems to be to encapsulate Pester tests in a Try-Finally construct.
Since a Finally block is always run, this allows for a cleanup of the environment, independent of the Try block outcome.

As a reference, have a look at the Pester Unit and Integration test templates that MSFT provides in their DSCResources repo.

The general layout:

function Invoke-TestSetup
{
    # Init code
}

function Invoke-TestCleanup
{
    # Cleanup code
}

Try
{
    Invoke-TestSetup
    # Tests
}
Finally
{
    Invoke-TestCleanup
}
@SimeonGerginov
Copy link
Contributor

Yes, this would help in our case because we have defined the BeforeAllTests and AfterAllTests functions and with this we can call BeforeAllTests in the beginning of the try block and AfterAllTests in the Finally block.

@SimeonGerginov SimeonGerginov self-assigned this Feb 4, 2019
@SimeonGerginov SimeonGerginov added the enhancement New feature or request label Feb 4, 2019
@SimeonGerginov
Copy link
Contributor

All Unit and Integration Tests have been refactored to use Try-Catch via PR #94. Closing the issue.

@SimeonGerginov SimeonGerginov added this to the 2.0-Consider milestone Apr 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants