简体   繁体   中英

How to structure an asp.net 5 dnx project with unit tests in Visual Studio Code?

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.

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, eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM