共用方式為


一般測試範例

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

  • 其他所有情況都會失敗。

請參閱

工作

逐步解說:建立和執行一般測試