10

I just created a new asp.net 5 project using the yeoman generator. Everything worked fine so far, I can edit and build the project with Visual Studio Code.

Now I want to add unit tests, but how are they structured in VS Code? In Visual Studio, the normal way is to add a new project that contains the tests, described in the xUnit.net documentation. However, in VS Code, I can't just add a project, can I? Where do I put the tests? The yeoman generator would create a new project, too, but that would mean I'd have to have a second instance of VS Code running and either a second git repo or a weird folder structure.

Kahbazi
  • 14,331
  • 3
  • 45
  • 76
looper
  • 1,929
  • 23
  • 42
  • So far I have solved the problem by switching to Visual Studio, but it seems weird that the default generator does not support this. – looper May 17 '16 at 16:24

1 Answers1

10

There is nothing that prevents you from having multiple projects within a single instance of Visual Studio Code. The default project structure for ASP.NET Core projects actually makes use of multiple folders. You can see that structure in action in all the ASP.NET Core projects on GitHub, e.g. the MVC one:

─ src
   ├─ Microsoft.AspNetCore.Mvc
   ├─ Microsoft.AspNetCore.Mvc.Abstractions
   ├─ Microsoft.AspNetCore.Mvc.Core
   └─ Microsoft.AspNetCore.Mvc.Razor
─ test
   ├─ Microsoft.AspNetCore.Mvc.Abstractions.Test
   ├─ Microsoft.AspNetCore.Mvc.Core.Test
   ├─ Microsoft.AspNetCore.Mvc.Razor.Test
   └─ Microsoft.AspNetCore.Mvc.Test

Each of those is a folder with a full independent project, with its own project.json that defines the project and sets up dependencies.

Unfortunately, the Yeoman integration in VS Code does not seem to allow scaffolding projects into subfolders. So what you need to do is create the src and test folder yourself, scaffold your projects individually and move them into the appropriate folder. For example, I started creating an “Empty Web Application” with the name Example, and then created a “Unit test project (xUnit.net)” with the name Example.Test. Then, I simply dragged Example into the src folder, and Example.Test into test.

So the root of the project, and as such the folder you open with VS Code, and where you would initialize your repository, is the directory where those src and test folders live in. For example, my project (set up with Git and vscode specific configs) would now look like this:

─ .git
─ .vscode
─ src
   └─ Example
─ test
   └─ Example.Test
─ global.json
─ README.md

The benefit of this structure is actually that it’s also compatible with what Visual Studio would do when you create a new solution.

poke
  • 369,085
  • 72
  • 557
  • 602
  • If I deploy my aspnet core application to Azure with CI through GitHub, how should I organise my folders? If I have src and test folders in one repo, how Azure understands from what folder to run deploy script? – Dmitry Gorshkov Jul 13 '16 at 21:01
  • @DmitryGorshkov Same problem here ... How to tell Azure to pick a project in src for deployment. Any idea? – Miguel Moura Feb 03 '17 at 20:49