SBT 中的基本测试功能

发布于 2024-11-13 06:59:11 字数 459 浏览 2 评论 0原文

如何使用 SBT 的测试功能为我的应用程序创建简单的单元测试?

我希望答案是我可以在 src/test/scala 中为我的项目编写一个文件,该文件从 SBT 导入一些特殊的测试包,这使得编写测试就像编写单个方法一样简单。

该教程ExampleSbtTest似乎是做比我需要的更复杂的事情,并且我在 SBT 上找不到任何更简单的东西GoogleCode 页面。

How do I create a simple unit test for my application using SBT's test feature?

I'm hoping the answer is that I can write a single file in src/test/scala for my project that imports some special testing package from SBT which makes writing tests as easy as writing a single method.

The tutorial ExampleSbtTest seems to be doing something more complicated than what I need, and I can't find anything simpler on the SBT GoogleCode page.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

多情癖 2024-11-20 06:59:11

使用 SBT 进行测试

无论您要使用哪个版本的 SBT,基本上您都必须执行以下步骤:

  1. 在项目配置中包含您所需的测试框架作为测试依赖项。
  2. 在源代码树中创建一个专用的测试文件夹,通常是 src/test/scala(如果尚不存在)。
  3. 一如既往:编写你的测试、规格……

这些基本步骤对于 sbt 0.7 分支(来自 google-code 的分支)和当前的 sbt 0.10 分支(现在在 github 上开发和记录)是相同的。然而,由于 0.10 提供了 0.7 中没有的新的快速配置方法,因此在定义测试依赖项方面存在细微差别。

定义 SBT 0.7 的依赖项

以下是如何使用 sbt 0.7 创建基本测试(基于 scalacheck)。通过在空文件夹中调用 sbt 创建一个新的 sbt 0.7 项目。更改为自动创建的 project 文件夹并创建一个新的构建文件夹

# cd [your-project-root]/project
# mkdir build

以下内容创建您的第一个项目构建文件 Project.scala

class Project(info: ProjectInfo) extends DefaultProject(info) {

    val scalacheck = "org.scala-tools.testing" %% "scalacheck" % "1.9" % "test"

}

更改为新创建的构建文件夹并使用 对于 0.7,测试文件夹是自动创建的,然后您可以立即开始编写您的第一个测试。进入“创建一个简单测试”段落。

定义 SBT 0.10 的依赖项

对于 0.10,可以使用 sbt 控制台添加依赖项。只需在项目目录中启动 sbt 并输入以下命令:

设置库依赖项+=“org.scala-tools.testing”%%“scalacheck”%“1.9”%“test”
会话保存

然后您可以关闭 sbt 控制台并查看您的项目 build.sbt 文件。您可以轻松地发现上面的libraryDependencies行已添加到您的项目快速配置中。

由于 0.10 不会自动创建源文件夹。您必须自己创建测试文件夹:

# cd [project-root]
# mkdir -p src/test/scala

这就是开始使用 0.10 所需的全部内容。此外,关于使用 0.10 进行测试的文档比旧版本要详细得多。有关更多详细信息,请参阅测试 wiki 页面。

创建一个简单的 scalacheck 测试

创建以下测试文件src/test/scala/StringSpecification.scala(取自 scalacheck 主页):


import org.scalacheck._

object StringSpecification extends Properties("String") {
   property("startsWith") = Prop.forAll((a: String, b: String) => (a+b).startsWith(a))

   property("endsWith") = Prop.forAll((a: String, b: String) => (a+b).endsWith(b))

    // Is this really always true?
    property("concat") = Prop.forAll((a: String, b: String) => 
        (a+b).length > a.length && (a+b).length > b.length
    )

    property("substring") = Prop.forAll((a: String, b: String) => 
        (a+b).substring(a.length) == b
    )

    property("substring") = Prop.forAll((a: String, b: String, c: String) =>
        (a+b+c).substring(a.length, a.length+b.length) == b
    )
}

如前所述,此基本检查将失败“concat”规范,但这是开始测试和 sbt 所需的基本测试步骤。如果您想使用其他测试框架,只需调整包含的依赖项即可。

运行测试

要运行测试,请打开 sbt 控制台并输入

> test

这将运行 src/test 树中存在的所有测试,无论这些测试是基于 java 还是 scala 。因此您可以轻松地重用现有的 java 单元测试并将其逐步转换为 scala。

Testing with SBT

No matter which version of SBT you want to use, basically you have to do the following steps:

  1. Include your desired testing framework as test-dependency in your project configuration.
  2. Create a dedicated testing folder within your source tree, usually src/test/scala, if it isn't present already.
  3. As always: Write your tests, specs ...

Those basic steps are identical for the sbt 0.7 branch (that's the one from google-code) and the current sbt 0.10 branch (now developed and documented on github). However, there are minor differences how to define the testing dependencies since 0.10 provides a new quick configuration method not present in 0.7.

Defining the dependency for SBT 0.7

Here is how you create a basic test (based on scalacheck) with sbt 0.7. Create a new sbt 0.7 project by calling sbt in an empty folder. Change into the automatically created project folder and create a new build folder

# cd [your-project-root]/project
# mkdir build

change into the newly created build folder and create your first project build file Project.scala with the follwing content:

class Project(info: ProjectInfo) extends DefaultProject(info) {

    val scalacheck = "org.scala-tools.testing" %% "scalacheck" % "1.9" % "test"

}

Since for 0.7 the testing folder is created automatically you can then start to write your first test right away. Step to the paragraph "Create a simple test".

Defining the dependency for SBT 0.10

For 0.10 one can use the sbt console to add the dependency. Just start sbt in your project directory and enter the following commands:

set libraryDependencies += "org.scala-tools.testing" %% "scalacheck" % "1.9" % "test"
session save

You can then close the sbt console and have a look at your projects build.sbt file. As you can easily spot the above libraryDependencies line was added to your projects quick configuration.

Since 0.10 doesn't create the source folders automatically. You have to create the testing folder on your own:

# cd [project-root]
# mkdir -p src/test/scala

That's all it takes to get started with 0.10. Moreover, the documentation about testing with 0.10 is far more detailed then the old one. See the testing wiki page for further details.

Create a simple scalacheck test

Create the following test file src/test/scala/StringSpecification.scala (taken from the scalacheck homepage):


import org.scalacheck._

object StringSpecification extends Properties("String") {
   property("startsWith") = Prop.forAll((a: String, b: String) => (a+b).startsWith(a))

   property("endsWith") = Prop.forAll((a: String, b: String) => (a+b).endsWith(b))

    // Is this really always true?
    property("concat") = Prop.forAll((a: String, b: String) => 
        (a+b).length > a.length && (a+b).length > b.length
    )

    property("substring") = Prop.forAll((a: String, b: String) => 
        (a+b).substring(a.length) == b
    )

    property("substring") = Prop.forAll((a: String, b: String, c: String) =>
        (a+b+c).substring(a.length, a.length+b.length) == b
    )
}

As already indicated this basic check will fail for the "concat" specification, but that are the basic testing steps needed to get started with testing and sbt. Just adapt the included dependency if you want to use another testing framework.

Run your tests

To run your test, open the sbt console and type

> test

That will run all tests present in your src/test tree no matter if those are java or scala based tests. So you can easily reuse your existing java unit tests and convert them step by step to scala.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文