コード: CS0012
型 'Form' は、参照されていないアセンブリに定義されています。アセンブリ 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=(トークン文字列)' に参照を追加する必要があります。
<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>
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>
<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");
}
}
}
ターゲット フレームワーク | ターゲット フレームワーク モニカー (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 |