Assertions
As in any other codebase, it is good practice to assert assumptions before continuing with more heavy procedures in your build automation. When an assertion is violated, it usually entails that the build should fail immediately.
In the most simple form, you can fail a build by calling:
Assert.Fail("This was unexpected!");
Furthermore, you can use one of the following more specific assertion methods:
- Nullness
- Conditions
- Collections
- Files & Directories
// Assert not-null fluently
obj.NotNull().ToString();
// Assert not-null explicitly
Assert.NotNull(obj);
// Assert true condition
Assert.True(response.IsSuccessStatusCode);
// Assert false condition
Assert.False(repository.IsOnMainBranch());
// Assert collection is not empty or empty
Assert.NotEmpty(releaseNotes);
Assert.Empty(errors);
// Assert collection count
Assert.Count(packages, length: 5);
Assert.HasSingleItem(matchingEntries);
// Assert file exists
Assert.FileExists(file);
// Assert directory exists
Assert.DirectoryExists(directory);
info
Each of the above methods uses the CallerArgumentExpressionAttribute
to capture usage details from the call-site. If you want to provide a more comprehensive explanation, you can pass the message
parameter instead.