2
5

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 5 years have passed since last update.

dotnet コマンドでクラスライブラリとテストプロジェクトを作る

Last updated at Posted at 2018-11-27

チートシート的なメモ。

ソリューションフォルダの作成

mkdir sample
cd sample

git のリポジトリの初期化

git init

.gitignore ファイルは以下

obj/
bin/

ソリューションファイルの作成

フォルダ名と同じ名前の.slnファイルが作成される。名前を変えるなら-nオプション。

dotnet new sln

ライブラリプロジェクトの作成

dotnet new classlib -o Sample
dotnet sln add ./Sample/Sample.csproj

クラスの作成

ファイル名は.csならなんでも。

using System;

namespace Sample
{
    public class SampleClass
    {
        public bool IsEven(int num)
        {
            return num % 2 == 0;
        }
    }
}

テストプロジェクト(xUnit)の作成と参照の追加

dotnet new xunit -o Sample.Tests
dotnet sln add ./Sample.Tests/Sample.Tests.csproj
dotnet add ./Sample.Tests/Sample.Tests.csproj reference ./Sample/Sample.csproj

テストクラスの作成

ファイル名は.csならなんでも。

using System;
using Xunit;

namespace Sample.Tests
{
    public class SampleClass_IsEven
    {
        [Fact]
        public void ReturnFalseGivenValueOf1()
        {
            var _sample = new SampleClass();
            var result = _sample.IsEven(1);

            Assert.False(result, "1 should not be even");
        }
    }
}

テストの実行

パスは指定してもしなくてもよい。

dotnet test ./Sample.Tests/Sample.Tests.csproj

(おまけ)nuget パッケージの追加

パッケージのページの.NET CLI の方のコマンドを追加したいプロジェクトあるディレクトリでコピペ実行すれば OK。
コマンドは以下の通り。

dotnet add <PROJECT> package [options] <PACKAGE_NAME>
2
5
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
2
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?