Export (0) Print
Expand All

Generic Test Sample

The "EvenOdd" sample is a project that you can build into a simple program. You can then wrap this program as a generic test. The files of this sample are provided for the following walkthrough: Walkthrough: Creating and Running a Generic Test.

The code for this sample is available here:

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

To work with this code, you first have to create a project for it in Visual Studio. Follow the steps in the "Prepare the Walkthrough" section in Walkthrough: Creating and Running a Generic Test.

The EvenOdd sample is a Visual C# console application. It returns a value of either 1 or 0, depending on the argument you pass it:

  • If you pass no argument and the seconds field of the current system time is even, the program returns 0. If you pass no argument and the value of the seconds field is odd, the program returns 1.

  • If you pass a single numeric argument, and the number you pass is even, the program returns 0. If the number you pass is odd, the program returns 1. If you pass a non-numeric argument, the program returns 1. This causes the generic test that wraps the program to produce a Failed result.

  • If you pass two arguments and the second argument represents a file that exists in the same directory as the program, the program returns 0; otherwise, the program returns 1.

  • All other cases will fail.

Community Additions

ADD
Show:
© 2014 Microsoft