scalaTest的使用

配置

修改pom.xml,添加以下内容


<dependency>
  <groupId>org.scalatestgroupId>
  <artifactId>scalatest_2.11artifactId>
  <version>3.0.0version>
  <scope>testscope>
dependency>


<plugin>
    <groupId>org.scalatestgroupId>
    <artifactId>scalatest-maven-pluginartifactId>
    <version>1.0version>
    <configuration>
        <reportsDirectory>${project.build.directory}/surefire-reportsreportsDirectory>
        <junitxml>.junitxml>
        <filereports>WDF TestSuite.txtfilereports>
    configuration>
    <executions>
        <execution>
            <id>testid>
            <goals>
                <goal>testgoal>
            goals>
        execution>
    executions>
plugin>


<testSourceDirectory>${basedir}/src/test/scalatestSourceDirectory>

一个简单的例子

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

如果测试通过,

scalaTest的使用_第1张图片

如果测试不通过,则会打包失败,比如

  test("Test difference") {
    val a = Set("a", "b", "a", "c")
    val b = Set("b", "d")
    //应该等于Set("a","b")
    assert(a -- b === Set("b", "c"))
  }

scalaTest的使用_第2张图片

你可能感兴趣的:(scala)