The Deterministic guideline
A unit test should have a deterministic behavior and should lead to the same result. This should be the case regardless of the following:
- Time: This includes changes in time zone and testing at different times.
- Environment: Such as the local machine or CI/CD server.
Let’s discuss some cases where we risk making non-deterministic unit tests.
Non-deterministic cases
There are cases that can lead to non-deterministic unit tests. Here are a few of them:
- Having interdependent unit tests, such as a test that writes to a static field.
- Loading a file with an absolute path as the file location on the development machine will not match that on the automation machine.
- Accessing a resource that requires higher privileges. This can work, for example, when running VS as an admin but may fail when running from a CI pipeline.
- Using randomization methods without treating them as dependencies.
- Depending on the system...