Generic Test Sample

"evenodd" amostra é um projeto que você pode construir em um programa simples. Em seguida, você pode dispor este programa como um teste genérico. Os arquivos neste exemplo são fornecidos para a instrução a seguir: Demonstra Passo a passo: Criando e executando um teste genérico.

Código de Exemplo

O código deste exemplo está disponível aqui:


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.  

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

Trabalhando com o código.

Para trabalhar com esse código, você primeiro deve criar um projeto para ele na Visual Studio. Siga as etapas de "preparar o passo a passo" seção Demonstra Passo a passo: Criando e executando um teste genérico.

Sobre o programa de exemplo de EvenOdd

O exemplo de EvenOdd é um aplicativo de console do Visual C#. Ele retorna um valor de 1 ou 0, dependendo do argumento que passá-lo:

  • Se você não passar nenhum argumento e o campo de segundos da hora atual do sistema for par, o programa retorna 0. Se você não passar nenhum argumento e o valor do campo segundos é ímpar, o programa retornará 1.

  • Se você passar um único argumento numérico e o número que você passar for par, que o programa retorna 0. Se o número passar for ímpar, o programa retorna 1. Se você passar um argumento de não-numéricos, o programa retornará 1. Isso faz com que o teste genérico que envolve o programa para produzir um resultado de falha.

  • Se você passar dois argumentos, e o segundo argumento representa um arquivo existente no mesmo diretório como o programa, o programa retorna 0; Caso contrário, o programa retornará 1.

  • Todos os outros casos falhará.

Consulte também

Tarefas

Demonstra Passo a passo: Criando e executando um teste genérico