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?

More than 1 year has passed since last update.

[ODC]VSCodeで作成したExternal Logicにユニットテストを追加する

Posted at

External LogicをVisual Studio Codeで作成するで、VSCodeを使ってExternal Logicを作成する手順をまとめた。
ここでは、上の記事で作成したソリューションにxUnit.netのユニットテストプロジェクトを作成する手順を確認する。ODCのExternal Logicとタイトルに書いてはいるが、External Logicと関係ない普通の.NETプロジェクトにテストプロジェクトを作成するときと変わらない。

環境情報

Windows 11 Home
Visual Studio Code (Version 1.85.2)
.NET SDK 6.0
xunit (Version 2.4.2)

参考ドキュメント

Unit testing C# in .NET Core using dotnet test and xUnit

コマンドサマリ

ソリューション=QiitaSamples
プロジェクト=ExternalLogicExample1.Tests
の場合の例なので、適宜読み替えて使う。

dotnet new xunit -o ExternalLogicExample1.Tests
dotnet sln add .\ExternalLogicExample1.Tests\ExternalLogicExample1.Tests.csproj
dotnet add .\ExternalLogicExample1.Tests\ExternalLogicExample1.Tests.csproj reference .\ExternalLogicExample1\ExternalLogicExample1.csproj

前提条件

  • External Logic用のプロジェクトが作成済み
  • プロジェクトの親フォルダにソリューションを作成済み

例(ソリューション:QiitaSamples、プロジェクト:ExternalLogicExample1)
image.png

操作後のフォルダ構成とプロジェクトファイル

image.png

テストプロジェクトを作成する

dotnet newでTEMPLATEにxunitを指定することで、xunit.netのプロジェクトが作成される。

c:\work\test\QiitaSamples>dotnet new xunit -o ExternalLogicExample1.Tests
テンプレート "xUnit Test Project" が正常に作成されました。

作成後の操作を処理しています...
c:\work\test\QiitaSamples\ExternalLogicExample1.Tests\ExternalLogicExample1.Tests.csproj を復元しています:
  復元対象のプロジェクトを決定しています...
  c:\work\test\QiitaSamples\ExternalLogicExample1.Tests\ExternalLogicExample1.Tests.csproj を復元しました (3.48 sec)。
正常に復元されました。

出来上がったプロジェクトの.csprojファイル

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

  <PropertyGroup>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>

    <IsPackable>false</IsPackable>
    <IsTestProject>true</IsTestProject>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.1" />
    <PackageReference Include="xunit" Version="2.4.2" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="coverlet.collector" Version="3.2.0">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
  </ItemGroup>

</Project>

ソリューションにテストプロジェクトを追加する

フォルダ内のソリューションに、今作成したテストプロジェクトのプロジェクトファイルを追加するコマンド。

c:\work\test\QiitaSamples>dotnet sln add .\ExternalLogicExample1.Tests\ExternalLogicExample1.Tests.csproj
プロジェクト `ExternalLogicExample1.Tests\ExternalLogicExample1.Tests.csproj` をソリューションに追加しました。

このコマンドによって、ソリューションファイル(拡張子.sln)に今作成したテストプロジェクトが追加される。
以下、該当部分抜粋

# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExternalLogicExample1", "ExternalLogicExample1\ExternalLogicExample1.csproj", "{8542FA66-36B7-40FA-B9A8-D3F21187946D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExternalLogicExample1.Tests", "ExternalLogicExample1.Tests\ExternalLogicExample1.Tests.csproj", "{63C8CA3E-3446-40D5-BC78-5C5EC742C131}"
EndProject

テストプロジェクトに、テスト対象プロジェクトへの参照を追加する

c:\work\test\QiitaSamples>dotnet add .\ExternalLogicExample1.Tests\ExternalLogicExample1.Tests.csproj reference .\ExternalLogicExample1\ExternalLogicExample1.csproj
参照 `..\ExternalLogicExample1\ExternalLogicExample1.csproj` がプロジェクトに追加されました。

テストプロジェクトの下の方に以下の様に参照が追加される

  <ItemGroup>
    <ProjectReference Include="..\ExternalLogicExample1\ExternalLogicExample1.csproj" />
  </ItemGroup>
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?