Search code examples
asp.net-mvcasp.net-core-mvcxunit.netdnx50

Why can't xUnit find my DNX tests?


I'm starting a new ASP.net vNext / MVC6 project and I want to use xUnit to test it.

I've followed the instructions on the xUnit web site for DNX projects.

When I try to run the tests in Visual Studio, either using the built-in runner or in ReSharper, I get this error message:

Test Runner error message

If I try to run the tests from the command line, I get this:

xUnit.net DNX Runner (32-bit DNX 4.5.1)
  Discovering: TA.Product.Tests
  Discovered:  TA.Product.Tests
  Starting:    TA.Product.Tests
    TA.Product.Tests.Class1.FactMethodName [FAIL]
      System.NullReferenceException : Object reference not set to an instance of an object.
      Stack Trace:
        D:\VS-Projects\TA.Product\src\TA.Product.Tests\Class1.cs(25,0): at TA.Product.Tests.Class1.FactMethodName()
  Finished:    TA.Product.Tests
=== TEST EXECUTION SUMMARY ===
   TA.Product.Tests  Total: 1, Errors: 0, Failed: 1, Skipped: 0, Time: 2.216s

Here's my project.json file:

{
  "version": "1.0.0-*",
  "description": "TA.Product.Tests Class Library",
  "authors": [ "Tim" ],
  "tags": [ "" ],
  "projectUrl": "",
  "licenseUrl": "",
  "dependencies": {
    "TA.Product": "1.0.0-*",
    "xunit": "2.1.0",
    "xunit.runner.dnx": "2.1.0-rc1-build204"
  },
  "commands": {
    "test": "xunit.runner.dnx"
  },
  "frameworks": {
    "dnx451": { },
    "dnxcore50": {
      "dependencies": {
        "Microsoft.CSharp": "4.0.1-beta-23516",
        "System.Collections": "4.0.11-beta-23516",
        "System.Linq": "4.0.1-beta-23516",
        "System.Runtime": "4.0.21-beta-23516",
        "System.Threading": "4.0.11-beta-23516"
      }
    }
  }
}

I found this question, and my version of the xUnit runner seems to match my DNX version, RC1-Update1.

I also found this question that says my tests must be public, and they are.

An ideas, please?


Solution

  • When you run dnx test from the command line and get these results:

    Finished:    TA.Product.Tests
    === TEST EXECUTION SUMMARY ===
       TA.Product.Tests  Total: 1, Errors: 0, Failed: 1, Skipped: 0, Time: 2.216s
    

    ...that is actually a successful test run. The only problem is that your 1 test is failing.

    I imagine that when it "crashes", you are trying to run using the ReSharper xUnit runner extension. That runner will currently (as of rc1) not work with DNX projects because R# looks for the compiled test assembly in a location where it does not exist. Try disabling resharper xUnit runner extension and running the tests from the default Visual Studio Test Explorer instead. It should work.

    Then, when you need to run tests, don's use any of the R# buttons or shortcuts. Use the built-in VS test buttons & commands instead (or just run dnx test from the command line, which should be way faster). Once you figure it out, you can re-enable the R# xunit runner for your other non-DNX projects.