Can't add xunit test project .NET Core

1.3k views Asked by At

I create a solution from classic .NET Framework Console App template. After that I go to CLI and create an xunit test project kicking off the following commands:

dotnet new -t xunittest

dotnet restore

dotnet test goes well, test passes.

I preserve the "src" -> "test" folders mapping as it is required with .NET Core as far as I know. After that, I try to add my testing project from within my VS 2015 Update 3 and I receive an error: "The DNX Project System package did not load correctly. The problem may have been caused by a configuration change or by the installation of another extension. You can get more information by examining the file 'path to a file on my local disk'.

I have uploaded the latest two events here.

Here is the content of .json of unit testing project:

    {
  "version": "1.0.0-*",
  "buildOptions": {
    "debugType": "portable"
  },
  "dependencies": {
    "System.Runtime.Serialization.Primitives": "4.1.1",
    "xunit": "2.1.0",
    "dotnet-test-xunit": "1.0.0-rc2-192208-24"
  },
  "testRunner": "xunit",
  "frameworks": {
    "netcoreapp1.0": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.0.1"
        }
      },
      "imports": [
        "dotnet5.4",
        "portable-net451+win8"
      ]
    }
  }
}

I have .NET Core runtime 1.1 installed. Three days ago I managed to import such a project with exactly the same steps. Today I can't do this.

1

There are 1 answers

0
Michal Ciechan On

If I am not mistaken, from my (limited) experience, I found that I could not use xUnit 2.1.0 to test my 1.0.1 .NET Core app and had to use 2.2.0 preview

I have blogged about ASP.NET Core – MVC xUnit Integration Testing.

In hindsight I had 2 problems:

  1. Versioning as mentioned above
  2. One of my projects was not in the same folder as its name.

TL;DR : Snipper from Blog post;

ASP.NET Core – MVC xUnit Integration Testing

Recently I wanted to test my new ASP.NET Core MVC app, so that I can be sure my startup.cs configuration is correct, and especially focusing on the JSON parsing of it.

I straight away run into a few basic stumbling blocks that might help a few people out there!

Test Discovery

The first stumbling block was trying to get the tests discovered at all.

I had to select a test runner and add this to the root of my project.json:

{
  "testRunner": "xunit",
  ....
}

I tried to initially use xunit and dotnet-test-xunit versions 2.1.0 which is the officially released version at the time of writing.

But as this does not support Microsoft.NETCore.App 1.0.1, after a bit off Bing’ing around, I figured out that I need to use "dotnet-test-xunit": "2.2.0-preview2-build1029"

Referencing the MVC Project

The second stumbling block was trying to simply reference the MVC project, in this case, “Taskily”. So I added the following line:

"dependencies": {
  .....
  "Taskily": "1.0.0*"
}

But this was giving me a “runtimes” is an unsupported framework error, which I couldn’t for the life of me figure out. In the end, it turned out to be that because the Taskily project (or at least it was called Taskily in the .sln) actually resided in a folder called WebApplication1 so my solution structure looked like this

root
|global.json
|---src
    |---WebApplication1  <-- renamed to Taskily in .sln
    |---Taskily.Tests    <-- new test project

The way I got this to compile is

  1. Close the .sln
  2. Rename WebApplicaiton1 folder to Taskily (same as project name in .sln)
  3. Re-Open .sln (Taskily won’t load as it is looking for src/WebApplication1/Taskily.xproj
  4. Remove the reference to the Taskily project from the .sln.
  5. Re-Add the Taskily project which should now be in src/Taskily/Taskily.xproj
  6. Compile

The error message was so not intuitive, and was bugging me for ages!