Add a test file to C# project via Command Line Terminal

Unit Test is important in C# development journey. In this post, I am going to talk about how to add a test file via command line terminal in Windows 10 and we compile the project in Visual Studio Code.

Start A New Project

I start a new project in a directory named TestProject. Type in the following code in a terminal. (whichever folder or directory that you are comfortable with)

dotnet new project


At this stage you should have a bright new project, I named mine as project, you can name yours whatever you need to.

Add A Test File

Before I create my test file, I make another folder under TestProject folder and the new folder’s name is test. I prefer doing it in the command line, but you can do it in GUI if it is easy for you.

In this project, i am using the xunit test framework, which is a widely used and popular these days.

Under the test folder, run the following code in the terminal.

dotnet new xunit

Screenshot – xunit test file restored successfully

At this stage, your file directory should look like this, but the job is done yet. VS Code need to know which file or program should be tested when you run the test. To do that, you need to add reference to the target file.

Add A Reference The Existing Project

Run the following code in the terminal under the test folder.

dotnet add reference .\test.csproj

test.csproj is the target file we are testing in this project.

As you can see, after we add the reference to the project, in test.csproj, we can locate the project reference.

Leave a Reply

Your email address will not be published. Required fields are marked *