【问题标题】:How do I specify specific test from the build.sbt如何从 build.sbt 指定特定测试
【发布时间】:2019-09-05 09:54:48
【问题描述】:

如何从 build.sbt 文件中指定测试,我只想运行一个测试,我使用了 sbt 文档中的过滤器,但它不适用于我,这是我的代码两个测试类,在我的 sbt 中我指定 test1 为 rub 但似乎这两个测试同时运行有人知道我应该做什么吗?

Test1Demo.scala

import org.scalatest.{FlatSpec, Matchers}

class Test1Demo extends FlatSpec with Matchers{
  "value of x " should " be 9 " in { assert(my.App.x == 9) }
}

Test2Demo.scala

import org.scalatest.{FlatSpec, Matchers}

class Test2Demo extends FlatSpec with Matchers{
  "value of y " should " be 8 " in { assert(my.App2.y == 8) }
}

build.sbt

version := "0.1"

scalaVersion := "2.12.8"

libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.5" % Test

testOptions in Test := Seq(Tests.Filter(s => s.startsWith("Test1")))

输出:

[info] Done updating.
[info] Compiling 2 Scala sources to /home/****/target/scala-2.12/classes ...
[info] Done compiling.
[info] Compiling 2 Scala sources to /home/****/target/scala-2.12/test-classes ...
[info] Done compiling.
[info] Test2Demo:
[info] value of y 
[info] - should be 8
[info] Test1Demo:
[info] value of x 
[info] - should be 9
[info] Run completed in 6 seconds, 365 milliseconds.
[info] Total number of tests run: 2
[info] Suites: completed 2, aborted 0
[info] Tests: succeeded 2, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
[success] Total time: 264 s, completed Apr 15, 2019 2:47:10 PM

【问题讨论】:

    标签: scala sbt scalatest


    【解决方案1】:

    如果你想从Test1Demo 运行value of x 测试:

    testOnly *Test1Demo -- -z value
    

    这个 sbt 命令将只运行名称中包含子字符串“value”的测试。

    对于完全匹配而不是子字符串,使用-t 而不是-z

    注意--(两个-,不是一个)

    【讨论】:

    • 感谢您的回答,但我有 sbt 交叉编译平台,想分别运行每个平台测试。我只需要 JVM 测试,而不是本机或 JS,而不使用 testOnly 命令只需在我的 build.sbt 文件中添加一些设置
    • 也许我使用过滤器有误,您能纠正我吗?
    • 我认为您错过了config 部分。尝试这样的事情:lazy val FunTest = config("fun") extend(Test) ThisBuild / organization := "com.example" ThisBuild / scalaVersion := "2.12.8" ThisBuild / version := "0.1.0-SNAPSHOT" def unitFilter(name: String): Boolean = (name endsWith "Test") lazy val root = (project in file(".")) .configs(FunTest) .settings( libraryDependencies += scalatest % FunTest, testOptions in Test := Seq(Tests.Filter(unitFilter)) )。来自官方文档scala-sbt.org/1.x/docs/Testing.html
    • 我应该在哪里替换我想要运行的测试名称?
    • name endsWith "Test1Demo"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-19
    • 2017-03-25
    • 1970-01-01
    相关资源
    最近更新 更多