From 5b36160488946f7c7871d4d56422eef765f9299a Mon Sep 17 00:00:00 2001 From: Vladimir Pecanac Date: Tue, 1 Dec 2026 00:25:21 +0100 Subject: [PATCH] Update TestingExceptions to .NET 11, MSTest 4, NUnit 5 and xUnit 2.9.3 Retarget all projects to net11.0. MSTest tests move to Assert.ThrowsExactly and ThrowsExactlyAsync, the ExpectedException test is removed. NUnit tests use Action and Func instead of TestDelegate and AsyncTestDelegate and await ThrowsAsync. FluentAssertions is removed from the xUnit project. Each framework gets a test that accepts a base exception type. --- .../HeroTests.cs | 20 ++++++----- .../TestingExceptions.MSTestTests.csproj | 10 +++--- .../TestingExceptions.NUnitTests/HeroTests.cs | 36 +++++++++++-------- .../TestingExceptions.NUnitTests.csproj | 12 +++---- .../TestingExceptions.xUnitTests/HeroTests.cs | 35 ++++++++---------- .../TestingExceptions.xUnitTests.csproj | 11 +++--- .../TestingExceptions.csproj | 2 +- 7 files changed, 64 insertions(+), 62 deletions(-) diff --git a/dotnet-testing/TestingExceptions/TestingExceptions.MSTestTests/HeroTests.cs b/dotnet-testing/TestingExceptions/TestingExceptions.MSTestTests/HeroTests.cs index 86fd8967d8..a5c304ed3f 100644 --- a/dotnet-testing/TestingExceptions/TestingExceptions.MSTestTests/HeroTests.cs +++ b/dotnet-testing/TestingExceptions/TestingExceptions.MSTestTests/HeroTests.cs @@ -27,19 +27,21 @@ public void GivenInsufficientExperience_WhenLevelUpIsInvoked_ThenExceptionIsThro Action act = () => hero.LevelUp(); // Assert - Assert.ThrowsException(act); - Assert.ThrowsException(() => hero.LevelUp()); + Assert.ThrowsExactly(act); + Assert.ThrowsExactly(() => hero.LevelUp()); } [TestMethod] - [ExpectedException(typeof(ArgumentOutOfRangeException))] - public void GivenInsufficientExperience_WhenLevelUpIsInvoked_ThenExceptionIsThrownWithoutAssert() + public void GivenInsufficientExperience_WhenLevelUpIsInvoked_ThenBaseExceptionTypeIsAccepted() { // Arrange var hero = new Hero(500); // Act - hero.LevelUp(); + Action act = () => hero.LevelUp(); + + // Assert + Assert.Throws(act); } [TestMethod] @@ -52,8 +54,8 @@ public async Task GivenInsufficientExperience_WhenLevelUpAsyncIsInvoked_ThenExce Func act = hero.LevelUpAsync; // Assert - await Assert.ThrowsExceptionAsync(act); - await Assert.ThrowsExceptionAsync(() => hero.LevelUpAsync()); + await Assert.ThrowsExactlyAsync(act); + await Assert.ThrowsExactlyAsync(() => hero.LevelUpAsync()); } [TestMethod] @@ -63,7 +65,7 @@ public void GivenInsufficientExperience_WhenLevelUpIsInvoked_ThenExceptionIsThro var hero = new Hero(500); // Act - var exception = Assert.ThrowsException(() => hero.LevelUp()); + var exception = Assert.ThrowsExactly(() => hero.LevelUp()); // Assert Assert.IsNotNull(exception); @@ -77,7 +79,7 @@ public async Task GivenInsufficientExperience_WhenLevelUpAsyncIsInvoked_ThenExce var hero = new Hero(500); // Act - var exception = await Assert.ThrowsExceptionAsync(() => hero.LevelUpAsync()); + var exception = await Assert.ThrowsExactlyAsync(() => hero.LevelUpAsync()); // Assert Assert.IsNotNull(exception); diff --git a/dotnet-testing/TestingExceptions/TestingExceptions.MSTestTests/TestingExceptions.MSTestTests.csproj b/dotnet-testing/TestingExceptions/TestingExceptions.MSTestTests/TestingExceptions.MSTestTests.csproj index 6c2520b7c1..a2d5f49b30 100644 --- a/dotnet-testing/TestingExceptions/TestingExceptions.MSTestTests/TestingExceptions.MSTestTests.csproj +++ b/dotnet-testing/TestingExceptions/TestingExceptions.MSTestTests/TestingExceptions.MSTestTests.csproj @@ -1,7 +1,7 @@ - net7.0 + net11.0 enable enable @@ -9,10 +9,10 @@ - - - - + + + + diff --git a/dotnet-testing/TestingExceptions/TestingExceptions.NUnitTests/HeroTests.cs b/dotnet-testing/TestingExceptions/TestingExceptions.NUnitTests/HeroTests.cs index 8c801495d9..e834eaa180 100644 --- a/dotnet-testing/TestingExceptions/TestingExceptions.NUnitTests/HeroTests.cs +++ b/dotnet-testing/TestingExceptions/TestingExceptions.NUnitTests/HeroTests.cs @@ -1,14 +1,7 @@ -using System; - namespace TestingExceptions.NUnitTests { public class HeroTests { - [SetUp] - public void Setup() - { - } - [Test] public void GivenSufficientExperience_WhenLevelUpIsInvoked_ThenLeveIsIncreased() { @@ -30,25 +23,40 @@ public void GivenInsufficientExperience_WhenLevelUpIsInvoked_ThenExceptionIsThro var hero = new Hero(500); // Act - TestDelegate act = hero.LevelUp; + Action act = hero.LevelUp; // Assert Assert.Throws(act); Assert.Throws(hero.LevelUp); + Assert.That(hero.LevelUp, Throws.TypeOf()); + } + + [Test] + public void GivenInsufficientExperience_WhenLevelUpIsInvoked_ThenBaseExceptionTypeIsAccepted() + { + // Arrange + var hero = new Hero(500); + + // Act + Action act = hero.LevelUp; + + // Assert + Assert.Catch(act); + Assert.That(act, Throws.InstanceOf()); } [Test] - public void GivenInsufficientExperience_WhenLevelUpAsyncIsInvoked_ThenExceptionIsThrown() + public async Task GivenInsufficientExperience_WhenLevelUpAsyncIsInvoked_ThenExceptionIsThrown() { // Arrange var hero = new Hero(500); // Act - AsyncTestDelegate act = hero.LevelUpAsync; + Func act = hero.LevelUpAsync; // Assert - Assert.ThrowsAsync(act); - Assert.ThrowsAsync(hero.LevelUpAsync); + await Assert.ThrowsAsync(act); + await Assert.ThrowsAsync(hero.LevelUpAsync); } [Test] @@ -66,13 +74,13 @@ public void GivenInsufficientExperience_WhenLevelUpIsInvoked_ThenExceptionIsThro } [Test] - public void GivenInsufficientExperience_WhenLevelUpAsyncIsInvoked_ThenExceptionIsThrownWithCorrectMessage() + public async Task GivenInsufficientExperience_WhenLevelUpAsyncIsInvoked_ThenExceptionIsThrownWithCorrectMessage() { // Arrange var hero = new Hero(500); // Act - var exception = Assert.ThrowsAsync(hero.LevelUpAsync); + var exception = await Assert.ThrowsAsync(hero.LevelUpAsync); // Assert Assert.That(exception, Is.Not.Null); diff --git a/dotnet-testing/TestingExceptions/TestingExceptions.NUnitTests/TestingExceptions.NUnitTests.csproj b/dotnet-testing/TestingExceptions/TestingExceptions.NUnitTests/TestingExceptions.NUnitTests.csproj index 5e89f4ea7d..714922111b 100644 --- a/dotnet-testing/TestingExceptions/TestingExceptions.NUnitTests/TestingExceptions.NUnitTests.csproj +++ b/dotnet-testing/TestingExceptions/TestingExceptions.NUnitTests/TestingExceptions.NUnitTests.csproj @@ -1,7 +1,7 @@ - net7.0 + net11.0 enable enable @@ -9,11 +9,11 @@ - - - - - + + + + + diff --git a/dotnet-testing/TestingExceptions/TestingExceptions.xUnitTests/HeroTests.cs b/dotnet-testing/TestingExceptions/TestingExceptions.xUnitTests/HeroTests.cs index d4cc497916..9170c6c373 100644 --- a/dotnet-testing/TestingExceptions/TestingExceptions.xUnitTests/HeroTests.cs +++ b/dotnet-testing/TestingExceptions/TestingExceptions.xUnitTests/HeroTests.cs @@ -1,5 +1,3 @@ -using FluentAssertions; - namespace TestingExceptions.xUnitTests { public class HeroTests @@ -33,63 +31,58 @@ public void GivenInsufficientExperience_WhenLevelUpIsInvoked_ThenExceptionIsThro } [Fact] - public async Task GivenInsufficientExperience_WhenLevelUpAsyncIsInvoked_ThenExceptionIsThrown() + public void GivenInsufficientExperience_WhenLevelUpIsInvoked_ThenBaseExceptionTypeIsAccepted() { // Arrange var hero = new Hero(500); // Act - Func act = () => hero.LevelUpAsync(); + Action act = () => hero.LevelUp(); // Assert - await Assert.ThrowsAsync(act); - await Assert.ThrowsAsync(hero.LevelUpAsync); + Assert.ThrowsAny(act); } [Fact] - public void GivenInsufficientExperience_WhenLevelUpIsInvoked_ThenExceptionIsThrownWithCorrectMessage() + public async Task GivenInsufficientExperience_WhenLevelUpAsyncIsInvoked_ThenExceptionIsThrown() { // Arrange var hero = new Hero(500); // Act - var exception = Assert.Throws(() => hero.LevelUp()); + Func act = () => hero.LevelUpAsync(); // Assert - Assert.NotNull(exception); - Assert.Equal("Not enough Experience to level up! (Parameter 'Experience')", exception.Message); + await Assert.ThrowsAsync(act); + await Assert.ThrowsAsync(() => hero.LevelUpAsync()); } [Fact] - public async Task GivenInsufficientExperience_WhenLevelUpAsyncIsInvoked_ThenExceptionIsThrownWithCorrectMessage() + public void GivenInsufficientExperience_WhenLevelUpIsInvoked_ThenExceptionIsThrownWithCorrectMessage() { // Arrange var hero = new Hero(500); // Act - var exception = await Assert.ThrowsAsync(hero.LevelUpAsync); + var exception = Assert.Throws(() => hero.LevelUp()); // Assert Assert.NotNull(exception); - Assert.Equal("Not enough Experience to level up asynchronously! (Parameter 'Experience')", exception.Message); + Assert.Equal("Not enough Experience to level up! (Parameter 'Experience')", exception.Message); } [Fact] - public async Task GivenInsufficientExperience_WhenLevelUpIsInvoked_ThenExceptionIsThrownWithFluentAssertions() + public async Task GivenInsufficientExperience_WhenLevelUpAsyncIsInvoked_ThenExceptionIsThrownWithCorrectMessage() { // Arrange var hero = new Hero(500); // Act - Action actSync = () => hero.LevelUp(); - Func actAsync = () => hero.LevelUpAsync(); + var exception = await Assert.ThrowsAsync(hero.LevelUpAsync); // Assert - actSync.Should().Throw() - .WithMessage("Not enough Experience to level up! (Parameter 'Experience')"); - - await actAsync.Should().ThrowAsync() - .WithMessage("Not enough Experience to level up asynchronously! (Parameter 'Experience')"); + Assert.NotNull(exception); + Assert.Equal("Not enough Experience to level up asynchronously! (Parameter 'Experience')", exception.Message); } } } \ No newline at end of file diff --git a/dotnet-testing/TestingExceptions/TestingExceptions.xUnitTests/TestingExceptions.xUnitTests.csproj b/dotnet-testing/TestingExceptions/TestingExceptions.xUnitTests/TestingExceptions.xUnitTests.csproj index 2966fcb305..eb2eec35fa 100644 --- a/dotnet-testing/TestingExceptions/TestingExceptions.xUnitTests/TestingExceptions.xUnitTests.csproj +++ b/dotnet-testing/TestingExceptions/TestingExceptions.xUnitTests/TestingExceptions.xUnitTests.csproj @@ -1,7 +1,7 @@ - net7.0 + net11.0 enable enable @@ -9,14 +9,13 @@ - - - - + + + runtime; build; native; contentfiles; analyzers; buildtransitive all - + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/dotnet-testing/TestingExceptions/TestingExceptions/TestingExceptions.csproj b/dotnet-testing/TestingExceptions/TestingExceptions/TestingExceptions.csproj index f02677bf64..5bf6c9b8cf 100644 --- a/dotnet-testing/TestingExceptions/TestingExceptions/TestingExceptions.csproj +++ b/dotnet-testing/TestingExceptions/TestingExceptions/TestingExceptions.csproj @@ -2,7 +2,7 @@ Exe - net7.0 + net11.0 enable enable