다음을 통해 공유


제네릭 테스트 샘플

업데이트: 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이 반환됩니다.

  • 다른 모든 경우는 실패합니다.

참고 항목

작업

연습: 제네릭 테스트 생성 및 실행