汎用テストのサンプル
更新 : 2007 年 11 月
"EvenOdd" サンプルは、ビルドして簡単なプログラムを作成できるプロジェクトです。このプログラムは、汎用テストとしてラップできます。このサンプルのファイルは、「チュートリアル : 汎用テストの作成と実行」のチュートリアルに含まれています。
サンプル コード
このサンプルのコードについては、以下を参照してください。
using System;
using System.Globalization;
using System.IO;
namespace EvenOdd
{
class TestSecondsOrNumbersOrFiles
{
/* Purpose: Wrap this sample app to create a generic test that passes or fails. Use it in
conjunction with the walkthrough topic that covers creating and running a generic test
in the testing tools section of the Visual Studio Team System documentation.
When you run the EvenOdd app, it exhibits the following Pass/Fail behavior:
* Pass zero arguments: EvenOdd randomly returns 1 (Fail) or 0 (Pass).
* Pass one (integer) argument: EvenOdd returns 1 if the argument is odd, 0 if even.
* Pass two arguments: EvenOdd ignores the first argument and uses only the second one, a string.
If the file named by that string has been deployed, EvenOdd returns 0 (Pass); otherwise 1 (Fail).
*/
[STAThread]
public static int Main(string[] args)
{
// If no argument was supplied, test whether the value of Second is even.
if (args.Length == 0)
return TestNumber(DateTime.Now.Second);
// If only a single numeric (integer) argument was supplied,
// test whether the argument is even.
if (args.Length == 1)
{
try
{
int num = Int32.Parse(args[0], CultureInfo.InvariantCulture);
return TestNumber(num);
}
// catch non-integer argument for args[0]
catch (FormatException)
{
Console.WriteLine("Please type an integer.");
return 1;
}
// catch too-large integer argument for args[0]
catch (OverflowException)
{
Console.WriteLine("Type an integer whose value is between {0} and {1}.", int.MinValue, int.MaxValue);
return 1;
}
}
// If two arguments are supplied, the test passes if the second
// argument is the name of a file that has been deployed.
if (args.Length == 2)
{
if (File.Exists(args[1]))
return 0;
}
// Test fails for all other cases
return 1;
}
public static int TestNumber(int arg)
{
return arg % 2;
}
}
}
コードの操作
このコードを操作するには、必要なプロジェクトを Visual Studio で作成する必要があります。「チュートリアル : 汎用テストの作成と実行」の「チュートリアルを準備する」の手順を実行します。
EvenOdd サンプル プログラムの概要
EvenOdd サンプル プログラムは、Visual C# コンソール アプリケーションです。渡される引数に基づいて、値 1 または 0 を返します。
引数が渡されないと、現在のシステム時刻の秒フィールドが偶数の場合は 0 を返します。奇数の場合は 1 を返します。
1 つの数値引数が渡されると、その数値が偶数の場合は 0 を返します。奇数の場合は 1 を返します。数値以外の引数が渡されると、1 を返します。これにより、プログラムをラップする汎用テストは失敗という結果を生成します。
2 つの引数が渡されると、2 つ目の引数がこのプログラムと同じディレクトリに存在するファイルを表す場合は 0 を返します。それ以外の場合は 1 を返します。
その他の場合は失敗します。