sbt 和 Scala Test

SBT 构建工具

sbt使用标准的项目结果,你可以很方便构建你的第一个项目。
项目目录结构:

build.sbt
project/
src/
|-- main/
   |-- java/
   |-- resources/
   |-- scala/
|-- test/
   |-- java/
   |-- resources/
   |-- scala/
target/

在build.sbt文件中:

name := "HelloWorld"
version := "1.0"
scalaVersion := "2.13.2"

在src/main/scala 目录下:

object HelloWorld extends App {
     
    println("Hello, world")
}

在项目目录下:

$ sbt run

Updated file /Users/al/Projects/Scala/Hello/project/build.properties setting sbt.version to: 0.13.15
[warn] Executing in batch mode.
[warn]   For better performance, hit [ENTER] to switch to interactive mode, or
[warn]   consider launching sbt without any commands, or explicitly passing 'shell'
[info] Loading project definition from /Users/al/Projects/Scala/Hello/project
[info] Updating {
     file:/Users/al/Projects/Scala/Hello/project/}hello-build...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[info] Set current project to Hello (in build file:/Users/al/Projects/Scala/Hello/)
[info] Updating {
     file:/Users/al/Projects/Scala/Hello/}hello...
[info] Resolving jline#jline;2.14.5 ...
[info] Done updating.
[info] Compiling 1 Scala source to /Users/al/Projects/Scala/Hello/target/scala-2.12/classes...
[info] Running HelloWorld 
Hello, world
[success] Total time: 4 s

也可以启动sbt再运行:

> sbt
[info] Loading project definition from /Users/al/Projects/Scala/Hello/project
[info] Set current project to Hello (in build file:/Users/al/Projects/Scala/Hello/)
> run
[info] Running HelloWorld 
Hello, world
[success] Total time: 0 s

可以输入 exit 来退出sbt ,或快捷键 ctrl+D

还有一些可以使用的构建工具:

  • Ant
  • Gradle
  • Maven
  • Fury
  • Mill

ScalaTest 代码测试框架

build.sbt文件:

name := "HelloScalaTest"
version := "1.0"
scalaVersion := "2.13.2"

libraryDependencies +=
    "org.scalatest" %% "scalatest" % "3.0.8" % Test
)

然后在src/main/scala 写代码:

package simpletest

object Hello extends App {
     
    val p = new Person("Alvin Alexander")
    println(s"Hello ${p.name}")
}

class Person(var name: String)
> sbt run

[warn] Executing in batch mode.
[warn]   For better performance, hit [ENTER] to switch to interactive mode, or
[warn]   consider launching sbt without any commands, or explicitly passing 'shell'
...
...
[info] Compiling 1 Scala source to /Users/al/Projects/Scala/HelloScalaTest/target/scala-2.12/classes...
[info] Running simpletest.Hello 
Hello Alvin Alexander
[success] Total time: 4 s

使用ScalaTest

在 src/test/scala 中写代码:

package simpletest

import org.scalatest.FunSuite

class HelloTests extends FunSuite {
     

    // test 1
    test("the name is set correctly in constructor") {
     
        val p = new Person("Barney Rubble")
        assert(p.name == "Barney Rubble")
    }

    // test 2
    test("a Person's name can be changed") {
     
        val p = new Person("Chad Johnson")
        p.name = "Ochocinco"
        assert(p.name == "Ochocinco")
    }

}
> sbt test
[info] Set current project to HelloScalaTest (in build file:/Users/al/Projects/Scala/HelloScalaTest/)
[info] HelloTests:
[info] - the name is set correctly in constructor
[info] - a Person's name can be changed
[info] Run completed in 277 milliseconds.
[info] Total number of tests run: 2
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 2, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
[success] Total time: 1 s

BDD( Behavior-Driven Development)行为驱动开发

在src/main/scala 写代码:

package simpletest

object MathUtils {
     

    def double(i: Int) = i * 2

}

在src/test/scala 写代码:

package simpletest

import org.scalatest.FunSpec

class MathUtilsSpec extends FunSpec {
     
  
    describe("MathUtils::double") {
     

        it("should handle 0 as input") {
     
            val result = MathUtils.double(0)
            assert(result == 0)
        }

        it("should handle 1") {
     
            val result = MathUtils.double(1)
            assert(result == 2)
        }

        it("should handle really large integers") (pending)
        
    }

}

在TDD(测试驱动开发)中是使用 FunSuite
在BDD(行为驱动开发)中使用 FunSpec

> sbt test

[info] HelloTests:
[info] - the name is set correctly in constructor
[info] - a Person's name can be changed
[info] MathUtilsSpec:
[info] MathUtils::double
[info] - should handle 0 as input
[info] - should handle 1
[info] - should handle really large integers (pending)
[info] Total number of tests run: 4
[info] Suites: completed 2, aborted 0
[info] Tests: succeeded 4, failed 0, canceled 0, ignored 0, pending 1
[info] All tests passed.
[success] Total time: 4 s, completed Jan 6, 2018 4:58:23 PM

你可能感兴趣的:(Scala,Book,scala)