我想创建几个与数据库互操作的规范.
class DocumentSpec extends mutable.Specification with BeforeAfterExample {
sequential
def before() = {createDB()}
def after() = {dropDB()}
// examples
// ...
}
Run Code Online (Sandbox Code Playgroud)
在每个示例之前和之后(按顺序执行)创建和删除数据库.Everithing按预期工作,直到只有一个规范适用于数据库.由于规范是并行执行的,因此会干扰和失败.
我希望我能够通过指示specs2顺序运行具有副作用的测试同时保持副作用免费测试并行运行来避免这种情况.可能吗?
我注意到SBT正在并行运行我的specs2测试.这似乎很好,除了我的一个测试涉及从文件读取和写入因此无法预测失败,例如见下文.
有没有更好的选择
class WriteAndReadSpec extends Specification{
val file = new File("testFiles/tmp.txt")
"WriteAndRead" should {
"work once" in {
new FileWriter(file, false).append("Foo").close
Source.fromFile(file).getLines().toList(0) must_== "Foo"
}
"work twice" in {
new FileWriter(file, false).append("Bar").close
Source.fromFile(file).getLines().toList(0) must_== "Bar"
}
}
trait TearDown extends After {
def after = if(file.exists) file.delete
}
}
Run Code Online (Sandbox Code Playgroud) 我需要将我的一个测试用例置于"挂起"状态.
我想给它一些消息,它可以在运行测试时显示在输出上,就像JUnit一样@Ignore("Pending: issue #1234 needs to be fixed").
与Specs2相同吗?
class MySpec extends mutable.Specification {
args(skipAll = true) // Can I include a message here in the output somehow?
"cool MyClass feature" should {
"which is broken unfortunately" in {
failure
}
}
}
Run Code Online (Sandbox Code Playgroud)
提前致谢!
默认情况下Logger,在应用程序运行时可见的所有输出在测试应用程序时都是静音的.
如何强制在specs2报告中显示调试,信息等?
我想暂时在我的specs2测试套件中进行ScalaCheck属性测试,以便于调试.现在,每次重新运行测试套件时都会生成不同的值,这会使调试变得令人沮丧,因为您不知道观察到的行为的变化是由代码更改引起的,还是仅仅是由生成的不同数据引起的.
我怎样才能做到这一点?是否有正式的方法来设置ScalaCheck使用的随机种子?
我正在使用sbt运行测试套件.
奖金的问题:是否有一个正式的方式打印出由ScalaCheck使用的随机种子,这样就可以重现即使是非确定性的试运行?
我想测试一个返回a的方法Future.我的尝试如下:
import org.specs2.mutable.Specification
import scala.concurrent.ExecutionContext.Implicits.global
import scala.util.{Failure, Success}
class AsyncWebClientSpec extends Specification{
"WebClient when downloading images" should {
"for a valid link return non-zero content " in {
val testImage = AsyncWebClient.get("https://www.google.cz/images/srpr/logo11ww.png")
testImage.onComplete { res =>
res match {
case Success(image) => image must not have length(0)
case _ =>
}
AsyncWebClient.shutDown
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
除了我无法使这个代码工作的事实,我想有一个更好的方法来测试一个Future定向匹配器的期货.
如何在specs2中正确完成?
如果您在SBT项目中定义了2个测试:
class Spec1 extends Specification {
def is =
"Tests for specification 1" ^
p ^
"Test case 1" ! todo ^
end
}
Run Code Online (Sandbox Code Playgroud)
和
class Spec2 extends Specification {
def is =
"Tests for specification 2" ^
p ^
"Test case 2" ! todo ^
end
}
Run Code Online (Sandbox Code Playgroud)
然后test从SBT内部运行将执行这两个测试.只运行其中一个测试的最简单方法是什么?
我正在尝试学习Play scala中的单元测试,但我遇到了一些问题.我试图在我的模型层上运行几个测试,如下所示:
"User Model" should {
"be created and retrieved by username" in {
running(FakeApplication()) {
val newUser = User(username = "weezybizzle",password = "password")
User.save(newUser)
User.findOneByUsername("weezybizzle") must beSome
}
}
"another test" in {
running(FakeApplication()) {
// more tests involving adding and removing users
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当以这种方式执行操作时,我无法在第二个单元测试中连接到数据库,说连接已关闭.我试图通过将所有代码包含在同一个假应用程序上运行的块中来解决这个问题,但这也没有用.
running(FakeApplication()) {
"be created and retrieved by username" in {
val newUser = User(username = "weezybizzle",password = "password")
User.save(newUser)
User.findOneByUsername("weezybizzle") must beSome
}
"another test" in {
// more tests involving adding …Run Code Online (Sandbox Code Playgroud) 我玩了!2用于Scala应用程序,我使用Specs2进行测试.我可以使用test命令或特定规范运行所有测试test-only MyParticularSpec.
我想做的是在规范中标记一些特定的规范,甚至是单个方法,以便执行以下操作:
等等.
我想这样的事情应该是可行的,也许是通过添加一些注释,但我不知道如何去做.
是否存在选择性运行某些测试而不是其他测试的机制?
编辑我在使用时已经回答了自己test-only.命令行选项仍然不适用于该test任务.在sbt指南之后,我尝试创建一个额外的sbt配置,比如
object ApplicationBuild extends Build {
// more settings
lazy val UnitTest = config("unit") extend(Test)
lazy val specs = "org.scala-tools.testing" %% "specs" % "1.6.9" % "unit"
val main = PlayProject(appName, appVersion, appDependencies, mainLang = SCALA)
.configs(UnitTest)
.settings(inConfig(UnitTest)(Defaults.testTasks) : _*)
.settings(
testOptions in UnitTest += Tests.Argument("exclude integration"),
libraryDependencies += specs
)
}
Run Code Online (Sandbox Code Playgroud)
当我传递没有选项的参数时,例如当我放置时,这是有效的Test.Argument("plan").但我无法找到如何传递更复杂的论点.我试过了
Tests.Argument("exclude integration")
Tests.Argument("exclude=integration")
Tests.Argument("-exclude integration")
Tests.Argument("-exclude=integration") …Run Code Online (Sandbox Code Playgroud)