IntelliJ IDEA + Scala:如何轻松导航到损坏的测试

问题描述 投票:0回答:1

我在 Scala 测试中有这样的构造:

class ExpressionsTest extends AnyFunSpec {
  describe("simple literals") {
    describe("valid") {
      it("123") {
        runTest("123")
      }

      it("123.456") {
        runTest("123.456")
      }

      // gazillions of other tests here
    }
  }

  def runTest(ex: String) {
    // some repetitive implementation of a test
    // includes assertions, which sometimes break
  }
}

...并且有很多

it(...)
的实例提供了测试用例的结构,每个实例都在内部调用
runTest(...)
。但是,当测试中断时,Intellij IDEA 导航到
runTest(...)
的内部行(通常不会中断),我希望它导航到测试用例本身 - 即
it(...)
行。

我知道的两种替代方法是:

  • 显然,将
    runTest(...)
    复制到每个方法中 — 丑陋且容易出错
  • ,有效地将
    runTest(...)
    嵌入到
    it(...)
    中,这在这里似乎是一个巨大的杀伤力

有什么方法可以让开发者使用 IntelliJ IDEA 获得更好的体验吗?

scala intellij-idea scalatest
1个回答
0
投票

您可以将隐式

Position
传递给
runTest
方法(该方法由 Scalatest 提供,并使用宏为您提供错误的文件/行坐标),如下所示:

import org.scalactic.source.Position
import org.scalatest.funspec.AnyFunSpec

class ExpressionsTest extends AnyFunSpec {
  describe("simple literals") {
    describe("valid") {
      it("123") {
        runTest("123")
      }

      it("123.456") {
        runTest("123.456")
      }

      // gazillions of other tests here
    }
  }

  // Passing an implicit `Position` here allows an assertion failure to
  // be reported at the caller's file and line number
  def runTest(ex: String)(implicit pos: Position) = {
    // some repetitive implementation of a test
    // includes assertions, which sometimes break
    assert(ex == "123")
  }
}

运行此测试会导致以下错误:

"123[.456]" did not equal "123[]"
ScalaTestFailureLocation: ExpressionsTest at (ExpressionsTest.scala:12)
Expected :"123[]"

通过 IntelliJ IDEA UI 单击

ExpressionsTest.scala:12
导航到显示
runTest("123.456")
的行(如果没有隐式
Position
,结果将是您将被指向第 21 行,转到
runTest
方法代替)。

© www.soinside.com 2019 - 2024. All rights reserved.