12

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

背景:

我正在测试我的数据访问对象OverQuotaException是否最终会抛出一个用户,例如创建太多页面。这些测试需要很长时间才能运行。为了更开心,我想将进度打印到标准输出——因为有很多测试,我想在输出中包含测试名称,这样我就知道当前正在运行什么测试。

(我在这里没有找到任何看似相关的功能: 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?
      }
4

5 回答 5

13

这样做的预期方法是覆盖 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")
    }
  }
}
于 2013-02-12T16:08:39.540 回答
3

创造你自己的特质,比方说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 => 
   ...
 }

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

于 2013-02-12T13:59:30.513 回答
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)
  }
}
于 2013-02-12T15:40:24.697 回答
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")
  }
}
于 2021-02-14T17:08:55.240 回答
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
}
于 2021-06-01T10:47:36.040 回答