Skip to content

Updated NUnit testing tutorials #40315

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/core/porting/porting-approaches.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ The best way to make sure everything works when you've ported your code is to te
- [Getting Started](https://xunit.net/docs/getting-started/netcore/cmdline)
- [Tool to convert an MSTest project to xUnit](https://github.com/dotnet/codeformatter/tree/main/src/XUnitConverter)
- [NUnit](https://nunit.org/)
- [Getting Started](https://github.com/nunit/docs/wiki/Installation)
- [Getting Started](https://docs.nunit.org/articles/nunit/getting-started/installation)
- [Blog post about migrating from MSTest to NUnit](https://www.florian-rappl.de/News/Page/275/convert-mstest-to-nunit)
- [MSTest](/visualstudio/test/unit-test-basics)

Expand Down
2 changes: 1 addition & 1 deletion docs/core/testing/order-unit-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ Then in a test class you set the test case order with the `TestCaseOrdererAttrib

## Order by priority

To order tests explicitly, NUnit provides an [`OrderAttribute`](https://github.com/nunit/docs/wiki/Order-Attribute). Tests with this attribute are started before tests without. The order value is used to determine the order to run the unit tests.
To order tests explicitly, NUnit provides an [`OrderAttribute`](https://docs.nunit.org/articles/nunit/writing-tests/attributes/order). Tests with this attribute are started before tests without. The order value is used to determine the order to run the unit tests.

:::code language="csharp" source="snippets/order-unit-tests/csharp/NUnit.TestProject/ByOrder.cs":::

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<IsPackable>false</IsPackable>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<IsPackable>false</IsPackable>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
Expand Down
12 changes: 6 additions & 6 deletions docs/core/testing/unit-testing-fsharp-with-nunit.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ This tutorial takes you through an interactive experience building a sample solu

## Prerequisites

- [.NET Core 2.1 SDK](https://dotnet.microsoft.com/download) or later versions.
- [.NET 8 SDK](https://dotnet.microsoft.com/download) or later versions.
- A text editor or code editor of your choice.

## Creating the source project
Expand Down Expand Up @@ -76,9 +76,9 @@ This creates a test project that uses NUnit as the test framework. The generated

```xml
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
<PackageReference Include="NUnit" Version="3.9.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.9.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="NUnit" Version="4.1.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
</ItemGroup>
```

Expand Down Expand Up @@ -125,10 +125,10 @@ type TestClass () =

[<Test>]
member this.TestMethodPassing() =
Assert.True(true)
Assert.That(true, Is.True)

[<Test>]
member this.FailEveryTime() = Assert.True(false)
member this.FailEveryTime() = Assert.That(false, Is.True)
```

The `[<TestFixture>]` attribute denotes a class that contains tests. The `[<Test>]` attribute denotes a test method that is run by the test runner. From the *unit-testing-with-fsharp* directory, execute `dotnet test` to build the tests and the class library and then run the tests. The NUnit test runner contains the program entry point to run your tests. `dotnet test` starts the test runner using the unit test project you've created.
Expand Down
9 changes: 6 additions & 3 deletions docs/core/testing/unit-testing-visual-basic-with-nunit.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: Unit testing Visual Basic in .NET Core with dotnet test and NUnit
description: Learn unit test concepts in .NET Core through an interactive experience building a sample Visual Basic solution step-by-step using NUnit.
author: rprouse
ms.date: 10/04/2018
ms.date: 03/27/2024
---
# Unit testing Visual Basic .NET Core libraries using dotnet test and NUnit

Expand All @@ -12,7 +12,7 @@ This tutorial takes you through an interactive experience building a sample solu

## Prerequisites

- [.NET Core 2.1 SDK](https://dotnet.microsoft.com/download) or later versions.
- [.NET 8 SDK](https://dotnet.microsoft.com/download) or later versions.
- A text editor or code editor of your choice.

## Creating the source project
Expand Down Expand Up @@ -78,6 +78,9 @@ The [dotnet new](../tools/dotnet-new.md) command creates a test project that use

[!code-xml[Packages](~/samples/snippets/core/testing/unit-testing-vb-nunit/vb/PrimeService.Tests/PrimeService.Tests.vbproj#Packages)]

> [!NOTE]
> Prior to .NET 9, the generated code may reference older versions of the NUnit test framework. You may use [dotnet CLI](/nuget/consume-packages/install-use-packages-dotnet-cli) to update the packages. Alternatively, open the *PrimeService.Tests.vbproj* file and replace the contents of the package references item group with the code above.

The test project requires other packages to create and run unit tests. `dotnet new` in the previous step added NUnit and the NUnit test adapter. Now, add the `PrimeService` class library as another dependency to the project. Use the [`dotnet add reference`](../tools/dotnet-add-reference.md) command:

```dotnetcli
Expand Down Expand Up @@ -121,7 +124,7 @@ Namespace PrimeService.Tests
Sub IsPrime_InputIs1_ReturnFalse()
Dim result As Boolean = _primeService.IsPrime(1)

Assert.False(result, "1 should not be prime")
Assert.That(result, [Is].False, $"1 should not be prime")
End Sub

End Class
Expand Down
9 changes: 6 additions & 3 deletions docs/core/testing/unit-testing-with-nunit.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: Unit testing C# with NUnit and .NET Core
description: Learn unit test concepts in C# and .NET Core through an interactive experience building a sample solution step-by-step using dotnet test and NUnit.
author: rprouse
ms.date: 07/26/2022
ms.date: 03/27/2024
ms.custom: devdivchpfy22
---
# Unit testing C# with NUnit and .NET Core
Expand All @@ -13,7 +13,7 @@ This tutorial takes you through an interactive experience building a sample solu

## Prerequisites

- [.NET 6.0](https://dotnet.microsoft.com/download) or later versions.
- [.NET 8.0](https://dotnet.microsoft.com/download) or later versions.
- A text editor or code editor of your choice.

## Creating the source project
Expand Down Expand Up @@ -84,6 +84,9 @@ The [dotnet new](../tools/dotnet-new.md) command creates a test project that use

[!code-xml[Packages](~/samples/snippets/core/testing/unit-testing-using-nunit/csharp/PrimeService.Tests/PrimeService.Tests.csproj#Packages)]

> [!NOTE]
> Prior to .NET 9, the generated code may reference older versions of the NUnit test framework. You may use [dotnet CLI](/nuget/consume-packages/install-use-packages-dotnet-cli) to update the packages. Alternatively, open the *PrimeService.Tests.csproj* file and replace the contents of the package references item group with the code above.

The test project requires other packages to create and run unit tests. The `dotnet new` command in the previous step added the Microsoft test SDK, the NUnit test framework, and the NUnit test adapter. Now, add the `PrimeService` class library as another dependency to the project. Use the [`dotnet add reference`](../tools/dotnet-add-reference.md) command:

```dotnetcli
Expand Down Expand Up @@ -137,7 +140,7 @@ namespace Prime.UnitTests.Services
{
var result = _primeService.IsPrime(1);

Assert.IsFalse(result, "1 should not be prime");
Assert.That(result, Is.False, "1 should not be prime");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

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

<IsPackable>false</IsPackable>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

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

<IsPackable>false</IsPackable>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>
Expand Down