.NET Framework の WIndows Form アプリケーションで xUnit テストプロジェクトを利用する方法を紹介します。
xUnitテストプロジェクトでは、.NET 5.0 または、.NET Core をターゲットフレームワークに設定するため、.NET Framework のアプリケーションのテストはできないことになっています。
.NET Framework のアプリケーションをテストする場合は、NUnitまたは、MSTestを利用するのが一般的です。
とはいえ、xUnitが最新のテストフレームワークであり、.NET 5.0 でのテストプロジェクトはxUnitを利用することが多いため、
できれば、.NET Framewwork でも xUnitのテストプロジェクトを利用したいです。
この記事では、.NET Framework のWindows Formアプリケーションで xUnitのテストを利用する方法を紹介します。
.NET Framework のWindows Formアプリケーションを作成します。
合わせて、xUnitのテストプロジェクトも作成します。xUnitのテストプロジェクトの作成はこちらの記事を参照してください。
xUnitテストプロジェクトのプロジェクトファイルを編集します。ソリューション エクスプローラーウィンドウで、テストプロジェクトのノードを選択し、右クリックします。
ポップアップメニューが表示されますので、メニューの[プロジェクト ファイルの編集]の項目をクリックします。
プロジェクトファイルのXMLコードが表示されます。
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\xUnitTestSimpleDotNetFramework\xUnitTestSimpleDotNetFramework.csproj" />
</ItemGroup>
</Project>
<TargetFramework>net5.0</TargetFramework>
のターゲットフレームワークの設定を.NET Frameworkのターゲットフレームワークに変更します。
テストされる側のWindows Formアプリケーションが .NET Framework 4.8 の場合は net48
に変更します。
なお、ターゲットフレームワークのバージョンに対応する モニカー はこのページの末尾を参照してください。
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net48</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\xUnitTestSimpleDotNetFramework\xUnitTestSimpleDotNetFramework.csproj" />
</ItemGroup>
</Project>
xUnitのテストプロジェクトの[依存関係]ノードを選択して、プロジェクトの参照でテストされる.NET FrameworkのWindows Formアプリケーションのプロジェクトを追加します。
アラートが出ずに追加できます。
しかし、この状態でテストプロジェクトをビルドすると、以下のエラーメッセージが表示されます。
これは、テストプロジェクトに System.Windows.Forms
のアセンブリ参照が無いために発生するエラーです。プロジェクトファイルを編集して、アセンブリの参照を追加します。
プロジェクトファイルを編集して、PackageReference
タグを追加し System.Windows.Forms
の参照を追加します。
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net48</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="System.Windows.Forms" Version="4.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\xUnitTestSimpleDotNetFramework\xUnitTestSimpleDotNetFramework.csproj" />
</ItemGroup>
</Project>
上記の変更でもエラーが発生する場合には、System アセンブリの参照も追加することで、エラーを回避できる場合があります。
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net48</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="System" Version="4.1.311.2" />
<PackageReference Include="System.Windows.Forms" Version="4.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\xUnitTestSimpleDotNetFramework\xUnitTestSimpleDotNetFramework.csproj" />
</ItemGroup>
</Project>
下記のコードを記述し、プログラムとテストクラスを実装します。
コードの実装についてはこちらの記事を参照して下さい。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace xUnitTestSimpleDotNetFramework
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int value1 = Convert.ToInt32(textBox1.Text);
int value2 = Convert.ToInt32(textBox2.Text);
int value3 = calc(value1, value2);
textBox3.Text = value3.ToString();
}
public int calc(int a, int b)
{
return a + b;
}
}
}
using System;
using Xunit;
using xUnitTestSimpleDotNetFramework;
namespace xUnitTestSimpleDotNetFrameworkTest
{
public class UnitTest1
{
[Fact]
public void Test1()
{
FormMain f = new FormMain();
int result = f.calc(1, 2);
Assert.True(result == 3, "NG");
}
}
}
テストを実行します。.NET Frameworkのアプリケーションに対してもテストが実行できました。
ターゲット フレームワーク | ターゲット フレームワーク モニカー (TFM) |
---|---|
.NET 6 | net6.0 |
.NET 5 | net5.0 |
.NET Core 3.1 | netcoreapp3.1 |
.NET Core 3.0 | netcoreapp3. |
.NET Core 2.2 | netcoreapp2.2 |
.NET Core 2.1 | netcoreapp2.1 |
.NET Core 2.0 | netcoreapp2.0 |
.NET Core 1.1 | netcoreapp1.1 |
.NET Core 1.0 | netcoreapp1.0 |
.NET Framework 4.8 | net48 |
.NET Framework 4.7.2 | net472 |
.NET Framework 4.7.1 | net471 |
.NET Framework 4.7 | net47 |
.NET Framework 4.6.2 | net462 |
.NET Framework 4.6.1 | net461 |
.NET Framework 4.6 | net46 |
.NET Framework 4.5.2 | net452 |
.NET Framework 4.5.1 | net451 |
.NET Framework 4.5 | net45 |
.NET Framework 4.0.3 | net403 |
.NET Framework 4.0 | net40 |
.NET Framework 3.5 | net35 |
.NET Framework 2.0 | net20 |
.NET Framework 1.1 | net11 |
.NET Standard 2.1 | netstandard2.1 |
.NET Standard 2.0 | netstandard2. |
.NET Standard 1.6 | netstandard1.6 |
.NET Standard 1.5 | netstandard1.5 |
.NET Standard 1.4 | netstandard1.4 |
.NET Standard 1.3 | netstandard1.3 |
.NET Standard 1.2 | netstandard1.2 |
.NET Standard 1.1 | netstandard1.1 |
.NET Standard 1.0 | netstandard1.0 |
.NET Core 4.5 | netcore |
.NET Core 4.5(windows 8) | netcore45 |
.NET Core 4.5.1(windows 8.1) | netcore45 |
.NET Micro Framework | netmf |
Silverlight 4 | sl4 |
Silverlight 5 | sl5 |
Windows Phone 8.1 | wpa81 |
Windows Phone 8.1 | wp81 |
Windows Phone 8.0 | wp8 |
Windows Phone 7.5 | wp75 |
Windows Phone 7.0 | wp7 |
Windows Phone | wp |