scala 如何在 IntelliJ 想法中运行 ScalaTest?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4508818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 02:40:34  来源:igfitidea点击:

How do I run a scala ScalaTest in IntelliJ idea?

unit-testingscalaintellij-ideascalatest

提问by Matteo Caprari

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.

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

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.

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

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

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

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

更新:注释类@RunWith(classOf[JUnitRunner])也无济于事

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:

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

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

-teo

采纳答案by thoredge

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 {
  ...
}

回答by lysium

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

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

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.

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

回答by gstvolvr

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

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

回答by Hans-Peter St?rr

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

ScalaTest对所有可能性进行了概述;我只是注释了所有测试类

@RunWith(classOf[JUnitRunner])

回答by Conor Svensson

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.

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

回答by om-nom-nom

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 scalaversion was 2.9.0-1and scala-testhas version 2.8.*.
It started to work after upgrading scala-testto the newest version (same as a scala).

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

回答by michael.kebe

AFAIK, the JUnitRunneris exactly for this.

AFAIK,JUnitRunner正是为此。

回答by Tim Barrass

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

以防它对任何人有帮助——我对 Scala 有了新的认识,这是因为我构建测试类的方式。我有

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
    }

}

回答by Jay

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.

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

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.

前任。在下面的代码中,第一个测试将运行,但第二个不会(如果单独运行)。这是因为在行为字符串“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 Version: 2.11.8
  • ScalaTest Version: 3.0.1
  • IDE:Intellij IDeA 2017.1C
  • 斯卡拉版本:2.11.8
  • ScalaTest 版本:3.0.1

回答by user12125202

You have to make it Testat the end of the file and click Run on SbtIn the test config.

您必须Test在文件末尾进行设置,然后单击Run on Sbt在测试配置中。