gpt4 book ai didi

scala - 何时在具有 BeforeAndAfter 特性的 ScalaTest 类中调用 before() 和 after()

转载 作者:行者123 更新时间:2023-12-02 15:02:06 25 4
gpt4 key购买 nike

考虑以下用于 BeforeAndAfterBeforeAndAfterAll 的最小可行自包含测试用例:

import org.scalatest.{BeforeAndAfter, BeforeAndAfterAll, FunSuite}

class BeforeAndAfterTestTest extends FunSuite with BeforeAndAfter with BeforeAndAfterAll {

override protected def beforeAll(): Unit = println("beforeAll")

override protected def afterAll(): Unit = println("afterAll")

override protected def before(fun: => Any)(implicit pos: Position): Unit = {
println("before")
}

override protected def after(fun: => Any)(implicit pos: Position): Unit = {
println("after")
}

test("hello1") { println("hello1") }

test("hello2") { println("hello2") }
}

通过 scalatest 运行的结果是:

enter image description here

所以:

  • before/afterAll do 执行
  • 之前/之后

调用 beforeafter 方法需要什么?

最佳答案

您应该调用之前之后,而不是覆盖它们:

import org.scalatest.{BeforeAndAfter, BeforeAndAfterAll, FunSuite}

class BeforeAndAfterTestTest extends FunSuite with BeforeAndAfter with BeforeAndAfterAll {

override protected def beforeAll(): Unit = println("beforeAll")

override protected def afterAll(): Unit = println("afterAll")

before {
println("before")
}

after {
println("after")
}

test("hello1") { println("hello1") }

test("hello2") { println("hello2") }
}

参见文档 here

如果你想要可覆盖的方法,你应该使用BeforeAndAfterEach,而不是BeforeAndAfter (doc)

关于scala - 何时在具有 BeforeAndAfter 特性的 ScalaTest 类中调用 before() 和 after(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48753284/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com