|
| 1 | +--- |
| 2 | +title: Use MSTest with .NET Core on Windows |
| 3 | +description: How to use MSTest with .NET Core on Windows, using Visual Studio 2015 |
| 4 | +keywords: MSTest, .NET, .NET Core |
| 5 | +author: ncarandini |
| 6 | +manager: wpickett |
| 7 | +ms.date: 08/18/2016 |
| 8 | +ms.topic: article |
| 9 | +ms.prod: .net-core |
| 10 | +ms.technology: .net-core-technologies |
| 11 | +ms.devlang: dotnet |
| 12 | +ms.assetid: ed447641-3e85-4e50-b7ed-004630048a3e |
| 13 | +--- |
| 14 | + |
| 15 | +# Unit testing with MSTest and .NET Core on Windows, using Visual Studio 2015 |
| 16 | + |
| 17 | +by [Nicolò Caradini](https://github.com/ncarandini) |
| 18 | + |
| 19 | +While xUnit could be a better choice when targeting multiple platforms, with .NET Core on Windows is also possible to use MSTest. |
| 20 | + |
| 21 | +## Prerequisites |
| 22 | + |
| 23 | +Follow the instructions on [Getting started with .NET Core on Windows](../tutorials/using-on-windows.md) to create the library project. |
| 24 | + |
| 25 | +### Writing the test project using MSTest |
| 26 | + |
| 27 | +1. In Solution Explorer, open the context menu for the **Solution** node and choose **Add**, **New Solution Folder**. Name the folder "test". |
| 28 | + This is only a solution folder, not a physical folder. |
| 29 | + |
| 30 | +2. Open the context menu for the **test** folder and choose **Add**. **New Project**. In the **New Project** dialog, choose **Console Application (.NET Core)**. Name it "TestLibrary" and explicitly put it under the `Golden\test` path. |
| 31 | + |
| 32 | + > [!IMPORTANT] |
| 33 | + > The project needs to be a console application, not a class library. |
| 34 | +
|
| 35 | +3. In the **TestLibrary** project, open the context menu for the **References** node and choose **Add Reference**. |
| 36 | + |
| 37 | +4. In the **Reference Manager** dialog, check **Library** under the **Projects**, **Solution** node, and then click **OK**. |
| 38 | + |
| 39 | +5. In the **TestLibrary** project, open the `project.json` file, and replace `"Library": "1.0.0-*"` with `"Library": {"target": "project"}`. |
| 40 | + |
| 41 | + This is to avoid the resolution of the `Library` project to a NuGet package with the same name. Explicitly setting the target to "project" ensures that the tooling will first search for a project with that name, and not a package. |
| 42 | + |
| 43 | +6. Open the context menu for the **References** node and choose **Manage NuGet Packages**. |
| 44 | + |
| 45 | +7. Choose "nuget.org" as the **Package source**, and choose the **Browse** tab. Check the **Include prerelease** checkbox, and then browse for **MSTest.TestFramework** version 1.0.1-preview or newer, and then click **Install**. |
| 46 | + |
| 47 | +8. Browse for **dotnet-test-mstest** version 1.1.1-preview or newer, and then click **Install**. |
| 48 | + |
| 49 | +9. Edit `project.json` to add `"testRunner": "mstest",` after the `"version"` section. |
| 50 | + |
| 51 | +10. Add a `LibraryTests.cs` class file to the **TestLibrary** project, add the `using` directives `Microsoft.VisualStudio.TestTools.UnitTesting;` and `using Library;` to the top of the file, and add the following code to the class: |
| 52 | + ```csharp |
| 53 | + [TestClass] |
| 54 | + public class LibraryTests |
| 55 | + { |
| 56 | + [TestMethod] |
| 57 | + public void ThingGetsObjectValFromNumber() |
| 58 | + { |
| 59 | + Assert.AreEqual(42, new Thing().Get(42)); |
| 60 | + } |
| 61 | + } |
| 62 | + ``` |
| 63 | + * Optionally, delete the `Program.cs` file from the **TestLibrary** project, and remove `"buildOptions": {"emitEntryPoint": true},` from `project.json`. |
| 64 | + |
| 65 | + You should now be able to build the solution. |
| 66 | + |
| 67 | +11. On the **Test** menu, choose **Windows**, **Test Explorer**, and in Test Explorer choose **Run All**. |
| 68 | + |
| 69 | + The test should pass. |
0 commit comments