使用“sbt testOnly"从 jar 运行测试在 SBT? [英] Running tests from jar with "sbt testOnly" in SBT?

查看:39
本文介绍了使用“sbt testOnly"从 jar 运行测试在 SBT?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为 CI 设置的一部分,第一步是使用 SBT dist 创建包/jar.以下步骤是使用 dist 创建的 jar 运行单元、集成和功能测试.

As part of a CI setup the very first step is to create a package/jar using SBT dist. The following steps are to run unit, integration and functional tests with the dist-created jar.

SBT 可以做到吗?

通常我使用 sbt testOnly "Unit.*",但这适用于项目上下文.当已有 jar 时,我找不到任何说明如何执行此操作的文档.

Normally I use sbt testOnly "Unit.*", but that works in the context of a project. I can't find any documentation showing how to do this when there is already a jar.

我正在使用 ScalaTest,我知道有一个运行器可以使用 http://www.scalatest.org/user_guide/using_the_runner.但如果可能的话,使用 SBT 会更简单.

I'm using ScalaTest and I know there is a runner for it I could use http://www.scalatest.org/user_guide/using_the_runner. But using SBT would be simpler, if that is possible.

举个例子,我正在寻找这样的东西:

As an example, something like this is what I am looking for:

sbt testOnly "Unit.* -jar myjar.jar"

当我使用以下内容时,我的测试甚至会包含在 jar 中:

Will my tests even be included in the jar when I use the following:

sbt dist

?

编辑

  1. 我创建了一个新文件夹
  2. 我添加了具有以下内容的 build.sbt:

name := "abc"

version := "1.0-SNAPSHOT"

scalaVersion := "2.10.0"

  • 我添加了 lib 文件夹并将我的测试 jar 复制到其中

  • I added the lib folder and copied my tests jar into it

    我运行了 sbt testOnly Unit.*

    找不到任何测试

    编辑 2

    我尝试使用以下正确的"SBT 文件:

    I tried with the following "proper" SBT file:

    name := "ihs2tests"
    
    version := "1.0-SNAPSHOT"
    
    scalaVersion := "2.10.0"
    
    unmanagedBase in Test := new java.io.File(".")
    

    并将 test.jar 移动到项目的根目录中.同样,未找到任何测试.

    and moved test.jar into the root of the project. Again, no tests found.

    推荐答案

    通常我使用 sbt testOnly "Unit.*",但这在项目上下文中有效.当已有 jar 时,我找不到任何说明如何执行此操作的文档.

    Normally I use sbt testOnly "Unit.*", but that works in the context of a project. I can't find any documentation showing how to do this when there is already a jar.

    SBT 中的 test 系列任务(以 testOnly 为例)与 compile 任务一起使用,该任务通过以下方式返回已编译文件的列表sbt.inc.Analysis 实例.我不知道如何修改它并将更改后的 Analysis 实例注入到 testOnly 以便它知道我要运行的测试存在.

    The test-family tasks in SBT (with testOnly as an example) work with compile task that returns a list of compiled files through an sbt.inc.Analysis instance. I couldn't figure out how to amend it and inject the changed Analysis instance to testOnly so it knows the test I'm going to run exists.

    我提出了另一种解决方案 - 技巧.

    I'm proposing another solution - a trick.

    使用 test:package 任务将测试类打包到 jar 中,如下所示:

    Package the tests classes to a jar with test:package task as follows:

    [test-lib]> test:package
    [info] Updating {file:/Users/jacek/sandbox/so/test-lib/}test-lib...
    [info] Resolving org.fusesource.jansi#jansi;1.4 ...
    [info] Done updating.
    [info] Compiling 1 Scala source to /Users/jacek/sandbox/so/test-lib/target/scala-2.10/test-classes...
    [info] Packaging /Users/jacek/sandbox/so/test-lib/target/scala-2.10/test-lib_2.10-0.1-SNAPSHOT-tests.jar ...
    [info] Done packaging.
    [success] Total time: 9 s, completed Mar 4, 2014 11:34:13 PM
    

    当你有测试 jar 时,你可以在命令行上执行测试框架没有 SBT(我假设你使用 ScalaTest 给定 标签,但我会使用 Specs2).阅读有关如何执行此操作的测试框架文档,对于 Specs2,它是 specs2.run,如 控制台输出.

    When you have the test jar, you can execute the test framework on the command line without SBT (I assume you use ScalaTest given the scalatest tag, but I'll use Specs2). Read the documentation of the test framework on how to do it and for Specs2 it's specs2.run as described in Console output.

    从测试 jar 执行测试需要定义一个正确的 CLASSPATH,这可能容易也可能不容易正确.这就是 SBT 可以提供很大帮助的地方 - 管理依赖项,从而管理 CLASSPATH.

    Executing tests from the test jar requires defining a proper CLASSPATH that may or may not be easy to get right. That's where SBT can be of great help - to manage dependencies and hence the CLASSPATH.

    创建另一个 SBT 项目并将测试 jar 保存在 lib 子目录中以将其放在 CLASSPATH(如 非托管依赖项).

    Create another SBT project and save the test jar in lib subdirectory to have it on CLASSPATH (as described in Unmanaged dependencies).

    lib 中的依赖项在所有类路径上(用于编译、测试、运行和控制台).

    Dependencies in lib go on all the classpaths (for compile, test, run, and console).

    添加示例 build.sbt,您可以在其中将测试框架定义为项目的依赖项.对于 Specs2,它如下(我使用了 Specs2 主页 中描述的默认配置):

    Add a sample build.sbt where you define your test framework as a dependency of the project. For Specs2 it's as follows (I used the default configuration as described in the Specs2 home page):

    libraryDependencies += "org.specs2" %% "specs2" % "2.3.8" % "test"
    
    scalacOptions in Test ++= Seq("-Yrangepos")
    

    诀窍是执行测试框架的主类,例如specs2.run 用于 Specs2,就像在命令行上执行类一样.SBT 有助于 test:runMain.

    The trick is to execute the main class of the test framework, e.g. specs2.run for Specs2, as if the class were executed on the command line. SBT helps with test:runMain.

    [my-another-project]> test:runMain specs2.run HelloWorldSpec
    [info] Running specs2.run HelloWorldSpec
    HelloWorldSpec
    
    The 'Hello world' string should
    + contain 11 characters
    + start with 'Hello'
    + end with 'world'
    
    Total for specification HelloWorldSpec
    Finished in 62 ms
    3 examples, 0 failure, 0 error
    
    Exception: sbt.TrapExitSecurityException thrown from the UncaughtExceptionHandler in thread "run-main-0"
    [success] Total time: 5 s, completed Mar 4, 2014 11:15:14 PM
    

    不要担心这个 Exception,因为它来自 SBT,它从 Specs2(测试执行后)捕获 exit,因此 SBT 保持正常运行.

    Don't worry about this Exception as it comes from SBT that catches exit from Specs2 (after test execution) so SBT remains up.

    这篇关于使用“sbt testOnly"从 jar 运行测试在 SBT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