scalaTest的使用

来源:互联网 发布:linux ps命令中括号 编辑:程序博客网 时间:2024/05/22 11:51

配置

修改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.FunSuiteclass 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"))  }

这里写图片描述

0 0
原创粉丝点击