ScalaTest test name without fasteners?

Firstly, I saw this, and this other post sounds exactly the same as I need, except for one, I can’t use fixture.TestDataFixture, because I can’t extend fixture.FreeSpecLike, and I'm sure there should be some way to get the test name in a way that looks more like this (imaginary code that doesn't compile).

class MySpec extends FlatSpecLike with fixture.TestDataFixture {
  "this technique" - {
     "should work" in { 
      assert(testData.name == "this technique should work")
    }
    "should be easy" in { td =>
      assert(testData.name == "this technique should be easy")
    }
  }
}

Any ideas? I just can’t believe that this is impossible: D

+4
source share
3 answers

While you have already come to this solution, here is a safer change:

private val _currentTestName = new ThreadLocal[String]

override def withFixture(test: NoArgTest) = {
  _currentTestName.set(test.name)
  val outcome = super.withFixture(test)
  _currentTestName.set(null)
  outcome
}

protected def currentTestName: String = {
  val testName = _currentTestName.get()
  assert(testName != null, "currentTestName should only be called in a test")
  testName
}

On the other hand,

protected def currentTestName = Option(_currentTestName.get())
+1
source

( ), , , :

,

class MySpec extends FlatSpecLike {
//... other stuff
    var testName = "UndefinedTestName"
    override def withFixture (test: NoArgTest) :Outcome= {
       testName = test.name
       super.withFixture(test)
   }
}

, , , - - .

0

You can find the sequence of test names using the testNames method in the FlatSpecLike characteristic:

import org.scalatest.FlatSpecLike

class FooSpec extends FlatSpecLike {

  it should "check case one" in {
    println("test some code ...")

    println(testNames.mkString("\n"))
    /*
      prints:
      should check case one
      should check case two
    */
    // ...
    assert(1 == 1)
    println("end.")
  }

  it should "check case two" in {
    println("test some code ...")

    assert(1 == 1)
    println("end.")
  }
}

and find everyone you need. Hope this helps.

0
source

All Articles