如何在Travis CI中使用多个dotnet核心单元测试目标框架



我正在开发一个以.NetStandard为目标的api,并希望用不同版本的.NetCore.进行测试

我想针对当前支持的所有LTS版本的.Net Core运行单元测试,但我不知道如何针对当前安装的SDK构建单元测试。

目前支持的.Net Core版本是2.1和3.1。我可以使用<TargetFrameworks>来指定单元测试项目可以处理这两个目标,并且当我在开发机器上运行时,一切都很好。然而,与我的开发机器不同,Travis运行测试时只安装了一个SDK(这很好——我想确切地知道我在测试什么(。但是,单元测试项目希望同时提供这两个SDK。如果我只以2.1为目标,特拉维斯的3.1就失败了,如果我以3.1为目标,2.1就失败了。

那么,有没有一种方法可以用一个单元测试项目在Travis CI上测试两个LTS版本的.NetCore?

能够使其工作。

  1. 在单元测试项目中,使用TargetFrameworks而不是TargetFramework来针对多个框架。我认为您必须手动编辑csproj文件,我认为没有办法从Visual Studio UI设置它。<TargetFrameworks>netcoreapp2.1;netcoreapp3.1</TargetFrameworks>
  2. 在.travis.yml中,在dotnet:部分中包含一个框架,在before_install:部分中显式安装另一框架,然后在script:部分中显性调用每个框架的测试:
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

最新更新