Command line program test automation

[Software Testing Interview Crash Course] How to force yourself to finish the software testing eight-part essay tutorial in one week. After finishing the interview, you will be stable. You can also be a high-paying software testing engineer (automated testing)

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();

        }

    }

}

 The following are supporting learning materials. For those who are doing [software testing], it should be the most comprehensive and complete preparation warehouse. This warehouse has also accompanied me through the most difficult journey. I hope it can also help you!

Software testing interview applet

A software test question bank that has been used by millions of people! ! ! Who is who knows! ! ! The most comprehensive interview test mini program on the Internet, you can use your mobile phone to answer questions, take the subway, bus, and roll it up!

Covers the following interview question sections:

1. Basic theory of software testing, 2. web, app, interface function testing, 3. network, 4. database, 5. linux

6. Web, app, interface automation, 7. Performance testing, 8. Programming basics, 9. HR interview questions, 10. Open test questions, 11. Security testing, 12. Computer basics

 

How to obtain documents:

This document should be the most comprehensive and complete preparation warehouse for friends who want to engage in [software testing]. This warehouse has also accompanied me through the most difficult journey. I hope it can also help you!

All of the above can be shared. You only need to search the vx official account: Programmer Hugo to get it for free.

Guess you like

Origin blog.csdn.net/2301_79535544/article/details/133210617