MOCKSTACKS
EN
Questions And Answers

More Tutorials









Scala Testing with ScalaCheck

Examples

Scalacheck with scalatest and error messages


Example of usage scalacheck with scalatest. Below we have four tests:

• "show pass example" - it passes

•"show simple example without custom error message " - just failed message without details, && boolean operator is used

•"show example with error messages on argument" - error message on argument ("argument" |:) Props.all method is used instead of &&

•"show example with error messages on command" - error message on command ("command" |:) Props.all method is used instead of &&


import org.scalatest.prop.Checkers
import org.scalatest.{Matchers, WordSpecLike}
import org.scalacheck.Gen._
import org.scalacheck.Prop._
import org.scalacheck.Prop
object Splitter {
 def splitLineByColon(message: String): (String, String) = {
 val (command, argument) = message.indexOf(":") match {
 case -1 =>
 (message, "")
 case x: Int =>
 (message.substring(0, x), message.substring(x + 1))
 }
 (command.trim, argument.trim)
 }
 def splitLineByColonWithBugOnCommand(message: String): (String, String) = {
 val (command, argument) = splitLineByColon(message)
 (command.trim + 2, argument.trim)
 }
 def splitLineByColonWithBugOnArgument(message: String): (String, String) = {
 val (command, argument) = splitLineByColon(message)
 (command.trim, argument.trim + 2)
}
}
class ScalaCheckSpec extends WordSpecLike with Matchers with Checkers {
 private val COMMAND_LENGTH = 4
 "ScalaCheckSpec " should {

"show pass example" in {
 check {
 Prop.forAll(listOfN(COMMAND_LENGTH, alphaChar), alphaStr) {
 (chars, expArgument) =>
 val expCommand = new String(chars.toArray)
 val line = s"$expCommand:$expArgument"
 val (c, p) = Splitter.splitLineByColon(line)
 Prop.all("command" |: c =? expCommand, "argument" |: expArgument =? p)
 }
 }
 }

"show simple example without custom error message " in {
 check {
 Prop.forAll(listOfN(COMMAND_LENGTH, alphaChar), alphaStr) {
 (chars, expArgument) =>
 val expCommand = new String(chars.toArray)
 val line = s"$expCommand:$expArgument"
 val (c, p) = Splitter.splitLineByColonWithBugOnArgument(line)
 c === expCommand && expArgument === p
 }
 }
}

"show example with error messages on argument" in {
 check {
 Prop.forAll(listOfN(COMMAND_LENGTH, alphaChar), alphaStr) {
 (chars, expArgument) =>
 val expCommand = new String(chars.toArray)
 val line = s"$expCommand:$expArgument"
 val (c, p) = Splitter.splitLineByColonWithBugOnArgument(line)
 Prop.all("command" |: c =? expCommand, "argument" |: expArgument =? p)
 }
 }
}

"show example with error messages on command" in {
 check {
 Prop.forAll(listOfN(COMMAND_LENGTH, alphaChar), alphaStr) {
 (chars, expArgument) =>
 val expCommand = new String(chars.toArray)
 val line = s"$expCommand:$expArgument"
 val (c, p) = Splitter.splitLineByColonWithBugOnCommand(line)
 Prop.all("command" |: c =? expCommand, "argument" |: expArgument =? p)
 } }
}

The output (fragments):

[info] - should show example // passed
[info] - should show simple example without custom error message *** FAILED ***
[info] (ScalaCheckSpec.scala:73)
[info] Falsified after 0 successful property evaluations.
[info] Location: (ScalaCheckSpec.scala:73)
[info] Occurred when passed generated values (
[info] arg0 = List(), // 3 shrinks
[info] arg1 = ""
[info] )
[info] - should show example with error messages on argument *** FAILED ***
[info] (ScalaCheckSpec.scala:86)
[info] Falsified after 0 successful property evaluations.
[info] Location: (ScalaCheckSpec.scala:86)
[info] Occurred when passed generated values (
[info] arg0 = List(), // 3 shrinks
[info] arg1 = ""
[info] )
[info] Labels of failing property:
[info] Expected "" but got "2"
[info] argument
[info] - should show example with error messages on command *** FAILED ***
[info] (ScalaCheckSpec.scala:99)
[info] Falsified after 0 successful property evaluations.
[info] Location: (ScalaCheckSpec.scala:99)
[info] Occurred when passed generated values (
[info] arg0 = List(), // 3 shrinks
[info] arg1 = ""
[info] )
[info] Labels of failing property:
[info] Expected "2" but got ""
[info] command


Conclusion

In this page (written and validated by ) you learned about Scala Testing with ScalaCheck . What's Next? If you are interested in completing Scala tutorial, your next topic will be learning about: Scala Testing with ScalaTest.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.