2.3 用控制台应用程序创建测试软件
如果希望应用程序返回系统的某些信息,但不必坐下来输入数据,并相应应用程序,让软件在无人操作的情况下自动执行操作,可以不使用windows窗体应用程序,而是用控制台应用程序。
控制台应用程序可以访问三种基本数据流:
标准输入:进入程序的数据
标准输出:程序生成的数据
标准错误:表示一种特殊的数据:错误信息。
例:
新建一个控制台应用程序,检查文件的存在性
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine();
Console.WriteLine(Enter the file to find or enter 'Q' to Quit);
string strInput = ;
while (strInput != Q)
{
strInput = Console.ReadLine();
if (File.Exists(strInput))
Console.WriteLine(File ExistsTest passed);
else
Console.WriteLine(File doesn't ExistsTest failure);
Console.WriteLine(Enter the file to find or enter 'Q' to Quit);
}}}}
这些代码类似于2.2 中为windows窗体编写的代码,但有两个主要区别:第一,用console类读写消息。第二个区别是必须使用循环结构,让用户继续查找文件。
专业的测试人员可能比较挑剔,会在这里多考虑一下,更多地了解可以在控制台应用程序中提供什么,以满足用户的需求。