更新:修改了构建脚本以下载 content_shell。还更新了跳转任务createUnitTestTask()中content_shell的路径。
我使用hop pub package 对无人机进行无头测试。相对简单的例子可以参考simplot库。但这些步骤基本上是将以下内容添加到您的pubspec.yaml 作为开发人员依赖项:
hop: '>=0.27.0'
unittest: '>=0.9.0 <0.10.0'
在您的项目根目录中创建一个工具目录并添加一个文件hop_runner.dart。我的看起来像这样:
library dumprendertree;
import 'package:hop/hop.dart';
import 'dart:io';
import 'dart:async';
main(List<String> args) {
addTask('test', createUnitTestTask());
runHop(args);
}
Task createUnitTestTask() {
return new Task((TaskContext tcontext) {
tcontext.info("Running Unit Tests....");
var result = Process.run('./content_shell',
['--dump-render-tree','test/simplot_tests.html'])
.then((ProcessResult process) {
tcontext.info(process.stdout);
});
return result;
});
}
您可以在测试目录中看到它在哪里调用我的simplot_tests.html 文件。
那么我的无人机脚本是:
$DART_SDK/../chromium/download_contentshell.sh
unzip content_shell-linux-x64-release.zip
mv drt*/* .
pub get
sudo start xvfb
dart --checked tool/hop_runner.dart test
simplot_tests.html 文件如下所示:
<!DOCTYPE html>
<html>
<head>
<title>Unit Tests for Simplot Library</title>
</head>
<body>
<script type="text/javascript" src="packages/unittest/test_controller.js"></script>
<script type="text/javascript" src="packages/browser/dart.js"></script>
<script type="application/dart" src="simplot_tests.dart"></script>
</body>
</html>
最后,dart 文件看起来像这样:
import 'package:simplot/simplot.dart';
import 'package:unittest/unittest.dart';
import 'package:unittest/html_enhanced_config.dart';
import 'dart:html';
import 'dart:math';
part 'tests/logarithmic_tests.dart';
part 'tests/time_stamp_tests.dart';
part 'tests/axis_configure_tests.dart';
part 'tests/create_single_plot.dart';
part 'tests/create_multiple_plots.dart';
part '../lib/src/axis_config.dart';
void main() {
print('Running unit tests for simplot library.');
useHtmlEnhancedConfiguration();
group('All Tests:', (){
test('test of logarithmic functions', () => logarithmicTests());
test('test of time stamp', () => timeStampTests());
test('test of axis configuration', () => axisConfigTests());
test('test of creating a single plot', () => createSinglePlot());
test('test of creating multiple plots', () => createMultiplePlots());
});
}
希望这能让你开始。