Compartir a través de


Ejemplo de prueba genérica

Actualización: noviembre 2007

El ejemplo "EvenOdd" es un proyecto que puede compilar en un sencillo programa. Después puede ajustar este programa como una prueba genérica. Los archivos de este ejemplo se proporcionan para el tutorial siguiente: Tutorial: Crear y ejecutar una prueba genérica.

Código de ejemplo

El código de este ejemplo está disponible aquí:

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;
        }
    }
}

Trabajar con el código

Para trabajar con este código, primero tiene que crear un proyecto para él en Visual Studio. Siga los pasos de la sección "Preparar el tutorial" en Tutorial: Crear y ejecutar una prueba genérica.

Acerca del programa de ejemplo EvenOdd

El ejemplo EvenOdd es una aplicación de consola de Visual C#. Devuelve un valor de 1 ó 0, según el argumento que se le pase:

  • Si no pasa ningún argumento y el campo de segundos de la hora actual del sistema es par, el programa devuelve 0. Si no pasa ningún argumento y el valor del campo de segundos es impar, el programa devuelve 1.

  • Si pasa un único argumento numérico y el número que pasa es par, el programa devuelve 0. Si el número que pasa es impar, el programa devuelve 1. Si pasa un argumento no numérico, el programa devuelve 1. Esto hace que la prueba genérica que ajusta el programa genere un resultado de error.

  • Si se pasan dos argumentos y el segundo argumento representa un archivo que está en el mismo directorio que el programa, el programa devuelve 0; de lo contrario, devuelve 1.

  • Se producirá un error en todos los demás casos.

Vea también

Tareas

Tutorial: Crear y ejecutar una prueba genérica