一般测试示例

更新: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。

  • 如果传递的是单个数值参数,且所传递的数字为偶数,则程序将返回 0;如果所传递的数字为奇数,则程序返回 1。如果传递的是非数值参数,则程序返回 1。这将使得封装此程序的一般测试产生的结果为“未通过”。

  • 如果传递两个参数,且第二个参数表示与该程序位于同一目录的文件,则该程序返回 0,否则返回 1。

  • 所有其他情况下都会失败。

请参见

任务

演练:创建和运行一般测试