How to create a dotnet core NUnit Test Project using dotnet-cli

NET Core command-line interface (dotnet-cli) is a new NET Core toolset (commands) for developing NET Core Applications. The dotnet-cli toolset provides a list of pre-installed dotnet core project templates from which the user can create various applications as a dotnet core NUnit Test Project, WPF (Windows Presentation Foundation) application, console application, Web application, Windows Form application, and other test projects. The application created with dotnet-cli can be executed on any Windows, Linux, or macOS operating system

How to create a “dotnet core NUnit Test Project”

The user can use the dotnet new “NUnit 3 Test Project” or “dotnet new nunit” command from the dotnet-CLI toolset to create a new dotnet core NUnit Test Project. Here 3 denotes the NUnit versioning, this might change as per the dotnet core runtime version installed on your machine. You can check the installed templates by using the dotnet new –list command. The command provides a few options that can be applied as a parameter to tweak the output of the command as shown below :

“dotnet new nunit –no-restore”: This command does not restore the required project dependencies by the test project. Please follow through to read about a few “dotnet new nunit” command usage.

Prerequisite:

NET Core SDK 2.2 and above. The current version is .NET Core 5.0 and can be downloaded here.

Command Syntax:

The format of the command is as follows: dotnet new nunit [options]

The details of the available command options can be listed using the –help option as follows:

c:\>dotnet new nunit --help
Usage: new [options]

Options:
  -h, --help          Displays help for this command.
  -l, --list          Lists templates containing the specified name. If no name is specified, lists all templates.
  -n, --name          The name for the output being created. If no name is specified, the name of the current directory is used.
  -o, --output        Location to place the generated output.
  -i, --install       Installs a source or a template pack.
  -u, --uninstall     Uninstalls a source or a template pack.
  --nuget-source      Specifies a NuGet source to use during install.
  --type              Filters templates based on available types. Predefined values are "project", "item" or "other".
  --dry-run           Displays a summary of what would happen if the given command line were run if it would result in a template creation.
  --force             Forces content to be generated even if it would change existing files.
  -lang, --language   Filters templates based on language and specifies the language of the template to create.


NUnit 3 Test Project (C#)
Author: Aleksei Kharlov aka halex2005 (codeofclimber.ru)
Description: A project that contains NUnit tests that can run on .NET Core on Windows, Linux and macOS
Options:
  -f|--framework    The target framework for the project.
                        netcoreapp1.0    - Target netcoreapp1.0
                        netcoreapp1.1    - Target netcoreapp1.1
                        netcoreapp2.0    - Target netcoreapp2.0
                        netcoreapp2.1    - Target netcoreapp2.1
                        netcoreapp2.2    - Target netcoreapp2.2
                        netcoreapp3.0    - Target netcoreapp3.0
                        net35            - Target net35
                        net40            - Target net40
                        net45            - Target net45
                        net451           - Target net451
                        net452           - Target net452
                        net46            - Target net46
                        net461           - Target net461
                        net462           - Target net462
                        net47            - Target net47
                        net471           - Target net471
                        net472           - Target net472
                        net48            - Target net48
                    Default: netcoreapp3.0

  -p|--enable-pack  Whether or not to enable packaging (via ("dotnet pack") for the project.
                    bool - Optional
                    Default: false / (*) true

  --no-restore      If specified, skips the automatic restore of the project on create.
                    bool - Optional
                    Default: false / (*) true


* Indicates the value used if the switch is provided without a value.

Command Usage – dotnet new nunit:

1. “dotnet new nunit”

The command creates a new “NET Core NUnit Test” project containing NUnit tests. As the name of the project is not specified in the command, by default the name of the project will be taken from the name of the folder the command has been executed in. The command also restores the dependencies required by the test project. As the default language is C#, a C# Unit Test project will be created. The list of files and folder created by the command is given below:

dotnet core nunit project
c:\temp>dotnet new nunit
The template "NUnit 3 Test Project" was created successfully.

Processing post-creation actions...
Running 'dotnet restore' on c:\temp\temp.csproj...
  Restore completed in 22.08 sec for c:\temp\temp.csproj.

Restore succeeded.

The list of the files and folders created:

c:\temp>dir
 Volume in drive C is OSDisk
 Volume Serial Number is 8053-FAC8

 Directory of c:\temp

06/20/2019  06:19 PM    <DIR>          .
06/20/2019  06:19 PM    <DIR>          ..
06/20/2019  06:19 PM    <DIR>          obj
06/20/2019  06:19 PM               421 temp.csproj
06/20/2019  06:19 PM               233 UnitTest1.cs

The content of UnitTest1.cs

c:\temp>type UnitTest1.cs

using NUnit.Framework;
namespace Tests
{
    public class Tests
    {
        [SetUp]
        public void Setup()
        {
        }

        [Test]
        public void Test1()
        {
            Assert.Pass();
        }
    }
}

2. “dotnet new nunit –name Mytest”

The command creates a directory name “MyTest” only if it does not exist and then creates a new “NET Core NUnit Test” project containing NUnit tests, having the name of MyTest inside the directory, additionally, the command also restores the dependencies required by the project. The list of files and directories created by the command are given below:

c:\>dotnet new nunit --name MyTest
The template "NUnit 3 Test Project" was created successfully.

Processing post-creation actions...
Running 'dotnet restore' on MyTest\MyTest.csproj...
  Restore completed in 1.36 sec for c:\MyTest\MyTest.csproj.

Restore succeeded.

The list of files and folder created:

c:\MyTest>dir

 Volume in drive C is OSDisk
 Volume Serial Number is 8053-FAC8

 Directory of c:\MyTest

06/20/2019  06:25 PM    <DIR>          .
06/20/2019  06:25 PM    <DIR>          ..
06/20/2019  06:25 PM               421 MyTest.csproj
06/20/2019  06:25 PM    <DIR>          obj
06/20/2019  06:25 PM               233 UnitTest1.cs

3. “dotnet new nunit –name MyTest –no-restore”

The command creates a directory name “MyTest” only if it does not exist and the creates a new “NET Core NUnit Test” project containing NUnit tests, having the name of MyTest inside the directory, the command does not restore the dependencies required by the project. The list of files and directories created by the command are given below:

c:\>dotnet new nunit --name MyTest --no-restore
The template "NUnit 3 Test Project" was created successfully.

The list of the files created:

c:\MyTest>dir
 Volume in drive C is OSDisk
 Volume Serial Number is 8053-FAC8

 Directory of c:\MyTest

06/20/2019  06:27 PM    <DIR>          .
06/20/2019  06:27 PM    <DIR>          ..
06/20/2019  06:27 PM               421 MyTest.csproj
06/20/2019  06:27 PM               233 UnitTest1.cs

4. “dotnet new nunit –name MyTest –language F#”

The command creates a directory name “MyTest” only if it does not exist and the creates a new “NET Core NUnit Test” project containing NUnit tests, the programming language for the project created is F#, and the extension of the project is .fsproj. Additionally, the command also restores the dependencies required for the project. The list of the files and folder created are given below:

c:\>dotnet new nunit --name MyTest --language F#
The template "NUnit 3 Test Project" was created successfully.

Processing post-creation actions...
Running 'dotnet restore' on MyTest\MyTest.fsproj...
  Restore completed in 1.5 sec for c:\MyTest\MyTest.fsproj.

Restore succeeded.

The list of files and folders created:

c:\MyTest>dir
 Volume in drive C is OSDisk
 Volume Serial Number is 8053-FAC8

 Directory of c:\MyTest

06/20/2019  06:30 PM    <DIR>          .
06/20/2019  06:30 PM    <DIR>          ..
06/20/2019  06:30 PM               509 MyTest.fsproj
06/20/2019  06:30 PM    <DIR>          obj
06/20/2019  06:30 PM                47 Program.fs
06/20/2019  06:30 PM               175 UnitTest1.fs

The content of UnitTest1.fs

c:\MyTest>type UnitTest1.fs

namespace Tests

open NUnit.Framework

type TestClass () =

    [<SetUp>]
    member this.Setup () =
        ()

    [<Test>]
    member this.Test1 () =
        Assert.Pass()

The content of MyTest.proj file:

c:\MyTest>type MyTest.fsproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <IsPackable>false</IsPackable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="nunit" Version="3.11.0" />
    <PackageReference Include="NUnit3TestAdapter" Version="3.12.0" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
  </ItemGroup>

  <ItemGroup>
    <Compile Include="UnitTest1.fs"/>
    <Compile Include="Program.fs"/>
  </ItemGroup>

</Project>

I hope you found this post on how to create a dotnet core NUnit Test Project using dotnet-cli helpful. Thanks for visiting. Cheers!!!

Learn about NUnit Test at: www.NUnit.org

[Further Readings: How to create a dotnet core xUnit Test Project using dotnet-cli |  How to create a dotnet core MSTest Project using dotnet-cli |  How to create dotnet core WinForms Application using dotnet-cli |  How to create a dotnet core WPF application using dotnet-cli |  How to create a dotnet core console app using dotnet-cli |  Top 7 Web Frameworks to Learn and Focus on in 2021 |  Top 7 Programming Languages to Focus on in 2021 |  Creational Design Patterns |   Abstract Factory Design Pattern in C#  |  Factory Method Pattern in C#  |  Singleton Design Pattern in C#  |  Introduction to Design Patterns  |  How to add Git Bash to Windows Terminal Application  |  How to customize Windows Terminal Application  |  How to customize Windows Terminal Key Bindings    ]

0 0 votes
Article Rating
Subscribe
Notify of
guest
6 Comments
oldest
newest most voted
Inline Feedbacks
View all comments
6
0
Would love your thoughts, please comment.x
()
x