Skip to content
This repository was archived by the owner on Nov 9, 2018. It is now read-only.

How to create test projects

Connie Yau edited this page May 5, 2015 · 14 revisions

Setup

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.

File system structure

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 test project's project.json file

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:

  1. We reference the actual project we want to test: MyProject. Make sure the version matches.
  2. We reference the xunit.runner.dnx runner.
  3. We define the test command. This is the command that build runs during the test target.
  4. xUnit repository contains the latest versions of the xUnit runner and libraries.

The global.json file

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"]
}

Dependencies install

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.

Running tests

Ad-hoc

cd test/MyProject.Tests
dnx . test

From Visual Studio

You can run tests using the built-in testing tools.

Clone this wiki locally