Search code examples
visual-studiovisual-studio-2015.net-coreprojects-and-solutionssolution

Trying to understand the default behaviour of "projects" in global.json in VS


After creating a new project (example is in .net core) the global.json of the solution looks like that

{
  "projects": [ "src", "test" ],
  "sdk": {
    "version": "1.0.0-preview2-003131"
  }
}

And the new project created inside src folder and there is a physical folder exists inside the solution folder. Although the global.json appears inside Solution Items solution folder in the visual studio, no physical folder named solution items exists.

Now if i add a unittest project it creates just inside the solution folder, it does not create any Solution Folder or Physical Folder named test.

Then i created a test folder it does not appear on VS and creating a new test project does not create within the test physical folder.

If i add an another new project the new project created inside src folder. If I add any item (e.g. XMLFile) it add it within the Solution Items solution folder. But if you add test project if just appears underneath solution folder

Now my project

enter image description here

and the physical folder looks like below

enter image description here

However, my question is about the discrepancies of the behaviours.

  1. Why the behaviour is different for test projects
  2. Why is the test added on global.json automatically?

I have also noticed that if the test project is not inside the test folder, sometimes, it does not find xunit or nunit tests, but it finds the MSTest tests. So, my point is how the test in global.json helping us?


Solution

  • A few things going on here.

    The Projects JSON Element

    Think of this as how .sln files used to work. It tells Visual Studio where to look for projects to load up. These can actually be full paths too (e.x. C:\Projects\SomeOtherProject). You should check out the official documentation here that explains a bit more : https://learn.microsoft.com/hu-hu/dotnet/articles/core/tools/global-json

    It should also be noted that in the upcoming release of .net core (Currently labelled Preview 4), we are back to .sln files and so the global.json projects element has been removed (See here : https://learn.microsoft.com/hu-hu/dotnet/articles/core/preview3/tools/global-json)

    Where do your new projects get placed?

    Honestly, this is just convention. When you create the project in Visual Studio, you are asked where you want to place the project so it's up to you... If you want everything in one solution folder then that's fine. It just seems to be a convention now that you have a "src" folder for your code, readme.md in the root etc.

    Obviously if you are creating the projects using dotnet new or yeoman, you have complete control over what's going on. Your code does not need to be in a "src" folder.

    Testing in .net core

    I can see your going down the wrong path with testing. Take a quick read of this article first : http://dotnetcoretutorials.com/2017/01/30/running-unit-tests-dotnet-test/

    So a couple of things.

    1. .net core tests are now "console" apps. Or are atleast created like them in VS.

    2. You need to install specific test runner packages to get run tests (Test runners exist for MSTest, XUnit and NUnit).

    3. You can run tests from the command line using "dotnet test".