Skip to content

Commit

Permalink
Scope Isolation and Direct Test Execution
Browse files Browse the repository at this point in the history
Test scripts are now executed in the caller's scope, instead of in Pester's module scope.  This means that the only Pester functions and variables available to the SUT will be the ones that Pester exports, and that tests won't fail if the file happens to use variables that match the names of internal Pester parameters ($test, etc.)
Note:  Some of pester's own test scripts are intended to be able to access its internal implementation details, and a mechanism for executing these tests has also been added.  This syntax can also bee used to execute test code in the scope of another module, if that is useful.

    InModuleScope Pester {
        Describe "Description" {
            # etc
        }
    }

Test script files can now be executed directly, if desired.  However, doing so gives up most of the functionality offered by Invoke-Pester.  When executing a test file this way, you no longer get a summary of tests passed / failed, can't output to NUnit or use -Passthru, etc.

Mocking code has been revised to be aware of the scope of the code being tested.  Mock -ModuleName has been changed slightly; it now allows you to mock CALLS to any command from inside a module.  This overlaps slightly with the InModuleScope command; the following two constructs have the same effect:

    Mock -ModuleName TestModule InternalFunction { 'I am the mock.' }

    InModuleScope TestModule {
        Mock InternalFunction { 'I am the mock.' }
    }

The difference is that when using InModuleScope, you can execute internal module functions directly.  This can be used to perform unit tests on a module's implementation details, instead of only being able to access its public API.

Module mocks can now be used to mock a call to any command from inside a module, instead of only mocking a module's own internal functions.  For example:

    Mock -ModuleName TestModule Start-Sleep { 'Sleep is for the weak.' }
  • Loading branch information
dlwyatt authored and nohwnd committed Jun 25, 2014
1 parent 361031f commit 90d5ecb
Show file tree
Hide file tree
Showing 27 changed files with 1,034 additions and 852 deletions.
29 changes: 13 additions & 16 deletions Functions/Assertions/Be.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
. "$here\Test-Assertion.ps1"
. "$here\Be.ps1"

