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.
Run tests in Test Explorer If Test Explorer is not visible, choose Test on the Visual Studio menu, choose Windows, and then choose Test Explorer (or press Ctrl + E, T). As you run, write, and rerun your tests, the Test Explorer displays the results in a default grouping of Project, Namespace, and Class.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With