Condividi tramite


Esempio di test generico

L'esempio "EvenOdd" è un progetto da cui è possibile compilare un semplice programma e quindi eseguirne il wrapping come test generico. I file forniti in questo esempio sono relativi alla Procedura dettagliata: creazione ed esecuzione di un test generico.

Codice di esempio

Il codice per questo esempio viene riportato di seguito.


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

Utilizzo del codice

Per utilizzare questo codice, è necessario avere prima creato un progetto apposito in Visual Studio. Completare i passaggi nella sezione "Preparazione per la procedura dettagliata" in Procedura dettagliata: creazione ed esecuzione di un test generico.

Informazioni sul programma di esempio EvenOdd

L'esempio EvenOdd è un'applicazione console di Visual C#. Restituisce un valore 1 o 0, a seconda dell'argomento passato.

  • Se non si passano argomenti e il campo secondi dell'ora di sistema corrente è pari, il programma restituisce 0. Se non si passano argomenti e il valore del campo secondi è dispari, il programma restituisce 1.

  • Se si passa un singolo argomento numerico e il numero passato è pari, il programma restituisce 0. Se il numero passato è dispari, il programma restituisce 1. Se si passa un argomento non numerico, il programma restituisce 1. In questo caso, il test generico che esegue il wrapping del programma restituirà il risultato Non superato.

  • Se si passano due argomenti, il programma restituisce 0 se il secondo argomento rappresenta un file esistente che si trova nella stessa directory del programma; in caso contrario restituisce 1.

  • In tutti gli altri casi l'esito sarà negativo.

Vedere anche

Attività

Procedura dettagliata: creazione ed esecuzione di un test generico