Describe "PesterBe" {

It "returns true if the 2 arguments are equal" {
Test-PositiveAssertion (PesterBe 1 1)
}
It "returns true if the 2 arguments are equal and have different case" {
Test-PositiveAssertion (PesterBe "A" "a")
}

It "returns false if the 2 arguments are not equal" {
Test-NegativeAssertion (PesterBe 1 2)
InModuleScope Pester {
Describe "PesterBe" {

It "returns true if the 2 arguments are equal" {
Test-PositiveAssertion (PesterBe 1 1)
}
It "returns true if the 2 arguments are equal and have different case" {
Test-PositiveAssertion (PesterBe "A" "a")
}

It "returns false if the 2 arguments are not equal" {
Test-NegativeAssertion (PesterBe 1 2)
}
}
}

26 changes: 12 additions & 14 deletions Functions/Assertions/BeExactly.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".")
. "$here\$sut"

Describe "BeExactly" {
It "passes if letter case matches" {
'a' | Should BeExactly 'a'
InModuleScope Pester {
Describe "BeExactly" {
It "passes if letter case matches" {
'a' | Should BeExactly 'a'
}
It "fails if letter case doesn't match" {
'A' | Should Not BeExactly 'a'
}
It "passes for numbers" {
1 | Should BeExactly 1
2.15 | Should BeExactly 2.15
}
}
It "fails if letter case doesn't match" {
'A' | Should Not BeExactly 'a'
}
It "passes for numbers" {
1 | Should BeExactly 1
2.15 | Should BeExactly 2.15
}
}
25 changes: 11 additions & 14 deletions Functions/Assertions/BeNullOrEmpty.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
. "$here\Test-Assertion.ps1"
. "$here\BeNullOrEmpty.ps1"
InModuleScope Pester {
Describe "PesterBeNullOrEmpty" {

Describe "PesterBeNullOrEmpty" {
It "should return true if null" {
Test-PositiveAssertion (PesterBeNullOrEmpty $null)
}

It "should return true if null" {
Test-PositiveAssertion (PesterBeNullOrEmpty $null)
}

It "should return true if empty string" {
Test-PositiveAssertion (PesterBeNullOrEmpty "")
}
It "should return true if empty string" {
Test-PositiveAssertion (PesterBeNullOrEmpty "")
}

It "should return true if empty array" {
Test-PositiveAssertion (PesterBeNullOrEmpty @())
It "should return true if empty array" {
Test-PositiveAssertion (PesterBeNullOrEmpty @())
}
}
}

31 changes: 14 additions & 17 deletions Functions/Assertions/Contain.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
. "$here\Test-Assertion.ps1"
. "$here\Contain.ps1"
InModuleScope Pester {
Describe "PesterContain" {

Describe "PesterContain" {
Context "when testing file contents" {
Setup -File "test.txt" "this is line 1`nrush is awesome"
It "returns true if the file contains the specified content" {
Test-PositiveAssertion (PesterContain "$TestDrive\test.txt" "rush")
}
It "returns true if the file contains the specified content with different case" {
Test-PositiveAssertion (PesterContain "$TestDrive\test.txt" "RUSH")
}

Context "when testing file contents" {
Setup -File "test.txt" "this is line 1`nrush is awesome"
It "returns true if the file contains the specified content" {
Test-PositiveAssertion (PesterContain "$TestDrive\test.txt" "rush")
}
It "returns true if the file contains the specified content with different case" {
Test-PositiveAssertion (PesterContain "$TestDrive\test.txt" "RUSH")
}

It "returns false if the file does not contain the specified content" {
Test-NegativeAssertion (PesterContain "$TestDrive\test.txt" "slime")
}
It "returns false if the file does not contain the specified content" {
Test-NegativeAssertion (PesterContain "$TestDrive\test.txt" "slime")
}

}
}
}

27 changes: 12 additions & 15 deletions Functions/Assertions/ContainExactly.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
. "$here\Test-Assertion.ps1"
. "$here\ContainExactly.ps1"

Describe "PesterContainExactly" {

Context "when testing file contents" {
Setup -File "test.txt" "this is line 1`nPester is awesome"
It "returns true if the file contains the specified content exactly" {
Test-PositiveAssertion (PesterContainExactly "$TestDrive\test.txt" "Pester")
}

It "returns false if the file does not contain the specified content exactly" {
Test-NegativeAssertion (PesterContainExactly "$TestDrive\test.txt" "pESTER")
InModuleScope Pester {
Describe "PesterContainExactly" {

Context "when testing file contents" {
Setup -File "test.txt" "this is line 1`nPester is awesome"
It "returns true if the file contains the specified content exactly" {
Test-PositiveAssertion (PesterContainExactly "$TestDrive\test.txt" "Pester")
}

It "returns false if the file does not contain the specified content exactly" {
Test-NegativeAssertion (PesterContainExactly "$TestDrive\test.txt" "pESTER")
}
}
}
}

19 changes: 8 additions & 11 deletions Functions/Assertions/Exist.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
. "$here\Test-Assertion.ps1"
. "$here\Exist.ps1"
InModuleScope Pester {
Describe "PesterExist" {

Describe "PesterExist" {
It "returns true for paths that exist" {
Test-PositiveAssertion (PesterExist $TestDrive)
}

It "returns true for paths that exist" {
Test-PositiveAssertion (PesterExist $TestDrive)
}

It "returns false for paths do not exist" {
Test-NegativeAssertion (PesterExist "$TestDrive\nonexistant")
It "returns false for paths do not exist" {
Test-NegativeAssertion (PesterExist "$TestDrive\nonexistant")
}
}
}

33 changes: 15 additions & 18 deletions Functions/Assertions/Match.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
. "$here\Should.ps1"
. "$here\Match.ps1"
InModuleScope Pester {
Describe "Match" {

Describe "Match" {
It "returns true for things that match" {
PesterMatch "foobar" "ob" | Should Be $true
}

It "returns true for things that match" {
PesterMatch "foobar" "ob" | Should Be $true
}

It "returns false for things that do not match" {
PesterMatch "foobar" "slime" | Should Be $false
}
It "returns false for things that do not match" {
PesterMatch "foobar" "slime" | Should Be $false
}

It "passes for strings with different case" {
PesterMatch "foobar" "FOOBAR" | Should Be $true
}
It "passes for strings with different case" {
PesterMatch "foobar" "FOOBAR" | Should Be $true
}

It "uses regular expressions" {
PesterMatch "foobar" "\S{6}" | Should Be $true
}
It "uses regular expressions" {
PesterMatch "foobar" "\S{6}" | Should Be $true
}



}
}

27 changes: 12 additions & 15 deletions Functions/Assertions/MatchExactly.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
. "$here\Should.ps1"
. "$here\MatchExactly.ps1"
InModuleScope Pester {
Describe "MatchExactly" {

Describe "MatchExactly" {
It "returns true for things that match exactly" {
PesterMatchExactly "foobar" "ob" | Should Be $true
}

It "returns true for things that match exactly" {
PesterMatchExactly "foobar" "ob" | Should Be $true
}

It "returns false for things that do not match exactly" {
PesterMatchExactly "foobar" "FOOBAR" | Should Be $false
}
It "returns false for things that do not match exactly" {
PesterMatchExactly "foobar" "FOOBAR" | Should Be $false
}

It "uses regular expressions" {
PesterMatchExactly "foobar" "\S{6}" | Should Be $true
}
It "uses regular expressions" {
PesterMatchExactly "foobar" "\S{6}" | Should Be $true
}


}
}

Loading

0 comments on commit 90d5ecb

Please sign in to comment.