- Name your files .tests.ps1
- Keep tests simple
- Test only what you need
- Reduce dependencies
- Be sure to tag your
Describeblocks based on their purpose- Tag
CIindicates that it will be run as part of the continuous integration process. These should be unit test like, and generally take less than a second. - Tag
Featureindicates a higher level feature test (we will run these on a regular basis), for example, tests which go to remote resources, or test broader functionality - Tag
Scenarioindicates tests of integration with other features (these will be run on a less regular basis and test even broader functionality than feature tests.
- Tag
- Make sure that
Describe/Context/Itdescriptions are useful- The error message should not be the place where you describe the test
- Use
Contextto group tests- Multiple
Contextblocks can help you group your test suite into logical sections
- Multiple
- Use
BeforeAll/AfterAll/BeforeEach/AfterEachinstead of custom initiators - Prefer Try-Catch for expected errors and check $_.fullyQualifiedErrorId (don't use
should throw) - Use
-testcaseswhen iterating over multipleItblocks - Use code coverage functionality where appropriate
- Use
Mockfunctionality when you don't have your entire environment - Avoid free code in a
Describeblock- Use
[Before|After][Each|All]see Free Code in a Describe block
- Use
- Avoid creating or using test files outside of TESTDRIVE:
- TESTDRIVE: has automatic clean-up
- Keep in mind that we are creating cross platform tests
- Avoid using the registry
- Avoid using COM
- Avoid being too specific about the count of a resource as these can change platform to platform
- ex: checking for the count of loaded format files, check rather for format data for a specific type
- Don't have too many evaluations in a single It block
- The first
Shouldfailure will stop that block
- The first
- Don't use
Shouldoutside of anItBlock - Don't use the word "Error" or "Fail" to test a positive case
- ex: "Get-Childitem TESTDRIVE: shouldn't fail", rather "Get-ChildItem should be able to retrieve file listing from TESTDRIVE"