如何在 IntelliJ idea 中运行 scala ScalaTest?

发布于 2024-10-08 21:56:18 字数 842 浏览 5 评论 0原文

我正在尝试在 Intellij IDEA(最新的社区构建,带有最新的 Scala 插件)中运行 scala flatspec 测试,但我不断收到“空测试套件”错误。

我尝试使用右键单击时的正常“运行”菜单,但它不起作用。我还尝试创建一个新的 ScalaTest 配置,但运行程序仍然没有进行测试。

我能够将 JScalaTest 与 unit 一起使用,但我真的更喜欢使用 flatspec 语法。

更新:用@RunWith(classOf[JUnitRunner])注释类也没有帮助

谢谢!

class SampleTestSpec extends FlatSpec with ShouldMatchers {
    "test" should "fail" in {
        "this" should equal ("that")
    }
}

更新:从 ScalaTest 切换到 Spec,解决了问题。我仍然更喜欢 ScalaTest 和 FlatSpec,但这已经足够好了。有效的代码:

import org.specs._
object SampleTestSpec extends Specification {
    "'hello world' has 11 characters" in {
     "hello world".size must be equalTo(113)
  }
  "'hello world' matches 'h.* w.*'" in {
     "hello world" must be matching("h.* w.*")
  }
}

-teo

I'm trying to run a scala flatspec test within Intellij IDEA (latest community build, with latest Scala plugin), but I keep getting "Empty test suite" errors.

I tried using the normal "run" menu on right click, but it does not work. I also tried creating a new ScalaTest configuration, but still the runner is not picking up the tests.

I was able to use JScalaTest with unit, but I'd really prefer to use flatspec syntax.

UPDATE: annotating the class with @RunWith(classOf[JUnitRunner]) does not help either

Thanks!

class SampleTestSpec extends FlatSpec with ShouldMatchers {
    "test" should "fail" in {
        "this" should equal ("that")
    }
}

UPDATE: Switching from ScalaTest to Spec, solved the problem. I still prefer ScalaTest with FlatSpec, but this is good enough. Code that works:

import org.specs._
object SampleTestSpec extends Specification {
    "'hello world' has 11 characters" in {
     "hello world".size must be equalTo(113)
  }
  "'hello world' matches 'h.* w.*'" in {
     "hello world" must be matching("h.* w.*")
  }
}

-teo

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

绻影浮沉 2024-10-15 21:56:18

如果 IntelliJ 没有自动选择测试,您可以执行以下操作:

在运行配置中,创建一个 ScalaTest 运行配置,并将其“测试种类”设置为“全部包含在包中”,将“测试包”设置为要运行的包。包含您的测试。

If IntelliJ does not pick up the tests automatically, you can do the following:

In the Run Configuration, create a ScalaTest run configuration and set up its "Test kind" to "All in package" and its "Test Package" to the package that contains your tests.

意中人 2024-10-15 21:56:18

这对我有用:
编辑配置->测试类型:全包

This worked for me:
Edit configurations -> Test kind: All in package

偏闹i 2024-10-15 21:56:18

我一直这样运行它(但是我确实发现它有点冗长,并且可能可以减少):

@RunWith(classOf[JUnitSuiteRunner])
class AuthorisationParserRunAsTest extends JUnit4(AuthorisationParserSpec)
object AuthorisationParserSpec extends Specification {
  ...
}

I've been running it like this (however I do find it a little verbose and it can probably be cut down):

@RunWith(classOf[JUnitSuiteRunner])
class AuthorisationParserRunAsTest extends JUnit4(AuthorisationParserSpec)
object AuthorisationParserSpec extends Specification {
  ...
}
情丝乱 2024-10-15 21:56:18

ScalaTest 对所有可能性都有概述;我刚刚注释了所有测试类

@RunWith(classOf[JUnitRunner])

ScalaTest has an Overview over all possibilities; I just annotated all test classes with

@RunWith(classOf[JUnitRunner])
惟欲睡 2024-10-15 21:56:18

AFAIK, the JUnitRunner is exactly for this.

烟火散人牵绊 2024-10-15 21:56:18

