I have construct like this in my Scala tests:
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
}
}
... and there a lot of instances of it(...) which provide structure of test cases, every one of them calling runTest(...) internally. However, when a test breaks, Intellij IDEA navigates to inner lines of runTest(...) (which are normally not broken), and I want it to navigate to test case itself — i.e. it(...) line.
Two alternative ways I'm aware of are:
- Obviously, copying
runTest(...)into every single method — ugly and error-prone - Macros, effectively doing these same embedding of
runTest(...)intoit(...), which seems to be a huge overkill here
Is there any way to achieve nicer developer's experience with IntelliJ IDEA here?
You can pass an implicit
Positionto therunTestmethod (which is provided by Scalatest and uses macros to give you the file/line coordinate for the error), like so:Running this test results in the following error:
Clicking on the
ExpressionsTest.scala:12through the IntelliJ IDEA UI navigates to the line that saysrunTest("123.456")(without the implicitPosition, the result would have been that you would have been pointed to line 21, going to therunTestmethod instead).Positionis part of Scalactic, a dependency of ScalaTest (created by the same author). You can read more about thePositionclass here.