SBT 中的基本测试功能
如何使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 SBT 进行测试
无论您要使用哪个版本的 SBT,基本上您都必须执行以下步骤:
这些基本步骤对于 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 文件夹并创建一个新的构建文件夹
以下内容创建您的第一个项目构建文件 Project.scala:
更改为新创建的构建文件夹并使用 对于 0.7,测试文件夹是自动创建的,然后您可以立即开始编写您的第一个测试。进入“创建一个简单测试”段落。
定义 SBT 0.10 的依赖项
对于 0.10,可以使用 sbt 控制台添加依赖项。只需在项目目录中启动 sbt 并输入以下命令:
然后您可以关闭 sbt 控制台并查看您的项目 build.sbt 文件。您可以轻松地发现上面的libraryDependencies行已添加到您的项目快速配置中。
由于 0.10 不会自动创建源文件夹。您必须自己创建测试文件夹:
这就是开始使用 0.10 所需的全部内容。此外,关于使用 0.10 进行测试的文档比旧版本要详细得多。有关更多详细信息,请参阅测试 wiki 页面。
创建一个简单的 scalacheck 测试
创建以下测试文件src/test/scala/StringSpecification.scala(取自 scalacheck 主页):
如前所述,此基本检查将失败“concat”规范,但这是开始测试和 sbt 所需的基本测试步骤。如果您想使用其他测试框架,只需调整包含的依赖项即可。
运行测试
要运行测试,请打开 sbt 控制台并输入
这将运行 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:
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
change into the newly created build folder and create your first project build file Project.scala with the follwing content:
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:
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:
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):
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
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.