我有戏!我想在其中添加一些代码覆盖率信息的项目。到目前为止,我已经尝试过 JaCoCo和 scct .前者存在基于字节码的问题,因此它似乎对 Scala 编译器自动生成的方法缺少测试给出警告,例如 copy
或 canEqual
. scct 似乎是一个更好的选择,但无论如何我在测试期间都会遇到很多错误。
让我坚持使用 scct。对于尝试连接到数据库的每个测试,我基本上都会出错。我的许多测试将一些装置加载到内存中的 H2 数据库中,然后进行一些断言。我的 Global.scala
包含
override def onStart(app: Application) {
SessionFactory.concreteFactory = Some(() => connection)
def connection() = {
Session.create(DB.getConnection()(app), new MySQLInnoDBAdapter)
}
}
而测试通常包含在一个块中
class MySpec extends Specification {
def app = FakeApplication(additionalConfiguration = inMemoryDatabase())
"The models" should {
"be five" in running(app) {
Fixtures.load()
MyModels.all.size should be_==(5)
}
}
}
线路
running(app)
允许我在连接到内存数据库的工作应用程序的上下文中运行测试,至少通常是这样。但是当我运行代码覆盖任务时,比如 scct coverage:doc
, 我得到一个 拍品 与连接到数据库相关的错误。更奇怪的是,至少有 4 个不同的错误,例如:
Why is that launching tests in the default configuration is able to connect to the database, while running in the context of scct (or JaCoCo) fails to initialize the cache and the db?
最佳答案
specs2 测试默认并行运行。 Play 禁用标准单元测试配置的并行执行,但 scct 使用不同的配置,因此它不知道不并行运行。
尝试将其添加到您的 Build.scala 中:
.settings(parallelExecution in ScctPlugin.ScctTest := false)
或者,您可以添加
sequential
到测试类的开头,以强制所有可能的运行配置按顺序运行。我的文件中仍然有两个,因为我认为当我使用 Play 的早期发布候选版本时,我曾在 Build.scala 解决方案上遇到了一些问题。
关于scala - Play 上的代码覆盖率!项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14505229/