Posted by Anuraj on Tuesday, December 20, 2016 Reading time :2 minutes
This post will give a brief idea about creating and executing unit tests in dotnet core. Unit testing is a software development process in which the smallest testable parts of an application, called units, are individually and independently scrutinized for proper operation. Unit testing is often automated but it can also be done manually.
To create a unit test project, you can open execute the following command in command line.
dotnet new -t xunittest
The command will create a simple unit test project with project.json file and Tests.cs file.
Here is the Test.cs file.
And here is the project.json file.
Now you can run
dotnet restore command to restore the dependencies. And you can run the unit tests with
dotnet test command.
In the code, there is a new Fact attribute is added instead of TestMethod attribute, because in .net core, xUnit is the default unit test framework, since it is cross platform. If you are using MS Test, you don’t need to change, you can use MS Test as well. For that you need to modify the project.json and use MS Test Framework and test runners.
Here is the changes required to run MS Test.
Also your code also need to change from xUnit to MS Test syntax.
And here is the results.
Last but not least, nUnit is supported in .NET Core. I’ve tested it in a few scenarios and works as expected so you should be able to use it with the same reservations as for any other beta library. Similar to MS Test, you need to modify the project.json file.
Also your code also need to change to include NUnit attributes.
And here is the execution results.
You can use global.json file to include your project references. By default project-to-project references must be sibling folders. Using a global.json file allows a solution to specify non-standard locations to locate references
Happy Programming :)
What do you think? I would like to hear your thoughts, suggestions, and questions in the comments section below.