-
Notifications
You must be signed in to change notification settings - Fork 41
How to create test projects
This article describes how to create a new unit test project that will be run as part of build. The two important artifacts you need to create, are a project.json
file and a global.json
file.
If you are just starting out, it is very likely that you have a file system layout similar to this:
project.json
File1.cs
File2.cs
...
Unit tests work in a separate project. This means we need to reorganise the file system layout. You should do something similar to this:
global.json
src/
MyProject
project.json
File1.cs
File2.cs
...
test/
MyProject.Tests
project.json
File1Tests.cs
...
...
You need a directory with the name for your project assembly (MyProject
), the mechanism for dependencies lookup will use the file system to find your project.
The project.json
inside test/
should look somewhat similar to the following.
{
"dependencies": {
"MyProject": "1.0.0-*",
"xunit": "2.1.0-*",
"xunit.runner.dnx": "2.1.0-*"
},
"frameworks": {
"aspnet50": { },
"aspnetcore50": { }
},
"commands": {
"test": "xunit.runner.dnx"
}
}
There are three things to note:
- We reference the actual project we want to test:
MyProject
. Make sure the version matches. - We reference the
xunit.runner.dnx
runner. - We define the
test
command. This is the command that build runs during the test target. - xUnit repository contains the latest versions of the xUnit runner and libraries.
Since the projects we're testing are under src/
and not test/
, we need to add a global.json
file. This file tells Project K where to find additional sources. It should go in your repository's root and have the following contents.
{
"sources": ["src", "test"]
}
You should cd
to the parent directory of src/
and test/
and run dnu restore
. This will restore all dependencies, including the xunit
testing framework.
cd test/MyProject.Tests
dnx . test
You can run tests using the built-in testing tools.