【发布时间】:2014-04-14 09:36:24
【问题描述】:
我正在使用 Project Euler 来学习 Scala 中的函数式编程,以及 sbt 和其他最佳实践。我最初尝试使用ScalaTest 创建测试以验证解决方案是:
package euler
import org.scalatest._
class SolutionsSpec extends Spec with Matchers {
"The problems" should "have the correct solutions" in {
Problem1.solve should be ( 233168 )
Problem2.solve should be ( 4613732 )
Problem3.solve should be ( 6857 )
Problem4.solve should be ( 906609 )
}
}
这很简洁,但我担心如果我遇到运行时间更长的更难的问题,它可能无法扩展。据我所知,我无法以这种方式测试个别问题。
我在 GitHub 上进行了搜索,发现了两个有用的示例,类似于我正在尝试做的:
两者都比我想要的更详细,因为我基本上只是在测试从问题编号到解决方案的映射。
我的问题:是否有一种简洁而优雅的方式来执行此测试,并且仍然灵活,以便我可以检查单个或一组问题的结果?
编辑:我发现 this answer 不鼓励使用 Project Euler 进行 TDD。为了澄清,我也在为数学辅助函数编写单元测试。我检查答案的目的部分是为了检查回归,但最重要的是作为练习。
编辑 2:我现在正在尝试使用 FunSuite:
class SolutionsSuite extends FunSuite with Matchers {
test("Problem 1") { Problem1.solve should be ( 233168 ) }
test("Problem 2") { Problem2.solve should be ( 4613732 ) }
test("Problem 3") { Problem3.solve should be ( 6857 ) }
test("Problem 4") { Problem4.solve should be ( 906609 ) }
}
这可能更接近我想要的。输出看起来更好:
$ sbt test
...
[info] SolutionsSuite:
[info] - Problem 1 (28 milliseconds)
[info] - Problem 2 (4 milliseconds)
[info] - Problem 3 (38 milliseconds)
[info] - Problem 4 (172 milliseconds)
但我仍然无法弄清楚如何运行问题的子集。这个问题似乎表明我正在寻找的东西还不能用 sbt:Run just a specific scalatest test from sbt。
编辑 3:看起来运行单个测试功能而不是整个套件的能力是针对每个问题 Allow running of single junit test #911 的 sbt 0.13.3。
编辑 4:供参考,my Project Euler GitHub project。
【问题讨论】:
标签: scala unit-testing sbt scalatest