Gradle 脚本测试指南
测试 Gradle 脚本的最佳实践是什么?
我目前使用 对我的 ant 脚本进行单元测试antunit,但我希望迁移到 Gradle。我只能找到有关从 Gradle 或 Groovy 测试 Java 代码的文章,但没有找到有关测试我创建的 Gradle 任务或测试一般 Groovy 的文章。 Gradle 是否有等效的 Antunit?有人使用 BDD 框架(例如 cucumber)来测试 Gradle 脚本吗?
例如,我目前有以下 Ant Target
<target name="dist-bin" depends="build" description="creates a zip distribution of the current build">
<zip destfile="build/TIBant-bin.zip">
<zipfileset dir="src/ant" includes="**" />
<zipfileset dir="test" includes="**" prefix="test" />
<zipfileset dir="build" includes="TIBant.jar" />
<zipfileset dir="build" includes="TIBant-*.html" />
<zipfileset dir="build/examples/XMLtoProperties"
includes="XMLtoProperties.html"
prefix="examples/XMLtoProperties" />
<zipfileset dir="lib" includes="**" prefix="lib" />
<zipfileset dir="src/xslt" includes="**" excludes="test/**,userguide.xslt" prefix="lib/xslt" />
<zipfileset dir="." includes="copyright.html,LICENSE.txt" />
<zipfileset dir="examples"
includes="**"
excludes="**/build/**,**/config/default.properties"
prefix="examples" />
</zip>
</target>
正如您可以想象的那样,在重构项目时很容易破坏它,因此我有以下 antunit 测试来检查它。
<target name="test-dist-bin">
<delete file="build/TIBant-bin.zip" />
<au:assertFalse message="Bin dist still present">
<available file="build/TIBant-bin.zip" />
</au:assertFalse>
<antcall target="dist-bin" />
<au:assertTrue message="Bin dist not created">
<available file="build/TIBant-bin.zip" />
</au:assertTrue>
<delete dir="build/testBinDist" />
<au:assertFalse message="TestBinDist still present">
<available file="build/testBinDist" />
</au:assertFalse>
<mkdir dir="build/testBinDist" />
<unzip src="build/TIBant-bin.zip" dest="build/testBinDist" />
<au:assertFalse message="config dir present">
<available file="build/testBinDist/config/default.properties" />
</au:assertFalse>
<au:assertTrue message="Ant Macros missing">
<available file="build/testBinDist/tibant.xml" />
</au:assertTrue>
<au:assertTrue message="Engine Stopper Jar missing">
<available file="build/testBinDist/TIBant.jar" />
</au:assertTrue>
<au:assertTrue message="Ant-contrib-missing">
<available file="build/testBinDist/lib/ant-contrib-1.0b3.jar" />
</au:assertTrue>
<au:assertTrue message="ant-unit missing">
<available file="build/testBinDist/lib/ant-antunit-1.2.jar" />
</au:assertTrue>
<au:assertTrue message="Copyright missing">
<available file="build/testBinDist/copyright.html" />
</au:assertTrue>
<au:assertTrue message="License missing">
<available file="build/testBinDist/LICENSE.txt" />
</au:assertTrue>
<au:assertFalse message="Src present">
<available file="build/testBinDist/src/java/org/windyroad/tibant/EngineStopper.jar" />
</au:assertFalse>
<au:assertTrue message="example missing">
<available file="build/testBinDist/examples/SimpleProject/src/bw/example/Build/example.archive" />
</au:assertTrue>
<au:assertFalse message="example has build files">
<available file="build/testBinDist/examples/SimpleProject/build/*" />
</au:assertFalse>
<au:assertFalse message="example has default config file">
<available file="build/testBinDist/examples/SimpleProject/config/default.properties" />
</au:assertFalse>
<property name="doc.file"
location="build/testBinDist/TIBant-User-Guide.html" />
<au:assertTrue message="doc missing: ${doc.file}">
<available file="${doc.file}" />
</au:assertTrue>
<au:assertTrue message="xslt missing">
<available file="build/testBinDist/lib/xslt/configure-ear.xslt" />
</au:assertTrue>
<subant target="run-quick-tests">
<fileset dir="build/testBinDist" includes="build.xml" />
</subant>
</target>
由以下代码片段调用以生成漂亮的 xml 报告
<au:antunit failonerror="@{failonerror}">
<propertyset>
<propertyref name="config.filename" />
</propertyset>
<path>
<pathelement location="@{antunit}" />
</path>
<au:plainlistener logLevel="info" />
<au:xmllistener todir="build" loglevel="info" />
</au:antunit>
我了解如何将 dist-bin
迁移到 gradle,但我不确定迁移 test-dist- 的正确方法是什么bin
和 au:antunit 调用。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想tom的意思是一种测试他自己写的gradle任务的方法,对吗?如果您通过扩展 DefaultTask 编写了自定义 gradle 任务并将其放入项目的 buildSrc 文件夹中,则可以添加基于 junit/spock/whatever 的测试类来测试任务实现。 Gradles 自己的构建为此提供了一个很好的例子。看看
https://github.com/gradle/gradle/blob/master/buildSrc/src/test/groovy/org/gradle/build/docs/dsl/source/ExtractDslMetaDataTaskTest.groovy
这个是一个 spock 规范,用于测试专门为 griddles 自己构建编写的 ExtractDslMetaDataTask。 ExtractDslMetaDataTask 位于:
https://github.com/gradle/gradle/blob/master/buildSrc/src/main/groovy/org/gradle/build/docs/dsl/source/ExtractDslMetaDataTask.groovy
将断言添加到您的构建脚本“临时任务”中,就像上面的示例一样,您可以使用 groovy power 断言。
一个例子:
如果您的脚本中有一个像这样的非常简单的任务:
您可以修改任务本身以添加断言:
或者向脚本添加专用的测试任务
请记住,您可以在构建中使用groovy语言的全部功能脚本。
计划在 gradle 中提供一个集成测试工具包,以支持构建脚本作者测试其自定义构建逻辑。看一下:
http://forums.gradle.org/gradle/topics/testing_toolkit_for_custom_build_logic
I think what tom meant is a way to test his own written gradle tasks, right? If you have written a custom gradle task by extending DefaultTask and you put it in the buildSrc folder of your project, you can add a junit/spock/whatever based test class to test your task implementation. Gradles own build provides a good example for that. have a look at
https://github.com/gradle/gradle/blob/master/buildSrc/src/test/groovy/org/gradle/build/docs/dsl/source/ExtractDslMetaDataTaskTest.groovy
This is a spock specification, that tests the ExtractDslMetaDataTask which was specifically written for griddles own build. The ExtractDslMetaDataTask is located at:
https://github.com/gradle/gradle/blob/master/buildSrc/src/main/groovy/org/gradle/build/docs/dsl/source/ExtractDslMetaDataTask.groovy
To add assertions to your build script "adhoc tasks" like your example above you can use the groovy power assertion.
an example:
if you have a a very simple task like this in your script:
you can either modify the task itself to add an assertion:
or you add a dedicated test task to your script
remember that you can use the full power of the groovy language in your build scripts.
There are plans to have an integrated testing toolkit in gradle to support build script authors on testing their custom build logic. have a look at:
http://forums.gradle.org/gradle/topics/testing_toolkit_for_custom_build_logic
只要您应用插件
groovy
并且您的测试位于src/test/groovy
下,就不需要额外的配置来运行它们。例如,对于 Spock 的 BDD 测试也是如此。如果您想了解有关 Gradle 测试功能的更多信息,请参阅使用 Gradle 进行构建和测试< /a>.它涵盖使用 JUnit、TestNG、Spock、Geb 和 EasyB 进行测试。As long as you apply the plugin
groovy
and your tests sit undersrc/test/groovy
there's no additional configuration needed to run them. The same is true for BDD tests with Spock for example. If you want to read more about Gradle's testing capabilities check out the book Building and Testing with Gradle. It covers testing with JUnit, TestNG, Spock, Geb and EasyB.Gradle 3.x 测试工具包可用!
请在此处查看用户指南: https://docs.gradle.org/current/userguide /test_kit.html
然后可以通过断言以下内容(可能组合使用)来验证逻辑的正确性:
复制粘贴示例:
Gradle 3.x test Toolkit available!
Please, check out userguide here: https://docs.gradle.org/current/userguide/test_kit.html
The correctness of the logic can then be verified by asserting the following, potentially in combination:
copy-pasted example: