6

I'm building an ASP.NET Core Web Application (.Net Framework) and am having a hard time figuring out how to hook unit tests up to it. I am targeting the .net framework version 4.6.1

If I create a regular "Class Library" project targeting 4.6.1, as I would for previous version of MVC, it lets me add references to my MVC project (which is part of the same solution) but any namespaces I add through a using statement report an error that I might be missing a reference or using statement. If I double click on the reference under the "References" section in the solution explorer it tells me that the project can't be found or hasn't been build yet.

I tried creating a "Class Library (.NET Core)" but that complains since I'm targeting .Net Framework and not .NET Core. I edited the class libaries Project.json to have it target the .net framework and that lets me add my references and doesn't complain when I the namespaces in a using statement but none of my tests are discovered by the test runner. I've tried both XUnit and NUnit and they both behave the same.

Is it possible to unit test an ASP.Net Core Web Application targeting the .Net Framework 4.6.1 or do I need to commit to the .NET Core?

Edit to add my test class Here is my test class stripped down to the bare minimum. TestBank.Services is the class I want to test.

using System;
using TestBank.Services;
using Xunit;

namespace TestBankUnitTests
    {    
    public class Class1
    {        
        [Fact]
        public void TestA()
        {
            Assert.True(false);
        }
    }
}

and here is my project.json

{
    "version": "1.0.0-*",

    "dependencies": {
        "xunit": "2.1.0",
        "dotnet-test-xunit": "1.0.0-rc2-build10025",
        "TestBank": "1.0.0-*"
    },

    "frameworks": {
        "net461": {
        }
    }
}
6
  • Unit test discovery is pretty finicky. I found that I had to have a solution containing only projects that were .net core projects or it wouldn't find the tests. For example, I had a .core core mvc app and a .core library with my tests, and a third project that was just a class library (not .net core). VS was for some reason unable to discover tests until I removed that non-core library from the solution.
    – steve v
    Commented Aug 29, 2016 at 16:13
  • Right now my solution only has the MVC project and the .net core class library (modified to target .Net Framework 4.6.1) and it isn't discovering the tests. This makes me wonder if the test discovery only works for true .net core and not .net core targeting the .net framework. Commented Aug 29, 2016 at 17:43
  • Shouldn't be an issue. That's what I do in my solutions as well. Can you post your test classes? They are public classes with public methods marked with Fact or equivalent right?
    – steve v
    Commented Aug 29, 2016 at 17:46
  • Yes, both class and methods are public. I updated the original question with my minimalist test class. Commented Aug 29, 2016 at 18:41
  • Can you post your project.json as well? Do you have the xunit test runner in there when using xunit? Does it work from the commandline (dotnet test)?
    – steve v
    Commented Aug 29, 2016 at 18:48

3 Answers 3

8

Your project.json needs a testRunner setting. Per the project.json documentation, the testRunner setting not only specifies which test framework to use, but also marks the project as a test project.

Try adding it and see if it finds your tests (verified locally that it will not find tests without this setting in the file)

{
    "version": "1.0.0-*",

    "dependencies": {
        "xunit": "2.1.0",
        "dotnet-test-xunit": "1.0.0-rc2-build10025",
        "TestBank": "1.0.0-*"
    },

    "frameworks": {
        "net461": {
        }
    },

   "testRunner": "xunit"
}
1
  • That did the trick. Now I feel silly for having missed that initially! Thanks! Commented Aug 29, 2016 at 20:07
0

I was having a similar issue and found a solution. I am posting it here in case it helps someone. What I have learned is that using an xproj library targeting net461 to test an Asp.Net Core Project (.Net Framework) project targeting net461 and a Windows class library targeting framework 4.61 works but the setup seems to be very finicky and fragile. The key insight for me came from this thread https://github.com/aspnet/Tooling/issues/245 where @BradRem indicated that the folder structure of the projects seemed to the source of issues.

Initially I tried unsuccessfully to use this folder structure:

src
____Asp.Net Core Project (.Net Framework) project targeting Net461
____Windows class library targeting framework 4.61

test
____Core Library used to run xUnit Tests

But when I tried to run the tests using this folder structure it produced the exception that starts off like this:

Unable to start C:\Program Files\dotnet\dotnet.exe
dotnet-test Error: 0 : [ReportingChannel]: Waiting for message failed System.IO.IOException: Unable to read data from the transport connection: An established connection was aborted by the software in your host machine. ---> System.Net.Sockets.SocketException: An established connection was aborted by the software in your host machine
   at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
   at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   --- End of inner exception stack trace ---
   at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   at System.IO.Stream.ReadByte()
   at System.IO.BinaryReader.ReadByte()
   at System.IO.BinaryReader.Read7BitEncodedInt()
   at System.IO.BinaryReader.ReadString()
   at Microsoft.DotNet.Tools.Test.ReportingChannel.ReadMessages()

But when I changed to the following folder structure I was able to get it to work:

src
____Asp.Net Core Project (.Net Framework) project targeting Net461
____Windows class library targeting framework 4.61
____Core Library used to run xUnit Tests

So the key was putting the folder that housed the testing class library in the src folder where the other projects folders were.

That said, the other thing that seemed to make a big difference was to add the references to the other two projects to the Core Library test project at the same time rather than one at a time.

0

I have found something that works for me.
Inside my test project, my project.json file looks like this:

{
  "version": "1.0.0-*",
  "testRunner": "xunit",

  "dependencies": {
    "xunit": "2.2.0-beta2-build3300",
    "dotnet-test-xunit": "2.2.0-preview2-build1029",   
    "Ng2a.WebApi": "1.0.0-*"
  },

  "frameworks": {
    "net452": {
    }

  }
}

'Ng2a.WebApi' is my web api core project. It targets only net452. The Ng2a.WebApi project.json file looks like this:

"frameworks": {
"net452": {}

},

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.