Skip to content

Instantly share code, notes, and snippets.

@ByteDev
Last active December 14, 2020 01:14
Show Gist options
  • Save ByteDev/793e1de1a3b68145d7fd8fd0e6454de3 to your computer and use it in GitHub Desktop.
Save ByteDev/793e1de1a3b68145d7fd8fd0e6454de3 to your computer and use it in GitHub Desktop.
NUnit notes

NUnit Notes

Serial running

Force test class to run tests in serial.

[TestFixture]
[NonParallelizable]

Internals visible

Make production assembly internals visible through Info.cs file to unit test assemblies:

using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("MyProj.UnitTests")]

Asserts

// Assert contains
Assert.That(ex.InnerException.Message, Does.Contain("Issue cannot be empty" ).IgnoreCase);

// Assert property throws exception
Assert.Throws<Exception>(() => { var i = sut.ExistsButIsNotInt; });

// Assert that two IEnumable lists have equal contents
Assert.That(result, Is.EquivalentTo(headerList));

.NET Core proj that uses NUnit

Create new .NET Core project, then:

  • Delete any Program.cs
  • Remove any output type in .csproj
  • Add required references to nuget packages:
    • Microsoft.NET.Test.Sdk
    • NUnit
    • NUnit3TestAdapter

Bare bones test .csproj file example:

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

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.0" />
    <PackageReference Include="NUnit" Version="3.12.0" />
    <PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\..\src\MyProj\MyProj.csproj" />
  </ItemGroup>

</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment