ダウンロードとインストール
NUnit - Download から msi をダウンロード(現時点で最新は NUnit-2.6.0.12051.msi)。
インストールはおなじみ、ダブルクリックしてぽちぽちぽちっと
インストールはおなじみ、ダブルクリックしてぽちぽちぽちっと
Visual Studio に登録
[ツール]→外部ツールメニューにて下記のデータでエントリを追加。
- タイトル : NUnit
- コマンド : C:\Program Files\NUnit 2.6\bin\nunit.exe
- 引数 : /run $(ProjectDir)/$(ProjectFileName)
- 初期ディレクトリ : $(ProjectDir)
Debug ビルドを可能に
デフォルトだと強制的に Release ビルドになるので、Release と Debug を選択できるようにする。
- [ツール]→[設定]にて基本設定から上級者設定に変更。これでソリューション構成選択などのメニューが上部に現れる。
- [ツール]→[オプション]のダイアログにて、左下の「すべての設定を表示」にチェック
- [プロジェクトおよびソリューション]→[全般]メニューにて「ビルド構成の詳細を表示」にチェック
試してみる
サンプルプロジェクト(NUnitSample) を作成、Class1 を Sample クラスに変更して、SampleMethod メソッドを登録。
ソースコードを修正する。
using System;
namespace NUnitSample
{
public class Sample
{
public int SampleMethod(string text)
{
return 0;
}
}
}
テスト用プロジェクト(NUnitSampleTest)を追加し、参照設定にプロジェクトから NUnitSample プロジェクトを、.NET から nunit.framework を追加。
テストクラス SampleTest を作成し、テストメソッドを追加。
using System;
using NUnit.Framework;
using NUnitSample;
namespace NUnitSampleTest
{
public class SampleTest
{
private Sample sample = new Sample();
[Test(Description="引数 null 時のSampleMethodテスト")]
[ExpectedException(typeof(ArgumentNullException))]
public void SampleMethodWithNullArgument()
{
this.sample.SampleMethod(null);
}
[Test(Description="数値でない引数のテスト")]
[ExpectedException(typeof(ArgumentException))]
public void SampleMethodWithInvalidArgument()
{
this.sample.SampleMethod("hoge");
}
[Test(Description="正常な引数時のテスト")]
public void SamplemethodWithValidArgument()
{
int expected = 1234;
int actual = this.sample.SampleMethod("1234");
Assert.AreEqual(expected, actual);
}
}
}
[外部ツール]→[NUnit]で実行する。
当然失敗する。ソースコードを修正する。
using System;
namespace NUnitSample
{
public class Sample
{
public int SampleMethod(string text)
{
if (text == null)
{
throw new ArgumentNullException("text");
}
int result = 0;
if (Int32.TryParse(text, out result))
{
return result;
}
else
{
throw new ArgumentException("text");
}
}
}
}
実行して成功するのを確認する。


参考にさせて頂き、お陰様でNUnitを実行できるようになりました。ありがとうございます。
返信削除ビルド時の「ソリューションプラットフォーム」を「Any CPU」→「x86」に変更すると、NUnit起動時に、テストケースのDLLが見つからないためエラーとなりました。
[ツール]→外部ツールメニューで次のようにすると正常に起動できるようです。
引数 : /run /run $(TargetName)$(TargetExt)
初期ディレクトリ : $(BinDir)
既にご認識されていることかもしれませんが、念のためコメントさせて頂きました。