从内部测试访问ScalaTest测试名称?

Kaj*_*nus 11 integration-testing scala scalatest

是否可以从ScalaTest测试中访问当前正在执行的测试的名称?(我该怎么做?)

背景:

我正在测试我的数据访问对象最终会抛出OverQuotaException一个用户,例如创建太多页面.这些测试需要很长时间才能运行.为了感觉更快乐,我想将进度打印到stdout - 由于有很多测试,我想在输出中包含测试名称,所以我知道当前正在运行什么测试.

(我在这里找不到任何看似相关的功能:http: //www.artima.com/docs-scalatest-2.0.M5/#org.scalatest.FreeSpec)

例:

  "QuotaCharger can" - {
    "charge and decline quota consumers" - {

      "charge a per site IP number (guest user)" in {
         // ... Here, a guest user post very many comments until it's over quota.
         // This takes a little while, and there are many similar tests.

         // ---> Here <--- I'd like to access the string:
         //   "charge a per site IP number (guest user)",
         //  is that possible somehow?
      }
Run Code Online (Sandbox Code Playgroud)

Bil*_*ers 11

预期的方法是覆盖withFixture并捕获测试数据.在这个用例中,最好在fixture.FreeSpec中覆盖withFixture,这样你就可以将测试数据传递给每个测试,而不是使用var.信息就在这里:

http://www.artima.com/docs-scalatest-2.0.M5/org/scalatest/FreeSpec.html#withFixtureNoArgTest

当我今天早上看到你的问题时,我意识到ScalaTest应该具有这样做的特性,所以我只添加了一个.它将在2.0.M6,下一个里程碑版本,但在此期间你可以使用本地副本.这里是:

import org.scalatest._

/**
 * Trait that when mixed into a <code>fixture.Suite</code> passes the
 * <code>TestData</code> passed to <code>withFixture</code> as a fixture into each test.
 *
 * @author Bill Venners
 */
trait TestDataFixture { this: fixture.Suite =>

  /**
   * The type of the fixture, which is <code>TestData</code>.
   */
  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 <code>TestData</code> for the test.
   *
   * <p>
   * To enable stacking of traits that define <code>withFixture(NoArgTest)</code>, this method does not
   * invoke the test function directly. Instead, it delegates responsibility for invoking the test function
   * to <code>withFixture(NoArgTest)</code>.
   * </p>
   *
   * @param test the <code>OneArgTest</code> to invoke, passing in the
   *   <code>TestData</code> fixture
   */
  def withFixture(test: OneArgTest) {
    withFixture(test.toNoArgTest(test))
  }
}
Run Code Online (Sandbox Code Playgroud)

你会像这样使用它:

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")
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 嗯,`def currentTestName:String`可以某种方式使用ThreadLocal,使其与并发测试一起使用?这可能是我认为最喜欢的方法.(但我现在的测试工作正常,所以对我而言不再重要:-)) (2认同)