Is it possible to access the name of the currently executing test, from within a ScalaTest test? (And how would I do it?)
Background:
I\'m testi
The intended way to do that is to override withFixture and capture the test data. In this use case, it is better to override withFixture in fixture.FreeSpec so you can pass the test data into each test rather than using a var. Info on that is here:
http://www.artima.com/docs-scalatest-2.0.M5/org/scalatest/FreeSpec.html#withFixtureNoArgTest
When I saw your question this morning I realized ScalaTest should have a trait that does this, so I just added one. It will be in 2.0.M6, the next milestone release, but in the meantime you can use a local copy. Here it is:
import org.scalatest._
/**
* Trait that when mixed into a fixture.Suite
passes the
* TestData
passed to withFixture
as a fixture into each test.
*
* @author Bill Venners
*/
trait TestDataFixture { this: fixture.Suite =>
/**
* The type of the fixture, which is TestData
.
*/
type FixtureParam = TestData
/**
* Invoke the test function, passing to the the test function to itself, because
* in addition to being the test function, it is the TestData
for the test.
*
*
* To enable stacking of traits that define withFixture(NoArgTest)
, this method does not
* invoke the test function directly. Instead, it delegates responsibility for invoking the test function
* to withFixture(NoArgTest)
.
*
*
* @param test the OneArgTest
to invoke, passing in the
* TestData
fixture
*/
def withFixture(test: OneArgTest) {
withFixture(test.toNoArgTest(test))
}
}
You would use it like this:
import org.scalatest._
class MySpec extends fixture.FreeSpec with TestDataFixture {
"this technique" - {
"should work" in { td =>
assert(td.name == "this technique should work")
}
"should be easy" in { td =>
assert(td.name == "this technique should be easy")
}
}
}