正如标题所示,我有一个只有一个模块的 sbt 项目(目前),但test:compile没有捕获任何语法错误(并且 test 没有找到任何要运行的测试)。据我理解,sbt 项目带有 src/main 和 src/test 设置(src/it 需要配置)。我对 SBT 有点业余,但我会尽力提供所有相关信息:
结构:
Root
build.sbt
mymodule
build.sbt
src
main
scala
test
scala
Run Code Online (Sandbox Code Playgroud)
Root build.sbt(我认为这些配置是不必要的,但我很绝望):
lazy val `mymodule` = (project in file("mymodule"))
.configs(Test)
.settings(scalaSource in Test := baseDirectory.value / "test")
Run Code Online (Sandbox Code Playgroud)
MyModule build.sbt 基本上只是一个库依赖项列表(除非其中一个是问题所在,不确定),如下所示:
libraryDependencies ++= Seq(
"org.mockito" % "mockito-all" % "1.9.5" % "test",
"org.scalamock" %% "scalamock-core" % scalaMockVersion % "test",
"org.scalamock" %% "scalamock-scalatest-support" % scalaMockVersion % "test",
"org.codehaus.janino" % "janino" % "2.7.8",
"org.http4s" %% "http4s-dsl" % "0.11.2",
"org.http4s" …Run Code Online (Sandbox Code Playgroud) 我们有一个包含多个测试套件的大项目,每个测试套件平均有 3 个测试。
\n\n对于我们的单元测试,我们使用 Spark Standalone,因此没有 Yarn 作为资源管理器。\n每个测试套件:
\n\n启动 Spark 会话:
\n\n implicit val spark = SparkSession\n .builder()\n .config(sparkConf)\n .getOrCreate()\nRun Code Online (Sandbox Code Playgroud)\n\n延伸BeforeAndAfterAll:
class MyTestsSpec extends WordSpec\n with Matchers\n with BeforeAndAfterAll {\n...\n}\nRun Code Online (Sandbox Code Playgroud)\n\n并重新定义 afterAll :
\n\n override def afterAll: Unit = {\n try {\n spark.stop()\n } finally {\n super.afterAll\n }\n }\nRun Code Online (Sandbox Code Playgroud)\n\n我们的解决方案在 Jenkins 中有一个 CI 作业,并且 Jenkins 作业开始经常不稳定,因为测试因以下错误而失败:
\n\nMessage d'erreur\nJob 9 cancelled because SparkContext was shut down\nPile d'ex\xc3\xa9cution\norg.apache.spark.SparkException: Job 9 cancelled …Run Code Online (Sandbox Code Playgroud) 在 ScalaTest 中,我通过以下方式未通过测试:
fail("message")
Run Code Online (Sandbox Code Playgroud)
它仅以红色突出显示消息,但不打印堆栈跟踪。
我不得不采取一种非常尴尬的方式,例如:
try { throw new Exception } catch { case e =>
e.printStackTrace()
fail("message")
}
Run Code Online (Sandbox Code Playgroud)
获取堆栈跟踪。
我尝试这样做fail("message", new Exception),但它也没有打印。有没有更好的方法来做到这一点?
我有一堆由 sbt 运行的集成测试,给定了 N 个测试套件,每个套件有 1..M 个测试。我已经设置了fork in IntegrationTest := true,但测试套件总是按顺序执行。根据文档,情况不能是这样:测试套件应该同时执行。
测试套件是一个类,如下所示:
class MyTestSuite1 extends FlatSpec with Matchers
...
it should "do A" {}
it should "do B" {}
Run Code Online (Sandbox Code Playgroud)
class MyTestSuite2 extends FlatSpec with Matchers
...
it should "do C" {}
it should "do D" {}
Run Code Online (Sandbox Code Playgroud)
MyTestSuite1 和 MyTestSuiteN 按顺序执行(准确地说按字母顺序)
MyTestSuite1 和 MyTestSuiteM 并发执行
.sbots:
-J-Xms1G
-J-Xmx4G
-J-XX:MaxMetaspaceSize=512m
-J-Xss4M
Run Code Online (Sandbox Code Playgroud)
我注意到所有测试都使用相同的池和线程运行,例如,pool-1-thread-1对于所有测试。
sbt 版本:1.2.8 Scala:2.12.8 操作系统:MacOS 10.15、Ubuntu 19.04 Scalatest 版本:3.2.0-SNAP10
尝试过 sbt v.1.3.2 - …
我正在尝试使用scalatest和mockito来模拟RabbitMQ ConnectionFactory对象以返回模拟连接.以下是我正在使用的示例测试:
class RabbitMQMockTest extends FunSuite with MockitoSugar {
test("RabbitMQ ConnectionFactory is correctly mocked") {
def connectionFactory = mock[ConnectionFactory]
def connection = mock[Connection]
when(connectionFactory.newConnection()).thenReturn(connection)
println(connectionFactory.newConnection())
assert(connectionFactory.newConnection() != null)
}
}
Run Code Online (Sandbox Code Playgroud)
这总是失败,println语句总是打印"null".我一起使用这些技术非常新,并且想知道是否有人有任何建议或者如果我做错了什么我可以告诉我.提前致谢!
从Specs2迁移到Scalatest我尝试使用WordSpec,但没有运气.我已经使用了来自Testing Actor Systems的示例,但是对于我来说错误并不适合我.然后我从scaladoc复制了基本测试,仍然有同样的问题.你能指导我,我在这里做错了什么:
class MasterSpec extends WordSpec {
"A Set" when {
"empty" should {
"have size 0" in (pending)
"produce NoSuchElementException when head is invoked" in {
intercept[NoSuchElementException] {
Set.empty.head
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这给出以下错误消息:
[error] /Users/bam/Projects/ppm/core/src/test/scala/net/batyuk/ppm/core/MasterSpec.scala:12: overloaded method value in with alternatives:
[error] (testFun: () => Any)Unit <and>
[error] (testFun: MasterSpec.this.FixtureParam => Any)Unit
[error] cannot be applied to (org.scalatest.PendingNothing)
[error] "have size 0" in (pending)
[error] ^
[error] one error found
Run Code Online (Sandbox Code Playgroud)
试图转移到FunSpec,但不能强迫自己进入它,WordSpec对我来说似乎更自然
我下载了Scala IDE Linux - 64位For Scala 2.11.2,我正在尝试运行Scala测试.以下是我的代码.
package ppg.experiment.gameofbusiness.engine
import org.scalatest.FlatSpec
import org.scalatest.Matchers
class DiceSpec extends FlatSpec with Matchers {
"A dice" should "roll a value greater than zero" in {
new Dice().roll > 0
}
it should "roll a value less than six" in {
new Dice().roll < 7
}
}
Run Code Online (Sandbox Code Playgroud)
当我右键单击并以Scala Test运行时,控制台上会打印以下内容
WARNING: -p has been deprecated and will be reused for a different (but still very cool) purpose in ScalaTest 2.0. Please change all uses …Run Code Online (Sandbox Code Playgroud) 我知道这可能会遗漏一些非常明显的东西,但我似乎无法模拟出一个返回Try [T]的方法
简化示例:
case class User (first: String, last: String)
// ...
// in Repository class:
def update[T](id: Int): Try[T] = { ... }
// ...
// in scalatest test using scalamock:
test("a bad id should return a failed update") {
val mockRepo = mock[Repository]
(mockRepo.update[User] _).expects(13).returns(???)
val result = mockRepo.update[User](13)
result.isSuccess should be (false)
}
Run Code Online (Sandbox Code Playgroud)
如何从模拟方法返回失败的Try [T]?
要测试我的代码是否抛出预期的异常,我使用以下语法:
an [IllegalArgumentException] should be thrownBy(Algorithms.create(degree))
Run Code Online (Sandbox Code Playgroud)
有没有办法测试抛出的异常是否包含预期的消息,如:
an [IllegalArgumentException(expectedMessage)] should be thrownBy(Algorithms.create(degree)) -- what doesn't compile
Run Code Online (Sandbox Code Playgroud)
?
考虑以下针对BeforeAndAfterand的最小可行自包含测试用例BeforeAndAfterAll:
import org.scalatest.{BeforeAndAfter, BeforeAndAfterAll, FunSuite}
class BeforeAndAfterTestTest extends FunSuite with BeforeAndAfter with BeforeAndAfterAll {
override protected def beforeAll(): Unit = println("beforeAll")
override protected def afterAll(): Unit = println("afterAll")
override protected def before(fun: => Any)(implicit pos: Position): Unit = {
println("before")
}
override protected def after(fun: => Any)(implicit pos: Position): Unit = {
println("after")
}
test("hello1") { println("hello1") }
test("hello2") { println("hello2") }
}
Run Code Online (Sandbox Code Playgroud)
通过运行的结果scalatest是:
因此:
before/afterAll 不执行before/after做的不是调用 …
scala ×10
scalatest ×10
sbt ×3
apache-spark ×1
mocking ×1
mockito ×1
rabbitmq ×1
scala-ide ×1
unit-testing ×1