我一直在研究在我们的 maven CI 环境中进行 JS 单元测试的最佳方法。我目前在我的 maven 项目中拼凑的内容如下:
- qunit 资源(JS/CSS 文件)
- qunit 使用 html 夹具测试 html 文件(每个被测文件一个)(如果需要)
- index html 文件,它将测试 html 文件作为超链接的有序列表引用
- PhantomJS 运行器文件,其中:
- 打开 index html 文件并解析出测试文件列表
- 打开每个测试文件
- 截取每个文件的 qunit 测试结果
- 如果有任何失败,退出状态为“1”
- 如果没有失败,以“0”状态退出
- 如果 phantomjs 未安装,shell 文件将以“0”退出,如果已安装,将调用 phantomjs 测试
更改 pom.xml 以在构建的测试阶段运行 phantomjs 测试:
<plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.1</version> <executions> <execution> <id>PhantomJS Unit Testing</id> <phase>test</phase> <goals> <goal>exec</goal> </goals> </execution> </executions> <configuration> <executable>${project.basedir}/src/main/webapp/unittest/phantomcheck</executable> <arguments> <argument>${project.basedir}/src/main/webapp/unittest/qunit-runner.js</argument> <argument>${project.basedir}/src/main/webapp/unittest/tests/index.html</argument> <argument>${project.build.directory}/surefire-reports</argument> </arguments> </configuration> </plugin> </plugins>
所以,这很好用。它在我们的开发和构建机器上构建期间运行 qunit 测试(只要安装了 PhantomJS)。测试在无头浏览器环境中运行,对 qunit 测试没有任何限制。我见过的其他 maven/qunit 集成由于在 Rhino 或其他 JS 环境中运行测试而失败,这些环境对我们可以编写的测试类型施加了限制。加上 phantomjs 使我们能够获得测试运行的屏幕截图,这有助于解决任何故障。
我的方法的缺点是需要在构建/开发机器上安装 PhantomJS。我不知道如何将 phantomJS 捆绑到依赖项中,这样开发人员就不必担心安装 PhantomJS。谁能给我推动这个方向?我该如何开始?