How to create a dotnet core xUnit 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 xUnit 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 xUnit Test Project”

The user can use the dotnet new “xUnit Test Project” or “dotnet new xUnit” command from the dotnet-CLI toolset to create a new dotnet core xUnit Test Project. 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 xUnit –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 xUnit” command usage.

Prerequisite:

.NET Core SDK 1.x 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 xunit [options]

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

C:\>dotnet new xunit --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".
  --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.


xUnit Test Project (C#)
Author: Microsoft
Description: A project that contains xUnit.net tests that can run on .NET Core on Windows, Linux and macOS
Options:
  -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 xunit:

1. “dotnet new xunit”

The command creates a new “NET Core xUnit Test” project containing xUnit 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 xUnit Test Project
C:\temp>dotnet new xunit
The template "xUnit Test Project" was created successfully.

Processing post-creation actions...
Running 'dotnet restore' on c:\temp\temp.csproj...
  Restoring packages for c:\temp\temp.csproj...
  Restoring packages for c:\temp\temp.csproj...
  Installing xunit.abstractions 2.0.1.
  Installing xunit.extensibility.core 2.3.1.
  Installing xunit.extensibility.execution 2.3.1.
  Installing xunit.core 2.3.1.
  Installing xunit.analyzers 0.7.0.
  Installing xunit.assert 2.3.1.
  Installing xunit 2.3.1.
  Installing xunit.runner.visualstudio 2.3.1.
  Generating MSBuild file c:\temp\obj\temp.csproj.nuget.g.props.
  Generating MSBuild file c:\temp\obj\temp.csproj.nuget.g.targets.
  Restore completed in 7.61 sec for c:\temp\temp.csproj.
  Installing System.Xml.XPath.XDocument 4.0.1.
  Installing System.Diagnostics.StackTrace 4.0.1.
  Installing Microsoft.NETCore.DotNetHost 1.0.1.
  Installing Microsoft.CodeAnalysis.Common 1.3.0.
  Installing Microsoft.NETCore.DotNetHostResolver 1.0.1.
  Installing runtime.native.System.Net.Security 4.0.1.
  Installing System.Security.Claims 4.0.1.
  Installing dotnet-xunit 2.3.1.
  Installing Microsoft.NETCore.App 1.0.0.
  Installing System.Security.Principal.Windows 4.0.0.
  Installing Microsoft.NETCore.Windows.ApiSets 1.0.1.
  Installing Microsoft.NETCore.Runtime.CoreCLR 1.0.2.
  Installing Microsoft.NETCore.Jit 1.0.2.
  Installing Microsoft.CodeAnalysis.VisualBasic 1.3.0.
  Installing Microsoft.CodeAnalysis.CSharp 1.3.0.
  Installing Microsoft.NETCore.DotNetHostPolicy 1.0.1.
  Installing Libuv 1.9.0.
  Installing System.Net.Security 4.0.0.
  Installing System.IO.FileSystem.Watcher 4.0.0.
  Installing Microsoft.VisualBasic 10.0.1.
  Installing System.ComponentModel.Annotations 4.1.0.
  Installing System.IO.UnmanagedMemoryStream 4.0.1.
  Installing System.Net.Requests 4.0.11.
  Installing System.Net.WebHeaderCollection 4.0.1.
  Installing System.Numerics.Vectors 4.1.1.
  Installing System.Reflection.DispatchProxy 4.0.1.
  Installing System.Threading.Tasks.Parallel 4.0.1.
  Installing System.Net.NameResolution 4.0.0.
  Installing System.IO.MemoryMappedFiles 4.0.0.
  Restore completed in 32.02 sec for c:\temp\temp.csproj.

Restore succeeded.

The list of the files and folders created:

c:\temp>dir
 Volume in drive C is Windows
 Volume Serial Number is 945D-A84B

 Directory of c:\temp

06/18/2019  10:45 PM    <DIR>          .
06/18/2019  10:45 PM    <DIR>          ..
06/18/2019  10:45 PM                 0 dir
06/18/2019  10:33 PM    <DIR>          obj
06/18/2019  10:33 PM               498 temp.csproj
06/18/2019  10:33 PM               164 UnitTest1.cs

The content of UnitTest1.cs

c:\temp>type UnitTest1.cs

using System;
using Xunit;

namespace temp
{
    public class UnitTest1
    {
        [Fact]
        public void Test1()
        {

        }
    }
}

2. “dotnet new xunit –name MyTest”

The command creates a directory name “MyTest” only if doesn’t exist and then creates a new “NET Core xUnit Test” project containing xUnit 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 xunit --name MyTest
The template "xUnit Test Project" was created successfully.

Processing post-creation actions...
Running 'dotnet restore' on MyTest\MyTest.csproj...
  Restoring packages for c:\MyTest\MyTest.csproj...
  Restore completed in 60.69 ms for c:\MyTest\MyTest.csproj.
  Generating MSBuild file c:\MyTest\obj\MyTest.csproj.nuget.g.props.
  Generating MSBuild file c:\MyTest\obj\MyTest.csproj.nuget.g.targets.
  Restore completed in 646.71 ms for c:\MyTest\MyTest.csproj.

Restore succeeded.

The list of files and folder created:

c:\>cd MyTest

c:\MyTest>dir
 Volume in drive C is Windows
 Volume Serial Number is 945D-A84B

 Directory of c:\MyTest

06/18/2019  10:48 PM    <DIR>          .
06/18/2019  10:48 PM    <DIR>          ..
06/18/2019  10:48 PM               498 MyTest.csproj
06/18/2019  10:48 PM    <DIR>          obj
06/18/2019  10:48 PM               166 UnitTest1.cs

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

The command creates a directory name “MyTest” only if doesn’t exist and the creates a new “NET Core xUnit Test” project containing xUnit 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 xunit --name MyTest --no-restore
The template "xUnit 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/19/2019  05:48 PM    <DIR>          .
06/19/2019  05:48 PM    <DIR>          ..
06/19/2019  05:48 PM               427 MyTest.csproj
06/19/2019  05:48 PM               166 UnitTest1.cs

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

The command creates a directory name “MyTest” only if doesn’t exist and the creates a new “NET Core xUnit Test” project containing xUnit 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 xunit --name MyTest --language F#
The template "xUnit Test Project" was created successfully.

Processing post-creation actions...
Running 'dotnet restore' on MyTest\MyTest.fsproj...
  Restore completed in 6.7 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/19/2019  05:54 PM    <DIR>          .
06/19/2019  05:54 PM    <DIR>          ..
06/19/2019  05:54 PM               588 MyTest.fsproj
06/19/2019  05:54 PM    <DIR>          obj
06/19/2019  05:54 PM                48 Program.fs
06/19/2019  05:54 PM                98 Tests.fs

The content of Tests.fs

C:\MyTest>type Tests.fs
module Tests

open System
open Xunit

[<Fact>]
let ``My test`` () =
    Assert.True(true)

The content of MyTest.proj file:

C:\MyTest>type MyTest.fsproj
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.0</TargetFramework>

    <IsPackable>false</IsPackable>
    <GenerateProgramFile>false</GenerateProgramFile>
  </PropertyGroup>

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

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.0.1" />
    <PackageReference Include="xunit" Version="2.4.0" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
  </ItemGroup>

</Project>

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

Learn about xUnit Test at: www.xunit.net

[Further Readings: 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
9 Comments
oldest
newest most voted
Inline Feedbacks
View all comments
9
0
Would love your thoughts, please comment.x
()
x