从内部测试访问ScalaTest测试名称? [英] Access ScalaTest test name from inside test?

查看:96
本文介绍了从内部测试访问ScalaTest测试名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is it possible to access the name of the currently executing test, from within a ScalaTest test? (And how would I do it?)

背景:

我正在测试我的数据访问对象最终是否会在用户(例如用户)抛出OverQuotaException的情况下抛出OverQuotaException.创建太多页面.这些测试需要相当长的时间才能运行.为了感到更开心,我想将进度打印到stdout上,并且由于有很多测试,我想在输出中包含测试名称,因此我知道当前正在运行哪个测试.

I'm testing that my Data Access Object eventually throws an OverQuotaException if a user e.g. creates too many pages. These tests take rather long to run. To feel happier, I'd like to print the progress to stdout — and since there are quite many tests, I'd like to include the test name in the output, so I know what test is currently being run.

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

(I didn't find any seemingly relevant function here: 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?
      }

推荐答案

实现此目的的预期方法是使用Fixture覆盖并捕获测试数据.在这种用例中,最好在Fixture.FreeSpec中重写withFixture,这样您就可以将测试数据传递到每个测试中,而不是使用var.信息在这里:

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

今天早上当我看到您的问题时,我意识到ScalaTest应该具有执行此任务的特征,因此我只添加了一个.它将在下一个里程碑版本2.0.M6中发布,但与此同时,您可以使用本地副本.在这里:

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

这篇关于从内部测试访问ScalaTest测试名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