【问题标题】:ScalaTest: setup/tearDown methods for only selected testsScalaTest:仅用于选定测试的 setup/tearDown 方法
【发布时间】:2020-06-23 12:26:54
【问题描述】:

我知道BeforeAndAfter trait 允许我们在每次测试套件之前和之后执行设置和拆卸操作。

是否有仅在套件前后运行或针对选定测试运行的替代方案?

【问题讨论】:

    标签: scala scalatest


    【解决方案1】:

    关于特定测试之前和之后考虑loan pattern

    import org.scalactic.Equality
    import org.scalatest.flatspec.AnyFlatSpec
    import org.scalatest.matchers.should.Matchers
    
    class LoanPatternExampleSpec extends AnyFlatSpec with Matchers {
      def life(testFun: Int => Any): Unit = {
        println("Doing stuff before the test")
        val meaningOfLife = 42
        testFun(meaningOfLife)
        println("Doing stuff after the test")
      }
    
      "User" should "do things" in life { i =>
        println(s"My fixture says meaning of life is $i")
        assert(true)
      }
    }
    

    哪个输出

    sbt:scalatest-seed> testOnly example.LoanPatternExampleSpec
    Doing stuff before the test
    My fixture says meaning of life is 42
    Doing stuff after the test
    [info] LoanPatternExampleSpec:
    [info] User
    [info] - should do things
    [info] Run completed in 314 milliseconds.
    [info] Total number of tests run: 1
    [info] Suites: completed 1, aborted 0
    [info] Tests: succeeded 1, failed 0, canceled 0, ignored 0, pending 0
    [info] All tests passed.
    

    BeforeAndAfterAll 覆盖了在套件前后执行一次的方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-13
      • 1970-01-01
      • 1970-01-01
      • 2011-10-14
      • 1970-01-01
      • 2015-04-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多