0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

TUnit を netframework (net462 - net48)でビルドできないときに見る記事

0
Last updated at Posted at 2026-06-13

前提:

  • 対応しているのは net462 以上
    • ※ 内部で使っている ライブラリ 都合 net461 (netstandard2.0の.netframework 下限バージョン) はサポートしていない警告が出ており、対応に含めていません。
      • 具体的には次タイプの警告 が Microsoft.Bcl.AsyncInterfaces / Microsoft.Extensions.DependencyModel / System.Collections.Immutable / System.IO.Pipelines / System.Reflection.Metadata / System.Runtime.CompilerServices.Unsafe / System.Text.Encodings.Web / System.Text.Json / System.Threading.Channels / System.Threading.Tasks.Extensions に対して発生していた為、対応に含めていません。
        • Microsoft.Bcl.AsyncInterfaces 9.0.0 doesn't support net461 and has not been tested with it. Consider upgrading your TargetFramework to net462 or later. You may also set <SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings> in the project file to ignore this warning and attempt to run in this unsupported configuration at your own risk.
  • 私が確認しているのは
    • net462
    • net48
    • net481
    • net8.0
    • net9.0
    • net10.0

やり方

とりあえず csproj を次の様にすれば net462;net48;net481;net8.0;net9.0;net10.0 で共存することができることを確認したのでメモしておきます。

*.csproj
<Project Sdk="Microsoft.NET.Sdk">

	<PropertyGroup>
		<TargetFrameworks>net462;net48;net481;net8.0;net9.0;net10.0</TargetFrameworks>
		<ImplicitUsings>enable</ImplicitUsings>
		<Nullable>enable</Nullable>
		<LangVersion>14</LangVersion>	
	</PropertyGroup>

	<PropertyGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net462'))">
		<EnableTUnitPolyfills>false</EnableTUnitPolyfills>

		<NoWarn>$(NoWarn);CS0592;CS0436</NoWarn>
	</PropertyGroup>

	<ItemGroup>
		<PackageReference Include="TUnit" Version="1.54.0" />
	</ItemGroup>
	<ItemGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net462'))">
		<PackageReference Include="Polyfill" Version="10.8.1" PrivateAssets="all" />
	</ItemGroup>
</Project>

あと 内部的に ExcludeFromCodeCoverageAttribute を多用していることが確認されているので次も必要です。

Net481Fixes.cs
#if NET462_OR_GREATER
namespace System.Diagnostics.CodeAnalysis;

// .NET Framework 4.8.1 に元からある同名の属性を、
// アセンブリ(AttributeTargets.Assembly)にも付与できるように上書き定義する
[AttributeUsage(
	AttributeTargets.Assembly |
	AttributeTargets.Class |
	AttributeTargets.Struct |
	AttributeTargets.Constructor |
	AttributeTargets.Method |
	AttributeTargets.Property |
	AttributeTargets.Interface |
	AttributeTargets.Event,
	Inherited = false,
	AllowMultiple = false)]
internal sealed class ExcludeFromCodeCoverageAttribute : Attribute
{
}
#endif

実は ExcludeFromCodeCoverageAttribute は net40 から実装されているのですが、定義が違い、指定できる対象が限られている都合で この書き方をしています。 先述している csproj にあるように NoWarn で CS0592 と CS0436 を抑制する必要があります。(※ 私は Warn をエラーにする都合)

以上。

追伸

TUnit のテンプレートが -f / --framework 引数に対応していないことをissues として投げてきました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?