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

时间:2013-02-12 11:15:46

标签: scala integration-testing 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?
      }

5 个答案:

答案 0 :(得分:9)

这样做的目的是覆盖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))
  }
}

您可以这样使用它:

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")
    }
  }
}

答案 1 :(得分:2)

创建自己的特性,比如RichFreeSpec

trait RichFreeSpec extends Free {
  protected final class RichFreeSpecStringWrapper(name: scala.Predef.String) {
    def in(f: String => scala.Unit) {
      def f2 = f(name)
      new WordSpecStringWrapper(string).in(f2)
    }
  }  

  protected implicit def convertToRichFreeSpecStringWrapper(n: scala.Predef.String): = {
    new RichFreeSpecStringWrapper(n)
  }
}

不仅仅是使用:

"sth" in { testName => 
   ...
 }

当然,您可以进一步实现全名层次结构。

答案 2 :(得分:1)

这是一个解决方案。扩展此类而不是FreeSpec。许可证:CC0

编辑:但这不适用于并发测试。

(这种方法和另一个答案的区别在于1)这里有一个currentTestName字段,在另一个答案中,测试名称被传递给测试体,2)这个测试名称包括所有测试分支名称连接+实际测试名称,而另一个答案的测试名称正是测试名称(没有测试分支名称)。)

(哎呀,你需要使用getOrElse ...而不是我可爱的getOrDie。)

/**
 * Adds a field `currentTestName` that you can use inside a FreeSpec test,
 * if you for example have many tests that take rather long, and you wonder
 * which one is currently running.
 */
trait RichFreeSpec extends FreeSpec {

  private var _currentTestName: Option[String] = None
  def currentTestName = _currentTestName getOrDie "DwE90RXP2"

  protected override def runTest(testName: String, args: org.scalatest.Args) {
    _currentTestName = Some(testName)
    super.runTest(testName, args)
  }
}

答案 3 :(得分:0)

如果目的是能够从任何地方访问测试名称,正如@kajmanus 在之前的评论中所建议的那样,ThreadLocal 非常适合。

您可以定义一个案例类来存储当前测试上下文所需的信息。例如,

case class TestContext(name: Option[String] = None)

object TestContext {
  val currentTest: ThreadLocal[TestContext] =
    ThreadLocal.withInitial(() => TestContext())
}

然后定义您的各种规格将扩展的特征。例如,

trait BaseFunSpec
  extends AnyFunSpec
  ...
{
  override protected def withFixture(test: NoArgTest): Outcome = {
    try {
      TestContext.currentTest.set(TestContext(name = Some(test.name)))
      super.withFixture(test)
    } finally {
      TestContext.currentTest.remove()
    }
  }
}

最后,您可以根据需要从当前线程内的任何位置访问您为当前线程设置的当前测试上下文(在本例中纯粹是测试名称)。例如,

def cachedResults(bytes: Array[Byte], fileType: String): Unit = {
  TestContext.currentTest.get().name match {
    case Some(testname) => 
      import scala.util.Using
      val file = new File("target", s"${testname}.${fileType}")
      Using(new BufferedOutputStream(new FileOutputStream(file))) { os =>
        os.write(bytes)
      }
    case None => throw new IllegalStateException("Unknown test context")
  }
}

这将适用于您是否并行运行测试,假设您没有异步处理事物(即,在另一个线程中)。

更简洁的用法是创建有目的的角色。例如,

case class TestContext(name: Option[String] = None)

object TestContext {
  val currentTest: ThreadLocal[TestContext] = ThreadLocal.withInitial(() => TestContext())

  class TestNamer {
    def currentName: String = currentTest.get().name match {
      case Some(testname) => testname
      case None => throw new IllegalStateException("No test context available")
    }
  }

  class TestContextWriter(testNamer: TestNamer = new TestNamer()) {
    def cachedBytes(bytes: Array[Byte], extension: String): Array[Byte] = {
      import java.io.{BufferedOutputStream, File, FileOutputStream}
      import scala.util.Using

      val file = new File("target", s"${testNamer.currentName}.${extension}")

      Using(new BufferedOutputStream(new FileOutputStream(file))) { outstream =>
        outstream.write(bytes)
      }

      bytes
    }
  }
}

并根据需要注入:

trait BaseFunSpec {
  val testContextWriter = new TestContextWriter()

  def fetchRawResults(...): Array[Byte] = {
    ...
    testContextWriter.cachedBytes(bytes, "pdf")
  }
}

答案 4 :(得分:0)

您可以根据需要使用 BeforeAndAfterEachTestData

如果您需要在 beforeEach 或 afterEach 方法中访问测试用例名称。

class MyTestSuite with AnyFunSuiteLike with BeforeAndAfterEachTestData {

    override def beforeEach(testData: TestData): Unit = {
        testData.name // do whatever.
        super.beforeEach(testData)
    }
}

如果你需要访问测试用例本身中的测试用例名称,那么你可以使用线程本地方法

private val currentTestCaseName = new ThreadLocal[String]

override def beforeEach(testData: TestData): Unit = {
    currentTestCaseName.set(testData.name)
    super.beforeEach(testData)
}

test("fancy test") {
    currentTestCaseName.get() // do whatever
}