Flutter HTML内容构建插件test_html_builder的使用
Flutter HTML内容构建插件test_html_builder的使用
test_html_builder
是一个Dart构建器,它使用模板生成HTML文件用于Dart测试。这对于需要自定义HTML的项目特别有用。而不是为每个需要自定义HTML的测试文件重复创建相同的文件,这个构建器可以将模板应用到任意数量的测试文件上。
使用
首先,将 test_html_builder
添加为开发依赖项:
dart pub add --dev test_html_builder
接下来,在 test/templates/
目录下创建所需的HTML测试模板。例如:
<!-- test/templates/example_template.html -->
<html>
<head>
<!-- "testName" 占位符将被每个测试的唯一名称替换。 -->
<title>{{testName}} 测试</title>
<!-- 加载测试所需的自定义资源。 -->
<script src="custom.js"></script>
<!-- 每个模板都必须包含此占位符。 -->
{{testScript}}
<!-- 它将被构建器替换为所需的 <link> 标签:
<link rel="x-dart-test" href="...">
-->
<!-- 每个模板都必须包括测试运行脚本。 -->
<script src="packages/test/dart.js"></script>
</head>
</html>
然后,告诉构建器应将哪些模板应用于哪些文件。例如:
targets:
$default:
builders:
test_html_builder:
options:
templates:
"test/react_with_styles_template.html":
- "test/components/styled/**_test.dart"
"test/react_template.html":
- "test/components/**_test.dart"
为了更好地说明如何工作,考虑以下示例测试目录结构:
test/
foo_test.dart
react_template.html
react_with_styles_template.html
components/
bar_test.dart
styled/
baz_test.dart
通过运行 dart run build_runner test
并使用上述配置,将会生成以下隐藏输出文件:
test/components/bar_test.html
(来自react_template.html
)test/components/styled/baz_test.html
(来自react_with_styles_template.html
)
注意事项
-
如果多个模板定义的通配符有重叠,构建器将选择第一个匹配的模板。
-
如果没有任何模板与给定的测试文件匹配,则不会生成任何HTML文件。
-
仍然支持为单个测试文件创建独立的自定义HTML文件,但它们必须使用
.custom.html
扩展名:test/example_test.dart
test/example_test.custom.html
聚合浏览器测试
有些项目可能希望在更接近生产环境的情况下获得额外的保证。这通常通过在发布模式下运行浏览器测试来实现(使用 -r|--release
),这将使用dart2js进行编译。然而,在大型项目中,由于dart2js不是模块化编译器,这可能会非常慢,因为它会将每个浏览器测试视为完整的程序,并且需要编译其导入的所有文件。
一种解决方法是创建一个“聚合”测试,导入并运行所有浏览器测试。这样,dart2js只需要编译一个东西,但所有的浏览器测试仍然可以运行。这种方法有一些注意事项:
- 聚合测试只能有一个HTML文件关联。如果你的项目使用了多个HTML模板,你需要为每个模板创建一个聚合测试。
- 需要设置一些配置并自定义运行测试的命令,以确保只编译和运行聚合测试;否则,你可能会花费大量时间不必要地编译所有单独的测试。
该包提供的构建器可以自动化所有这些操作!首先,通过设置 browser_aggregation: true
启用此功能:
targets:
$default:
builders:
test_html_builder:
options:
templates:
"test/react_with_styles_template.html":
- "test/components/styled/**_test.dart"
"test/react_template.html":
- "test/components/**_test.dart"
browser_aggregation: true
一旦启用,构建器将为每个模板生成一个聚合测试,该测试导入并运行使用该模板的每个测试。它还将为未匹配任何模板的浏览器测试生成默认聚合测试。最后,它生成一个 dart_test.browser_aggregate.yaml
文件,可以包含在项目的 dart_test.yaml
中,以便使用此测试参数轻松选择聚合测试:--preset=browser-aggregate
。
要运行这些测试,可以使用该包提供的可执行文件:
dart run test_html_builder:browser_aggregate_tests [--release]
或者,如果你有自己的测试运行器,可以集成此功能,可以运行:
dart run test_html_builder:browser_aggregate_tests --mode=args [--release]
这将打印必要的 build_runner
和 test
参数,格式如下:
<build args> -- <test args>
# 例如:
--release --build-filter=test/templates/react_template.browser_aggregate_test.** -- --preset=browser-aggregate
你可以解析这些参数或将它们直接传递到命令中以运行测试,如下所示:
dart run build_runner test $(dart run test_html_builder:browser_aggregate_tests --mode=args [--release])
随机化聚合文件
使用 dart test
,可以通过使用 --test-randomize-ordering-seed
标志来随机化测试文件的运行顺序。然而,当使用此包的浏览器聚合功能时,这种方法不起作用,因为只会有一个测试文件对应每个模板。换句话说,大多数测试将继续按相同顺序运行。
为此,test_html_builder
还支持在 build.yaml
中设置的 randomize_ordering_seed
选项。它的行为与 --test-randomize-ordering-seed
标志基本相同,这意味着你可以将其值设置为特定种子或 "random"
,如果你希望种子被随机选择。
例如:
targets:
$default:
builders:
test_html_builder:
options:
...
browser_aggregation: true
randomize_ordering_seed: random
更多关于Flutter HTML内容构建插件test_html_builder的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html