只是有点太晚了,但我遇到了同样的问题(“空测试套件”)在闲逛 IDEA 错误跟踪器后,我找到了适合我的案例的解决方案:
我的 scala 版本是 2.9.0-1scala-test 的版本是 2.8.*
将 scala-test 升级到最新版本(与 scala 相同)后它开始工作。

Just a little too late, but I've met the same problem ("Empty test suite") After hanging around IDEA bug tracker, I've found a solution for my case:
My scala version was 2.9.0-1 and scala-test has version 2.8.*.
It started to work after upgrading scala-test to the newest version (same as a scala).

ゝ杯具 2024-10-15 21:56:18

我已经能够在 IntelliJ 中运行 FunSuite 测试,没有任何问题。但是,我在使用 FlatSpec 测试时发现,如果使用 Ctrl+Shift+F10(即运行所有测试),则需要右键单击或将光标定位在实际测试类定义上才能正确执行所有定义的测试并避免令人沮丧的“空测试套件”错误。

I've been able to run FunSuite tests in IntelliJ without any issues. However, what I've found when working with FlatSpec tests, is that you need to right click on or have your cursor located on the actual test class definition if using Ctrl+Shift+F10 (i.e. run all tests) for it to correctly execute all defined tests and avoid the frustrating "Empty test suite" errors.

等待圉鍢 2024-10-15 21:56:18

如果它对任何人有帮助——我刚刚接触 Scala,这是因为我构建测试类的方式。我遇到的

class SomeTests extends FlatSpec {
  def aTest = {
    "This thing when that thing happens" should
    "return some particular response" in {
      // ... test body
    }
  }
}

问题是我将测试包装在一个方法中,没有意识到在这种情况下测试需要是裸露的:

class SomeTests extends FlatSpec {

    "This thing when that thing happens" should
    "return some particular response" in {
      // ... test body
    }

}

In case it helps anyone -- I hit this fresh to Scala and it was because of the way I'd structured my test class. I had

class SomeTests extends FlatSpec {
  def aTest = {
    "This thing when that thing happens" should
    "return some particular response" in {
      // ... test body
    }
  }
}

The problem was that I wrapped my test in a method, not realising that in this context the tests need to be bare:

class SomeTests extends FlatSpec {

    "This thing when that thing happens" should
    "return some particular response" in {
      // ... test body
    }

}
臻嫒无言 2024-10-15 21:56:18

我遇到了“空测试套件”。在 FlatSpec 中运行单独测试时也会出现问题。我将根本原因缩小到行为字符串开头的空格。

前任。在以下代码中,第一个测试将运行,但第二个测试不会运行(如果单独运行)。这是由于行为字符串“not run”开头存在空格所致。如果您将其取下并再次运行,它应该可以工作。

import org.scalatest.FlatSpec

class FlatSpecWhiteSpaceTest extends FlatSpec {
    "TestWithNoEmptySpace" should "run" in {
        runTest
    }

    "TestWithEmptySpace" should " not run" in {
        runTest
    }

    def runTest(): Unit = {
        val expectedValue = true
        val actualValue = true
        assert(expectedValue === actualValue)
    }
}
  • IDE:Intellij IDeA 2017.1C
  • Scala 版本:2.11.8
  • ScalaTest 版本:3.0.1

I ran into "Empty test suite." issue while running individual tests in FlatSpec as well. I narrowed down the root cause to a whitespace at the beginning of the in the behavior string.

Ex. In the following code, the first test will run but the second will not (If individually run). This is due to the presence of a whitespace at the beginning of the behavior string " not run". If you take it off and run it again, it should work.

import org.scalatest.FlatSpec

class FlatSpecWhiteSpaceTest extends FlatSpec {
    "TestWithNoEmptySpace" should "run" in {
        runTest
    }

    "TestWithEmptySpace" should " not run" in {
        runTest
    }

    def runTest(): Unit = {
        val expectedValue = true
        val actualValue = true
        assert(expectedValue === actualValue)
    }
}
  • IDE: Intellij IDeA 2017.1C
  • Scala Version: 2.11.8
  • ScalaTest Version: 3.0.1
很快妥协 2024-10-15 21:56:18

您必须在文件末尾将其设为Test,然后在测试配置中单击Run on Sbt

You have to make it Test at the end of the file and click Run on Sbt In the test config.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文