もう出尽くした感のあるネタだが、ネットで良い方法を見つけたのでメモしておく。
まず、テストプロジェクトの出力の種類を「Windowsアプリケーション」にする。
(通常のテストプロジェクトは、「クラスライブラリ」である)
そして、ダミーのエントリーポイントを追加する。
// 参照設定にnunit-gui-runner.dllを追加
public class NUnitTestRunner
{
[STAThread]
static void Main(string[] args)
{
NUnit.Gui.AppEntry.Main(
new string[] { Application.ExecutablePath, "/run"});
}
}
テストプロジェクトを、スタートアッププロジェクトに設定してRunすると、見慣れたテストランナーが起動する!
テストコード内でブレークポイントも使える。
GUIが目障りな場合は、出力ウィンドウに結果のみ表示させることもできる。
テストプロジェクトの出力の種類を「コンソールアプリケーション」にして、エントリーポイントを以下のようにする。
// 参照設定にnunit-console-runner.dllを追加
public class NUnitTestRunner
{
[STAThread]
static void Main(string[] args)
{
NUnit.ConsoleRunner.Runner.Main(
new string[] { Assembly.GetEntryAssembly().Location });
}
}
出力ウィンドウに結果が。
################################ UNIT TESTS ################################
Running tests in 'C:\Users\xxx\Desktop\NUnitExample\NUnitTest\bin\Debug\NUnitTest.EXE'...
スレッド 0x1580 はコード 259 (0x103) で終了しました。
############################################################################
############## S U C C E S S #################
############################################################################
Executed tests : 1
Ignored tests : 0
Failed tests : 0
Unhandled exceptions : 0
Total time : 0.0203104881145478 seconds
############################################################################
テストランナーのDLLは、NuGetでNUnit.Runnersでインストールできる。
ソリューションフォルダ以下、packages\NUnit.Runners.2.6.3\tools\lib にある。
<xml version="1.0" encoding="utf-8"?>
<packages>
<package id="NUnit" version="2.6.3" targetFramework="net45" />
<package id="NUnit.Runners" version="2.6.3" />
</packages>