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

Screenshot

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.


5 Replies to “Add a test file to C# project via Command Line Terminal”

  1. My wife and i were so satisfied when Peter managed to finish up his survey while using the precious recommendations he received when using the weblog. It is now and again perplexing just to be giving away helpful tips that men and women could have been making money from. And we discover we have the blog owner to appreciate because of that. Most of the explanations you’ve made, the easy blog menu, the friendships you assist to engender – it’s everything excellent, and it’s assisting our son and our family do think that content is brilliant, and that’s extraordinarily fundamental. Many thanks for all the pieces!

  2. I am only writing to make you understand what a really good experience my wife’s princess obtained viewing your webblog. She noticed some things, which included what it is like to possess an ideal teaching spirit to make others without difficulty learn about specific tortuous things. You undoubtedly surpassed readers’ expected results. Thanks for rendering those productive, trusted, informative as well as unique tips on your topic to Mary.

  3. My husband and i felt absolutely ecstatic Edward could complete his homework from the ideas he discovered from your very own site. It’s not at all simplistic just to be giving away helpful tips that men and women could have been making money from. And we discover we have the blog owner to appreciate because of that. Most of the explanations you’ve made, the easy blog menu, the friendships you assist to engender – it’s everything excellent, and it’s assisting our son and our family do think that content is brilliant, and that’s extraordinarily fundamental. Many thanks for all the pieces!

  4. You are likely to sing with nightingale in conservation areas and dance with peacock in national parks. Glenda Friedrick Gusba

Leave a Reply

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