准备工作
用Ruby进行测试,我们需要JtestR这个专门为简化Java测试而准备的Ruby测试工具,当前的最新版本是0.3.1。如果你使用maven,在pom.xml中加入:
<plugins> ... <plugin> <groupId>org.jtestr</groupId> <artifactId>jtestr</artifactId> <version>0.3.1</version> <configuration> <!-- Ruby测试文件所在目录 --> <tests>src/test/ruby</tests> </configuration> <executions> <execution> <goals> <goal>test</goal> </goals> </execution> </executions> </plugin> ... </plugins> |
使用ant的开发人员请参考这里。用Ruby做单元测试和Java一样,简单从Test::Unit::TestCase继承即可:
class MyFirstJRubyTests < Test::Unit::TestCase def test_true assert true end end |
可以将这个测试文件简单拷贝到myProj/src/test/ruby目录下,运行mvn test,你会看到JtestR产生的测试结果输出:
[INFO] [jtestr:test {execution: default}] Other TestUnit: 1 tests, 0 failures, 0 errors |
在这段输出报告之上,你应该还能看到正常的Java unit testcase输出的测试结果,这表明,我们可以在开发的过程中同时选择用Java的方式测试,或用Ruby的方式测试!
相关阅读: