这是另一个关于如何实现并行测试运行器(用于 Selenium 或任何其他类型的测试)的快速教程。
首先是代码:
1: #!C:/Perl64/bin/perl
2: use strict;
3: use warnings;
4: use File::Find::Rule;
5:
6: my $start_dir = shift || 'C:\Tests';
7: my $count = 0;
8:
9: my @subdirs = File::Find::Rule->directory->in($start_dir);
10:
11: my @files = File::Find::Rule->file()
12: ->name( '*.pl' ) # Only get perl files
13: ->in( $subdirs[0] );
14: foreach my $test (@files) {
15: system(1, $test);
16: print "Executing Test: " . $test . " Sequence #: " . $count . "\n";
17: print "My Father Is: " . $$ . "\n"; # process that started me
18: $count++;
19: }
20: exit(0);
这里的魔法由第 15 行执行。使用这种特殊形式的 perl 函数系统,我们可以强制它在继续之前不等待它启动的进程,从而有效地允许我们[几乎]同时产生多个测试。
来自 Perl 文档:
system(1, @args)
spawns an external process and immediately returns its process designator, without waiting for it to terminate.
上面的脚本将输出以下内容:
Executing Test: C:\Temp\test_template.pl Sequence #: 0
My Father Is: 8692
Executing Test: C:\Temp\test_template2.pl Sequence #: 1
My Father Is: 8692
Executing Test: C:\Temp\test_template3.pl Sequence #: 2
My Father Is: 8692
Executing Test: C:\Temp\test_template4.pl Sequence #: 3
My Father Is: 8692
下图显示了在我的调试器中,测试运行程序如何停止但 4 个衍生进程仍在运行(理论上正在运行自动化测试),正如我的 Windows 任务管理器中正在运行的 perl.exe 进程所指示的那样。
这就是使用 Perl 的系统函数同时启动多个自动化测试所需的全部内容。