Lance Albertson
Lance Albertson

Reputation: 91

How do you use multiple dotnet core unit test target frameworks in Travis-CI?

I am developing an api targeting .NetStandard, and want to test it with different versions of .NetCore.

I would like to run my unit tests against all the currently supported LTS versions of .NetCore, however I cannot figure out how to build the unit tests against the currently installed SDK.

The currently supported versions of .NetCore are 2.1 and 3.1. I can use <TargetFrameworks> to specify that the unit test project can handle both those targets, and when I run on my dev machine everything works fine. However, unlike my dev machine, when Travis runs tests there is only one SDK installed (which is good - I want to know exactly what I'm testing against). The unit test project expects both SDKs to present, however. If I target only 2.1 it fails for 3.1 on Travis, and if I target 3.1 it fails for 2.1.

So, is there a way to test against both LTS versions of .NetCore on Travis-CI with a single unit test project?

Upvotes: 0

Views: 600

Answers (1)

Lance Albertson
Lance Albertson

Reputation: 91

Was able to get it working.

  1. In the unit test project use TargetFrameworks instead of TargetFramework to target multiple frameworks. I believe you have to edit the csproj file by hand, I don't think there's a way to set it from the Visual Studio UI. <TargetFrameworks>netcoreapp2.1;netcoreapp3.1</TargetFrameworks>
  2. In .travis.yml, include one framework in the dotnet: section, explicitly install the other framework in the before_install: section, then explicitly invoke the tests with each framework in the script: section:
dotnet: # Include one of the frameworks
  - 2.1.804 # EOL for 2.1: 2021.08.21
before_install:
  # explicitly install other targeted SDKs side by side
  - sudo apt-get install dotnet-sdk-3.1
script:
  # explicitly identify the framework when invoking the tests
  - dotnet test UnitTests/UnitTests.csproj -f netcoreapp2.1
  - dotnet test UnitTests/UnitTests.csproj -f netcoreapp3.1

Upvotes: 1

Related Questions