【问题标题】:Specs2 unable to find my tests using sbtSpecs2 无法使用 sbt 找到我的测试
【发布时间】:2016-02-29 10:21:12
【问题描述】:

我已经使用简单的 sbt 构建了一个用于 Spec2 测试的简单项目。

package main.specs

import org.specs2._

class QuickStartSpec extends Specification {
  def is = s2"""
    This is my first specification
    it is working                 $ok
    really working!               $ok
                             """
}

这是我的 build.sbt 文件:

name := "QuickStartSpec"

version := "1.0"

scalaVersion := "2.10.1"

libraryDependencies ++= Seq("org.specs2" %% "specs2-core" % "3.6.5" % "test")

scalacOptions in Test ++= Seq("-Yrangepos")

但是当我在 sbt 中运行这个命令时

 testOnly main.specs.QuickStartSpec

我得到了这个:

[info] Updating {file:/Users/nabajeet/workspace/SpecsTest/}specstest...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[info] Passed: Total 0, Failed 0, Errors 0, Passed 0
[info] No tests to run for test:testOnly

我正在关注此页面以创建示例: https://etorreborre.github.io/specs2/website/SPECS2-3.6.5/quickstart.html

我无法弄清楚我的测试未检测到的原因。

我在 0.13.8 中的 sbt 版本

【问题讨论】:

  • 你能提供你项目的目录和文件布局吗?我怀疑测试不在正确的位置。在 vanilla sbt 项目中,QuickStartSpec.scala 应该在 /Users/nabajeet/workspace/SpecsTest/src/test/scala/main/specs/QuickStartSpec.scala 中(它可以在 /Users/nabajeet/workspace/SpecsTest/src/test/scala/ 下的任何位置,因为包名不必在 fs 结构中表示)
  • /Users/nabajeet/workspace/SpecsTest/src/main/specs/quickStartSpec.scala

标签: scala sbt specs2


【解决方案1】:

通过声明

libraryDependencies ++= Seq("org.specs2" %% "specs2-core" % "3.6.5" % "test")

您将specs2 的范围限制为仅测试源目录中的类。您将无法在生产代码中引用specs2 类(src/main/ 下的所有代码)

在您的评论中,您表示您将规范放在/Users/nabajeet/workspace/SpecsTest/src/main/specs/quickStartSpec.scala

尝试将您的文件移动到 /Users/nabajeet/workspace/SpecsTest/src/test/scala/specs/quickStartSpec.scala

不正确的位置是您指定 SBT 没有拾取它的原因(我有信心说它也无法编译)。

默认情况下,SBT 应用 maven 的 standard directory layout 添加 src/main/scala/src/test/scala/ 用于 scala 代码。这记录在SBT tutorial

我刚刚创建了一个具有以下布局的项目

.
./built.sbt
./src
./src/test
./src/test/scala
./src/test/scala/QuickStartSpec.scala

build.sbt 包含

name := "QuickStartSpec"

version := "1.0"

scalaVersion := "2.11.4"

libraryDependencies ++= Seq("org.specs2" %% "specs2-core" % "3.6.5" % "test")

scalacOptions in Test ++= Seq("-Yrangepos")

并且 QuickStartSpec.scala 包含

package main.specs

import org.specs2._

class QuickStartSpec extends Specification {
  def is = s2"""
    This is my first specification
    it is working                 $ok
    really working!               $ok
                             """
}

这是我得到的 sbt 输出

sbt
[info] Set current project to QuickStartSpec (in build file:/tmp/stack/)
> test:compile
[info] Updating {file:/tmp/stack/}stack...
[info] Resolving jline#jline;2.12 ...
[info] Done updating.
[info] Compiling 1 Scala source to /tmp/stack/target/scala-2.11/test-classes...
[info] 'compiler-interface' not yet compiled for Scala 2.11.4. Compiling...
[info]   Compilation completed in 6.372 s
[success] Total time: 9 s, completed 27 nov. 2015 06:38:26
> test
[info] QuickStartSpec
[info]     + This is my first specification
[info]       it is working
[info]     + really working!
[info]
[info] Total for specification QuickStartSpec
[info] Finished in 17 ms
[info] 2 examples, 0 failure, 0 error
[info]
[info] Passed: Total 2, Failed 0, Errors 0, Passed 2
[success] Total time: 1 s, completed 27 nov. 2015 06:38:31
>

【讨论】:

  • 谢谢@Jean 但现在我收到编译错误!!! [error] exception during macro expansion: [error] java.lang.UnsupportedOperationException: Position.end on class scala.reflect.internal.util.OffsetPosition [error] at scala.reflect.internal.util.Position.end(Position.scala:126) [error] at org.specs2.specification.create.S2Macro$$anonfun$2.apply(S2Macro.scala:29)
  • 我尝试将 scala 版本更改为 2.11.4 以解决某些解决方案中提到的此问题,但这再次使规范无法检测到测试!!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-08
  • 1970-01-01
  • 1970-01-01
  • 2020-01-15
  • 1970-01-01
  • 2018-08-22
相关资源
最近更新 更多