How to create a dotnet core MSTest 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 MSTest, 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 MSTest project”

The user can use the dotnet new “Unit Test Project” or “dotnet new mstest” command from the dotnet-CLI toolset to create a new dotnet core Microsoft Unit 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 mstest –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 mstest” 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 mstest [options]

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

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


Unit Test Project (C#)
Author: Microsoft
Description: A project that contains unit 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 mstest:

1. “dotnet new mstest”

The command creates a new “NET Core MS Unit Test” project. 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# so a C# Unit Test project will be created. The list of files and folder created by the command is given below:

dotnet core mstest application using cli
C:\temp>dotnet new mstest
The template "Unit Test Project" was created successfully.

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

Restore succeeded.

The list of the files and folders:

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

 Directory of C:\temp

06/17/2019  07:03 PM    <DIR>          .
06/17/2019  07:03 PM    <DIR>          ..
06/17/2019  07:03 PM    <DIR>          obj
06/17/2019  07:03 PM               435 temp.csproj
06/17/2019  07:03 PM               215 UnitTest1.cs

2. “dotnet new mstest –name MyTest”

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

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

Restore succeeded.

List of files and directories:

C:\>cd MyTest

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

 Directory of C:\MyTest

06/17/2019  07:05 PM    <DIR>          .
06/17/2019  07:05 PM    <DIR>          ..
06/17/2019  07:05 PM               435 MyTest.csproj
06/17/2019  07:05 PM    <DIR>          obj
06/17/2019  07:05 PM               217 UnitTest1.cs

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

The command creates a directory name “MyTest” only if doesn’t exist and then creates a new “NET Core MS Unit Test” project having the name of MyTest inside the directory, additionally, 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 mstest --name MyTest --no-restore
The template "Unit Test Project" was created successfully.

List of files created:

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

 Directory of C:\MyTest

06/18/2019  07:06 PM    <DIR>          .
06/18/2019  07:06 PM    <DIR>          ..
06/18/2019  07:06 PM               435 MyTest.csproj
06/18/2019  07:06 PM               217 UnitTest1.cs

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

The command creates a directory name “MyTest” only if doesn’t exist and then creates a new “NET Core MS Unit Test” F# project type having the name of MyTest inside the directory, additionally, the command restores the dependencies required by the project. The other languages options by default are C#, F#, and VB. The list of files and directories created by the command are given below:

C:\>dotnet new mstest --name MyTest --language F#
The template "Unit Test Project" was created successfully.

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

Restore succeeded.

List of the files and directories created:

C:\>cd MyTest

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

 Directory of C:\MyTest

06/18/2019  07:09 PM    <DIR>          .
06/18/2019  07:09 PM    <DIR>          ..
06/18/2019  07:09 PM               596 MyTest.fsproj
06/18/2019  07:09 PM    <DIR>          obj
06/18/2019  07:09 PM                48 Program.fs
06/18/2019  07:09 PM               214 Tests.fs

5. “dotnet new mstest –name MyTest –enable-pack”

The command creates a directory name “MyTest” only if doesn’t exist and then creates a new “NET Core MS Unit Test” project having the name of MyTest inside the directory, additionally, the command also restores the dependencies required by the project. The –enable-pack option set the value of IsPackable element to true, which in turn make the project exportable/included as a nuget package. By default the value of IsPackable is false. The content of MyTest.csproj is given below.

C:\MyTest>type MyTest.csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <IsPackable>true</IsPackable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.0" />
    <PackageReference Include="MSTest.TestAdapter" Version="1.2.1" />
    <PackageReference Include="MSTest.TestFramework" Version="1.2.1" />
  </ItemGroup>

</Project>

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

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