Execute command line program test automation

There is a small tool that needs to be tested these days. It is a command line tool. This command line tool is somewhat similar to command line tools such as mdbg. That is, after the program is run, it waits for the command typed by the user on the command line, processes the command and displays the result. Then continue to wait for the user to enter a new command.

The original test cases were all executed manually, that is, the commands to be entered and the expected results were written in the test document. Of course this manual work needs to be automated.

But there is a problem with the automated testing tool, because this tool is not like other command line programs - it accepts some command line parameters, processes them and displays the results, and then exits. Instead, it continuously accepts new commands on the command line, processes and echoes them, and then accepts new commands from the user. Therefore, tests cannot be executed using ordinary batch processing.

To perform automated testing on this kind of program, the main thing is to take advantage of the fact that when each process starts, there are actually three files that are opened by default: standard input (Standard Input), standard output (Standard Output) and standard error output (Standard Error). For command line programs, standard input is the keyboard, and standard output is the computer screen. By default, standard error output and standard output use the same file (in modern operating systems, all devices are regarded as files, Not only Linux and Unix handle it this way, but Windows also handles it this way).

The standard input, output and error output of the process can actually be replaced before starting the process. This is a technology often used in inter-process communication - pipeline technology. That is, you can use pipe technology to connect the standard input of one process to the standard output of another process, so that after one process outputs some data, the other process automatically obtains the data. The following simple command is an application of pipes:

say | luck

The above command is to directly transfer the output data of the dir command to the input of sort, so that sort can perform corresponding sorting. The process is as shown in the following figure:

In Win32 programming, using pipes is a little more cumbersome, but in .NET, replacing and closing the standard input, output, and error output of a process are fairly simple tasks. Assume that the following program is the command line program we are about to test. Its job is very simple. It is to continuously echo the string entered by the user on the command line. Finally, when the user hits the space button, the program exits:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace ConsoleApplication

{

    public class Program

    {

        public static void Main()

        {

            string command = null;

            do

            {

                Console.Write(">");

                command = Console.ReadLine();

                Console.WriteLine();

                command = command.TrimEnd();

                Console.WriteLine("Hello: {0}", command);

            }

            while (!string.IsNullOrEmpty(command));

            Console.WriteLine("Quiting ...");

        }

    }

}

The following is an automated test program. Its job is to open the command line program under test, use pipe technology to pass commands to the standard input of the program under test, and then read the results from the standard output of the program under test:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Diagnostics;

namespace CmdTest

{

    class Program

    {

        static void Main(string[] args)

        {

            if (args.Length != 1)

            {

                Console.WriteLine("Usage: CmdTest <Application>");

                return;

            }

            var cmd = args[0];

            var startinfo = new ProcessStartInfo(cmd);

            startinfo.UseShellExecute = false;

            startinfo.RedirectStandardInput = true;

            startinfo.RedirectStandardOutput = true;

            startinfo.RedirectStandardError = true;

            var process = new Process();

            process.StartInfo = startinfo;

            process.Start();

            var names = new string[] {

                "Yimin",

                "Zhang San",

                "Li Si",

                "Wang Wu"

            };

            foreach (var name in names)

            {

                process.StandardInput.WriteLine(name);

                process.StandardInput.Flush();

                // Skip the echo characters

                process.StandardOutput.ReadLine();

                var result = process.StandardOutput.ReadLine();

                if (result != string.Format("Hello: {0}", name))

                    Console.WriteLine("Error!");

            }

            process.StandardInput.WriteLine();

            process.WaitForExit();

        }

    }

}

[Latest in 2023] Python automated testing, complete 60 practical projects in 7 days, and provide you with practical information throughout the process. [Automated testing/interface testing/performance testing/software testing]

Finally, I would like to thank everyone who reads my article carefully. Reciprocity is always necessary. Although it is not a very valuable thing, if you can use it, you can take it directly:

Insert image description here

This information should be the most comprehensive and complete preparation warehouse for [software testing] friends. This warehouse has also accompanied tens of thousands of test engineers through the most difficult journey. I hope it can also help you!   

Guess you like

Origin blog.csdn.net/qq_48811377/article/details/133172382