使用批处理文件进行自动化测试:

时间:2020-03-06 14:42:59  来源:igfitidea点击:

我的测试套件具有以下布局:

TestSuite1.cmd:

  • 运行我的程序
  • 检查其返回结果
  • 如果返回结果不为0,则将错误转换为文本输出并中止脚本。如果成功,则写出成功。

在我的单个.cmd文件中,使用不同的输入调用程序约10次。

问题是我运行10次的程序每次都要花几个小时才能运行。

我有什么办法可以使程序的所有这10个运行并行化,同时仍然以某种方式检查返回结果并提供正确的输出文件,同时仍然使用单个.cmd文件和单个输出文件?

解决方案

尝试命令启动,它会产生一个新的命令提示符,我们可以发送任何我们希望它运行的命令。

我将使用它生成运行测试的批处理文件,然后使用>>将其追加到output.txt中,如下所示:

testthingie.cmd >> output.txt

批处理文件中的并行运行可以通过"开始"可执行文件/命令来完成。

假设它们不会通过写入相同文件等相互干扰:

test1.cmd

:: intercept sub-calls.
  if "%1"=="test2" then goto :test2

:: start sub-calls.
  start test1.cmd test2 1
  start test1.cmd test2 2
  start test1.cmd test2 3

:: wait for sub-calls to complete.
:loop1
  if not exist test2_1.flg goto :loop1
:loop2
  if not exist test2_2.flg goto :loop2
:loop3
  if not exist test2_3.flg goto :loop3

:: output results sequentially
  type test2_1.out >test1.out
    del /s test2_1.out
    del /s test2_1.flg
  type test2_2.out >test1.out
    del /s test2_2.out
    del /s test2_2.flg
  type test2_3.out >test1.out
    del /s test2_3.out
    del /s test2_3.flg

  goto :eof
:test2

:: Generate one output file
  echo %1 >test2_%1.out
  ping -n 31 127.0.0.1 >nul: 2>nul:

:: generate flag file to indicate finished
  echo x >test2_%1.flg

这将启动三个并发进程,每个进程都回显其序号,然后等待30秒。

全部带有一个cmd文件和(最终)一个输出文件。

视窗:

我们创建一个批处理文件,该批处理文件实际上调用:

start TestSuite1.cmd [TestParams1]
start TestSuite1.cmd [TestParams2]

等等,这实际上是在派生新的命令行,

如果应用程序可以处理并发用户(即使它是同一用户),并且TestSuite1.cmd能够处理参数,则这将起作用。

我们将需要在不同的计算机上使用不同的参数来启动脚本,因为当程序的多个实例一次运行时,使程序完成任务所需的时间(IO,CPU时间)变短。

唯一的例外:运行时间是由程序进入睡眠状态引起的。