Skip to content

Filtering Tests with Invoke Tests

Jon Wagner edited this page Feb 18, 2013 · 1 revision

Filtering Tests with Invoke-Tests

By default, Invoke-Tests will run all of the tests in a test file. You may want to only run some of the tests in a given file, or you may want to run a particular type of test across multiple files. The -Filter parameter gives you some options for this.

Let's assume you have a test hierarchy like this:

Describing "fruit" {
	Given "colors" {
		It "is red" {
			Color-Of apple | Should Be red
		}

		It "is orange" {
			Color-Of orange | Should Be orange
		}
	}

	Given "shape" {
		It "is round" {
			Shape-Of orange | Should Be round
		}
	}
}

Describing "vehicles" {
	Given "colors" {
		It "is red" {
			Color-Of Ferrari | Should Be red
		}
	}

	Given "wheels" {
		It "has four" {
			Count-Wheels Car | Should Be 4
		}

		It "has four" {
			Count-Wheels Motorcycle | Should Be LT 4 And | Should Be GT 1
		}

		It "is round" {
			Shape-Of Wheel | Should Be round
		}
	}
}

The -Filter parameter is an array of match strings that are applied against the name of the tests. The first string is matched against the level-one test cases, the second string is matched against the level-two test cases, etc.

Some examples may help:

  • Invoke-Tests -Filter "fruit"
    • Runs all of the fruit tests
  • Invoke-Tests -Filter "vehicle"
    • Runs all of the "vehicles" tests. Note that it uses -match to filter, so we don't need the "s" on vehicles.
  • Invoke-Tests -Filter "","colors"
    • Runs all of the colors tests for both fruit and vehicles.
  • Invoke-Tests -Filter "fruit","colors"
    • Runs all of the colors tests for just fruit.
  • Invoke-Tests -Filter "","","round"
    • Runs the fruit shape test and the wheel round test.