LP13
LP13

Reputation: 34109

Testing .Net Core Library with xUnit

I am creating a .Net Core Library that can be consumed by ASP.NET Core Application. By default Visual Studio create project.json as below

{
  "dependencies": {

    "NETStandard.Library": "1.6.0"
  },
  "frameworks": {
    "netstandard1.6": {
      "imports": "dnxcore50"
    }
  },
  "version": "1.0.0-*"
}

I think its recommended to use netstandard1.x for libraries that will be consumed by other applications. I am going to remove "imports": "dnxcore50" because that was for older .NET Core preview versions (Before .NET Core 1.0 RTM and .NET Core RC2 were released)

Now I wanted to unit test this library using xunit so followed the article Getting started with xUnit.net (.NET Core / ASP.NET Core) However xunit suggest to mark the library as .Net Core Application

You may have noticed that part of what we did here was mark your class library as a .NET Core Application (the netcoreapp1.0 framework). When using .NET CLI for testing, unit test projects are actually an application*, not a class library. If you forget to make this change, the compiler will tell you that dotnet-test-xunit is not compatible with your class library project. We tell Visual Studio to generate a class library, though, because it most closely matches our desired project structure.

  • The application's Main function is provided by the xUnit.net runner, not by you.

So I have updated the project.josn as suggested and I was able to run the unit test. However I have questions:

1> Is it recommended to change the target framework of the .Net Core library from netstandard1.6 to netcoreapp1.0 when that library can be consumed by other applications?

Upvotes: 4

Views: 1352

Answers (1)

Bruno Garcia
Bruno Garcia

Reputation: 6408

You'll have one project (and project.json) dedicated to your test project.

You need to mark the Test project as a .NET Core application since it will in fact be executed. No need to change the target project which is your Library to Core App.

Here you can see the project.json for a test project

And here the library project

Upvotes: 3

Related Questions