Skip to main content

Unit Testing Conventions

Suppress Async Warning Project-Wide

Since we have several calls to base classes for diagnostics and such that are async, it's a good idea to standardize on all Test methods be async.

The problem is that not every test method contains an awaited call. so, we get compiler warnings for them.

To suppress those, add this to the PropertyGroup of the csproj of your Test projects:

    <!-- NoWarn below suppresses CS1998 project-wide -->
    <!-- This suppresses the IDE warning that the async method lack await. -->
    <!-- We default all test methods to async, so the timing and dependency calls are the consistent. -->
    <NoWarn>$(NoWarn);CS1998</NoWarn>

The csproj would look like this:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>

    <IsPackable>false</IsPackable>
    <IsTestProject>true</IsTestProject>

    <!-- NoWarn below suppresses CS1998 project-wide -->
    <!-- This suppresses the IDE warning that the async method lack await. -->
    <!-- We default all test methods to async, so the timing and dependency calls are the consistent. -->
    <NoWarn>$(NoWarn);CS1998</NoWarn>
  </PropertyGroup>

  ...
</Project>