net自动化测试之道API测试-自动运行测试套件

Launching a Test Harness Automatically

自动运行测试套件

Problem

You want your test harness program tolaunch automatically.

问题

如何让我们的测试套件自动运行呢?

Design

Use the Windows Task Scheduler.

设计

利用windows的任务计划

Solution

You will often want to launch your testautomation automatically.For example you might

schedule a test automation harness to startat 2:00 A.M.so that it runs overnight,and the test

results are ready to review when you cometo work.The Windows Task Scheduler makes it easy

to schedule tasks in a Windows environment.You specify the test harness executable,the

scheduleof when you want the harness to run,and the security context under which youwant

theharness to run.

Ifyou have several lightweight test-automation harnesses that you want to run,youcan

createa.BAT file with commands to launch them.For example,

@echo off

echo Starting test automation sequence

echo.

C:\TestHarness1\bin\Debug\Run.exe

C:\TestHarness2\bin\Debug\Run.exe

C:\TestHarness3\bin\Debug\Run.exe

echo.

echo Test automation sequence complete

Ifyour harnesses explicitly log test case results to external files,then this isall you need.If

yourharnesses log test case results to the command shell,then you can easily savethem to

externalstorage using system redirection such as:

C:\TestHarness1\bin\Debug\Run.exe>C:\Results\Harness1Results.txt

C:\TestHarness2\bin\Debug\Run.exe>C:\Results\Harness2Results.txt

解决方案

通常我们想让测试套件自动运行。例如,我们可能想让我们的测试套件在凌晨2:00运行,这样套件可以在晚上运行,第二当我们来上班的时候就可以检查测试结果。在windows环境中,windows的任务计划可以使自动运行测试套件很容易实现。我们详细说明可执行的测试套件、运行计划时间和运行的安全策略。

如果我们有若干个轻量级自动化测试套件要自动运行,我们可以创建一个.BAT命令文件。例如:

@echo off

echo Starting test automation sequence

echo.

C:\TestHarness1\bin\Debug\Run.exe

C:\TestHarness2\bin\Debug\Run.exe

C:\TestHarness3\bin\Debug\Run.exe

echo.

echo Test automation sequence complete

如果我们将测试用例结果显式地记录在外部文件,那么这种方式正是我们需要的,如果测试用例结果记录在命令行,那通过系统重定向可以很容易地将测试结果存储到外部文件中,如:

C:\TestHarness1\bin\Debug\Run.exe>C:\Results\Harness1Results.txt

C:\TestHarness2\bin\Debug\Run.exe>C:\Results\Harness2Results.txt

 

Comments

For most lightweight test-automation situations,using.BATfiles to manage multiple test har-

nesses is simple and effective.Analternative is to write a C#master harness that coordinates

and calls the worker automationharnesses.You can write statements that call the worker har-

nesses by using the static Start()method inthe System.Diagnostics.Process namespace:

Console.WriteLine("Startingtest automation sequence\n");

Process.Start("C:\\TestHarness1\\bin\\Debug\\Run.exe");

Process.Start("C:\\TestHarness2\\bin\\Debug\\Run.exe");

Process.Start("C:\\TestHarness3\\bin\\Debug\\Run.exe");

Console.WriteLine("\nTest automation sequence complete\n");

and then schedulethis one master program using the Task Scheduler.An advantage of using a

.BATfile master solution is simplicity.An advantage of using a C#master solution isthat you

haveincreased power to do things like catch exceptions and add branching logic toexecute a

workerharness only if a preceding harness test results meet some condition.

Insteadof using the Windows Task Scheduler to automatically launch a test harness,you

canuse the old at command.Scheduling a test harness or master harness program torun

usingthe at command is more difficult than using the Windows Task Scheduler.Youshould

onlyuse at if your test system does not support the Task Scheduler or if you areintegrating a

newtest harness system into an existing system that uses the at command.

注解

对于大部分轻量级自动化场景,使用.BAT文件管理多个测试套件既简单又有效。编写一个C#主套件,在该套件中并列调用需要运行的测试套件同样可行。我们可以使用System.Diagnostics.Process命名空间下的Start()静态方法调用需要运行的测试套件:

Console.WriteLine("Starting testautomation sequence\n");

Process.Start("C:\\TestHarness1\\bin\\Debug\\Run.exe");

Process.Start("C:\\TestHarness2\\bin\\Debug\\Run.exe");

Process.Start("C:\\TestHarness3\\bin\\Debug\\Run.exe");

Console.WriteLine("\n Test automationsequence complete\n");

然后使用windows的任务计划计划该主程序的运行。使用.BAT的优点是简单。使用C#编写主程序实现的优点是当在前面调用的测试套件出现一些状况的时候,它更有能力处理如捕获异常、增加新的执行套件的逻辑分支等事件。

除了使用windows的任务计划让测试套件自动运行,我们也可以使用at命令来让测试套件或主测试套件程序自动运行。使用at命令要不使用windows任务计划要复杂。我们仅仅在测试系统不支持任务计划或者将新的测试套件系统整合到使用at命令的系统中的时候才用at命令。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值