简体   繁体   English

SBT清理钩子在测试中

[英]SBT cleanup hook in test

SBT has a nice hook which allows you to execute arbitrary code after all tests are run: SBT有一个很好的钩子,允许你在所有测试运行后执行任意代码:

testOptions in Test += Tests.Cleanup( () => println("Cleanup"))

That works. 这样可行。 My question is: I want to do some actual cleanup (stopping some services for example) but I can't import any dependencies which I've declared in the same build file. 我的问题是:我想做一些实际的清理(例如停止一些服务)但我无法导入我在同一个构建文件中声明的任何依赖项。 Is there any way to do this? 有没有办法做到这一点? I guess I need to put these on the sbt classpath or something, but I can't seem to find this in the docs. 我想我需要将这些放在sbt类路径或其他东西上,但我似乎无法在文档中找到它。

PS I might be doing this in the wrong location, is there a better place to shutdown things after all tests are run?) PS我可能在错误的位置执行此操作,是否有更好的地方在所有测试运行后关闭事物?)

Complementing venechka's answer: I'm running integration tests using Specs2, and in specs there is no way of knowing when all tests have run. 补充了venechka的答案:我正在使用Specs2运行集成测试,并且在规格中无法知道所有测试何时运行。 So I solved it pretty much the way venechka and you yourself already indicated, by loading a class from the project that does the cleanup when it's initialized: 因此,我通过从项目中加载一个初始化时执行清理的类来解决它,就像venechka和你自己已经指出的那样:


testOptions in IntegrationTest += Tests.Cleanup( (loader: java.lang.ClassLoader) => {
  loader.loadClass("com.mypackage.IntegrationTestCleanup").newInstance
} )

You can't use the classes that are added with libraryDependencies in project (you can add libraryDependencies in project/project, but I wouldn't recommend adding in 2 places). 您不能使用在项目中添加了libraryDependencies的类(您可以在项目/项目中添加libraryDependencies,但我不建议在2个位置添加)。 Instead you can invoke a cleanup method that is in your project's sources, and that has access to the declared library dependencies. 相反,您可以调用项目源中的清理方法,并且可以访问声明的库依赖项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM