scalaTest的使用
2016-08-31 本文已影响1102人
breeze_lsw
配置
修改pom.xml
,添加以下内容
<!--依赖-->
<dependency>
<groupId>org.scalatest</groupId>
<artifactId>scalatest_2.11</artifactId>
<version>3.0.0</version>
<scope>test</scope>
</dependency>
<!--插件-->
<plugin>
<groupId>org.scalatest</groupId>
<artifactId>scalatest-maven-plugin</artifactId>
<version>1.0</version>
<configuration>
<reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>
<junitxml>.</junitxml>
<filereports>WDF TestSuite.txt</filereports>
</configuration>
<executions>
<execution>
<id>test</id>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
<!--测试代码和文件-->
<testSourceDirectory>${basedir}/src/test/scala</testSourceDirectory>
一个简单的例子
import org.scalatest.FunSuite
class SetFuncSuite extends FunSuite {
//差集
test("Test difference") {
val a = Set("a", "b", "a", "c")
val b = Set("b", "d")
assert(a -- b === Set("a", "c"))
}
//交集
test("Test intersection") {
val a = Set("a", "b", "a", "c")
val b = Set("b", "d")
assert(a.intersect(b) === Set("b"))
}
//并集
test("Test union") {
val a = Set("a", "b", "a", "c")
val b = Set("b", "d")
assert(a ++ b === Set("a", "b", "c", "d"))
}
}
在IDEA里直接运行
这里写图片描述程序打包时会自动进行测试
mvn clean package
如果测试通过,
这里写图片描述如果测试不通过,则会打包失败,比如
test("Test difference") {
val a = Set("a", "b", "a", "c")
val b = Set("b", "d")
//应该等于Set("a","b")
assert(a -- b === Set("b", "c"))
}
这里写图片描述