Running xUnit Tests for Multiple Frameworks

Georg Dangl by Georg Dangl in DotNet Wednesday, September 28, 2016

Wednesday, September 28, 2016

Posted in DotNet

Say you're building some class library with the intent to be used in multiple frameworks, like .Net Core and .Net (Full). Since NETStandard still hasn't really gotten off yet (and I think you should be waiting for 2.0 before you can use it as sole target), you may want to have your tests run for every supported framework.

With xUnit, that's actually quite easy! Just have your test projects project.json file target multiple frameworks like in the example below:

{
  "dependencies": {
    "xunit": "2.2.0-beta2-build3300",
    "dotnet-test-xunit": "2.2.0-preview2-build1029",
  },
  "testRunner": "xunit",
  "frameworks": {
    "netcoreapp1.0": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "version": "1.0.1",
          "type": "platform"
        }
      }
    },
    "net461": {}
  }
}

Then run dotnet test and you'll see that both framework targets are being compiled and run:

dotnet test

Compiling Dangl.Calculator for .NETStandard,Version=v1.4
...
Compiling Dangl.Calculator.Tests for .NETCoreApp,Version=v1.0
...
=== TEST EXECUTION SUMMARY ===
   Dangl.Calculator.Tests  Total: 168, Errors: 0, Failed: 0, Skipped: 0, Time: 0,521s
...
Compiling Dangl.Calculator.Tests for .NETFramework,Version=v4.6.1
...
=== TEST EXECUTION SUMMARY ===
   Dangl.Calculator.Tests  Total: 168, Errors: 0, Failed: 0, Skipped: 0, Time: 0,324s
SUMMARY: Total: 2 targets, Passed: 2, Failed: 0.

The dotnet test runner takes care of compiling the project both for .NETCoreApp,Version=v1.0 and .NETFramework,Version=v4.6.1 and runs the test suite for each. It also works with code coverage, so branches from conditional compilation are covered as well.

Happy Testing=)

 


Share this post


comments powered by Disqus

About me

Hi, my name's George! I love coding and blogging about it. I focus on all things around .Net, Web Development and DevOps.

DanglIT

Need a partner for DevOps, Web Services or Software Development?

Contact me at [email protected], +49 (173) 56 45 689 or visit my professional page!

Dangl.Blog();
// Just 💗 Coding

Social Links