Skip to content

Instantly share code, notes, and snippets.

@fmcarvalho
Last active February 24, 2017 12:20
Show Gist options
  • Save fmcarvalho/bf8a04b0b812c91bdd58fe8d0db488b6 to your computer and use it in GitHub Desktop.
Save fmcarvalho/bf8a04b0b812c91bdd58fe8d0db488b6 to your computer and use it in GitHub Desktop.
Build a Visual Studio Code solution from the scratch for .Net Core with unit tests

This topic applies to .NET Core Tools Preview 2 and Visual Studio 2015.

First of all ensure that you have all required tools installed -- Installation

Cheatsheet for VS Code .Net Core solution with unit tests

  1. Create the projects folders structure according to Figure 1 (NOTE: create only folders and not the files), e.g. in boilerplate solution we have 3 projects in src folder, corresponding to Fibonacci, Primes and App and two unit tests projects (Primes.Tests and Fibonacci.Tests).
  2. CD into each library directory (e.g. src/Fibonacci)
  3. Run dotnet new -t lib to create the library source project or just dotnet new for applications.
  4. Check the project.json which contains dependencies necessary to build the library.
  5. Rename Library.cs (e.g.FibSupplier.cs)
  6. Open library C# file and rename the class according to the new file name (e.g. class FibSupplier {…)
  7. Rename the namespace according to the directory name (e.g. namespace Fibonacci {…)
  8. For each project that depends of other projects you must refer those projects in the dependencies property of project.json. e.g. the project.json of App has the dependencies of Figure 2.
  9. You can jump to step 24 to build the entire solution without trying each project individually Run dotnet restore, which calls into NuGet to restore the tree of dependencies.
  10. Check the project.lock.json files that contains a complete set of the graph of NuGet dependencies.
  11. Run dotnet build to compile source files.
  12. For applications run dotnet run that calls dotnet <assembly.dll> to run the target application (e.g. dotnet src/App/bin/Debug/netcoreapp1.0/App.dll). Note: If your App refers all project libraries, then you just need to build the App project because dotnet build ensures to build target projects.
  13. Setup unit tests project. CD into unit tests project directory (e.g. test/Fibonacci.Tests)
  14. Run dotnet new -t xunittest. Check the generated project.json, which includes the test runner and dependencies for xunit and dotnet-test-xunit Nuget libraries.
  15. Rename Tests.cs (e.g. FibSupplierTests.cs)
  16. Open tests C# file and rename the class according to the new file name (e.g. class FibSupplierTests{…)
  17. Rename the namespace according to the directory name (e.g. namespace Fibonacci.Tests{…)
  18. Add the dependency to Fibonacci project. Edit test/Fibonacci.Tests/project.json file and add the property "Fibonacci": {"target": "project"} to the dependencies object that will look like Figure 3
  19. You can jump to step 24 to build the entire solution without trying each unit tests project individually
  20. In the root directory create a global.json that contains the names of your src and test directories (e.g. { "projects": ["src", "test"] }) Run dotnet restore, which calls into NuGet to restore the tree of dependencies.
  21. Check the project.lock.json files that contains a complete set of the graph of NuGet dependencies.
  22. Run dotnet build to compile source files.
  23. Execute dotnet test to run the tests from the console. The xunit test runner has the program entry point to run your tests from the Console. dotnet test starts the test runner, and provides a command line argument to the testrunner indicating the assembly that contains your tests.
  24. Setup solution build. In the root directory create a global.json that contains the names of your src and test directories (e.g. { "projects": ["src", "test"] })
  25. Run dotnet restore, which calls into NuGet to restore the tree of dependencies (you can skip this step if you have already run it individually for each project previously).
  26. Check the project.lock.json files which appear on each project folder and contain a complete set of the graph of NuGet dependencies.
  27. Run code . on root directory that will open the Visual Studio Code for your solution
  28. Create the task runner file (.vscode/tasks.json) following one of the next options:
  • Option A: Click Yes if appears the Warning message Required assets to build and debug are missing from….

* Option B: Type `Ctr + Shif + B` and choose `Configure Task Runner` and then `.NET Core -- Executes .NET Core build command`

28. Open `.vscode/tasks.json` and add to `args` array the paths to your projects (e.g. `"${workspaceRoot}/src/App/project.json"` (use `\\` in Windows)). Because `App` depends of all other projects it will build the `Fibonacci` and `Primes` projects first. 29. If your solution includes an application (with property `"emitEntryPoint": true` in `buildOptions` of `project.json`) then run your application with `F5` or `Ctr + F5` (without debug). 30. Add a new entry in `launch.json` to run all your xunit tests. e.g. add [Figure 4](#fig-xunit-launch-cfg) item to `configurations` property of `launch.json`. 31. To run xunit tests select the `Debug` icon on left side bar and then `.Net Core Xunt tests` -- this is the name you gave in step 8.

32. You can run or debug each test individually by clicking on the corresponding option over each unit test method

Figure 1 - Folders structure

/<solution name>
|__global.json
|__/src
   |__/<library name>
      |__Source Files
      |__project.json
   |.../ Other libraries or applications
|__/test
   |__/<library name>.Test
      |__Test Files
      |__project.json

Figure 2 - Example of dependencies between projects

  "dependencies": {
    "Fibonacci": {"target": "project"},
    "Primes": {"target": "project"}
  }

Figure 3 - project.json of Fibonacci.Tests

"dependencies": {
  "System.Runtime.Serialization.Primitives": "4.1.1",
  "xunit": "2.1.0",
  "dotnet-test-xunit": "1.0.0-*",
  "Fibonacci": {"target": "project"}
}

Figure 4 - launch.json configurations item to run XUnit tests

"configurations": [
      {
            "name": ".NET Core Xunit tests",
            "type": "coreclr",
            "request": "launch",
            "preLaunchTask": "build",
            "program": " /usr/local/share/dotnet/dotnet ",
            "args": ["test"],
            "cwd": "${workspaceRoot}/test/Fibonacci.Tests",
            "externalConsole": false,
            "stopAtEntry": false,
            "internalConsoleOptions": "openOnSessionStart"
      },
      …
]

Installation

  1. .NET Core SDK
  2. Visual Studio Code (VS Code)
  3. Open VS Code and click on Extensions (last icon of left side bar or Ctrl + Shif + X)
  4. Install the extension “C# for Visual Studio Code (powered by OmniSharp)”
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment