3

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?

Community
  • 1
  • 1
Tim Long
  • 13,508
  • 19
  • 79
  • 147
  • 1
    So this is not a full answer, but I know that R#'s xunit test runner extension cannot run DNX tests - because it's looking in the wrong place for the dll. What does line #25 of `Class1` look like (in the `FactMethodName` method)? – danludwig Feb 23 '16 at 23:41
  • @danludwig I'm not exactly sure what the line is but your point makes perfect sense. So the R# runner can't find the tests because the compiler isn't emitting any output files, right? It's all done in-memory with aspnet5. So what I'm actually seeing is two different issues. And it looks like my code is causing the NullReferenceException. – Tim Long Feb 24 '16 at 00:37
  • 1
    Yep exactly. The second one where you run `dnx test` from the command line it isn't crashing, it's succeeding and telling you there is 1 failed test. You can set up your project to emit output files (assembly dll) but still R# will not look for it where it gets generated by default. It _should_ work with the default VS 2015 Test Explorer though. – danludwig Feb 24 '16 at 01:32
  • @danludwig Yep, that was it. D'oh! MSTest is able to run the tests OK, I'm starting to go off the R# unit test runner a bit... – Tim Long Feb 24 '16 at 01:32

1 Answers1

1

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.

danludwig
  • 46,965
  • 25
  • 159
  • 237