gpt4 book ai didi

scala - 是否有 scalaTest 方法或属性可用于在所有套件中的所有测试之后仅运行一次

转载 作者:行者123 更新时间:2023-12-01 11:18:20 24 4
gpt4 key购买 nike

我有一组测试套件也会增长,我需要运行一个测试清理任务,该任务应该只在所有测试执行完毕之后和测试进程退出之前运行一次。这类似于 .NET AssebmlyCleanup,但我无法在 Scala/Scalatest 世界中找到等效的东西而无需执行自定义代码,是吗?

谢谢

最佳答案

我一直在思考如何解决这个问题,其中一种方法是在我们的 build.sbt testtestOnly。所以假设我们在 src/test/scala 下面有两个套件:

class Suite1 extends FlatSpec{
"Test1 in Suite1" should "succeed" in{
succeed
}
}

class Suite2 extends FlatSpec{
"Test1 in Suite2" should "succeed" in{
succeed
}
}

现在让我们在 /project/ 文件夹下添加一个 CleanUp.scala 对象,我们的清理将在其中进行:

object CleanUp{
def cleanUp:Unit = println("Cleaning up after all suites are completed.")
}

这是一个最小的例子,实际上你可能有你需要的任何复杂的清理。现在在我们的 build.sbt 中添加以下内容:

(test in Test) := {
val testsResult = (test in Test).value
CleanUp.cleanUp
testsResult
}

(testOnly in Test) := {
(testOnly in Test).evaluated
CleanUp.cleanUp
}

这会覆盖 testtestOnly 任务的默认行为,因此将在执行所有套件(或用户套件指定的所有套件)后应用清理。

例如,这是我对新的 testOnly 的 sbt 控制台检查:

[IJ]sbt:AfterAllTests> testOnly Suite1
[info] Suite1:
[info] Test1 in Suite1
[info] - should succeed
[info] Run completed in 150 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.
Cleaning up after all suites are completed.
[success] Total time: 0 s, completed Dec 2, 2017 12:19:48 AM
[IJ]sbt:AfterAllTests>

这里是新测试的检查:

[IJ]sbt:AfterAllTests> test
[info] Suite2:
[info] Test1 in Suite2
[info] - should succeed
[info] Suite1:
[info] Test1 in Suite1
[info] - should succeed
[info] Run completed in 164 milliseconds.
[info] Total number of tests run: 2
[info] Suites: completed 2, aborted 0
[info] Tests: succeeded 2, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
Cleaning up after all suites are completed.
[success] Total time: 2 s, completed Dec 2, 2017 12:28:25 AM
[IJ]sbt:AfterAllTests>

如您所见,清除被调用。希望这会有所帮助。

关于scala - 是否有 scalaTest 方法或属性可用于在所有套件中的所有测试之后仅运行一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47601464/

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