Testing in Scala

By Daniel Hinojosa

Daniel Hinojosa, "Testing in Scala" English | ISBN: 1449315119 | 2013 | writer: O’Reilly Media | PDF | 166 pages | 6 + 1 MB

If you construct your Scala software via Test-Driven improvement, you’ll quick see the benefits of checking out prior to you write construction code. This hands-on booklet exhibits you ways to create checks with ScalaTest and the Specs2—two of the simplest trying out frameworks available—and easy methods to run your exams within the easy construct device (SBT) designed particularly for Scala projects.

By development a pattern electronic jukebox program, you’ll observe tips to isolate your assessments from huge subsystems and networks with mocking code, and the way to exploit the ScalaCheck library for automatic specification-based trying out. If you’re acquainted with Scala, Ruby, or Python, this ebook is for you.
Get an summary of Test-Driven Development
Start an easy venture with SBT and create assessments sooner than you write code
Dive into SBT’s simple instructions, interactive mode, packaging, and history
Use ScalaTest either within the command line and with SBT, and the best way to contain JUnit and TestNG
Work with the Specs2 framework, together with Specification types, matchers DSLs, and information Tables
Understand mocking by utilizing Java frameworks EasyMock and Mockito, and the Scala-only framework ScalaMock
Automate trying out by utilizing ScalaCheck to generate pretend information

Show description

Quick preview of Testing in Scala PDF

Show sample text content

The be certain approach simply accepts the mock, at which aspect one other process may be known as to make sure the habit. In different phrases, the final strains of the reputation specification proven listed here are of the shape verify(mock). method(params), the place approach is the tactic that's anticipated to be referred to as and the parameters given are the parameters that have been anticipated to be added. If any verification fails, an exception is thrown. for instance, one characteristic of trying out frameworks, together with EasyMock, is that the developer can specify what percentage instances a specific strategy could be referred to as with a given parameter.

Within the following instance, a host is selected randomly among 1 and fifty two, in order that the worth can be utilized in a try. What will get chosen in Gen. pick out has to make feel. src/test/scala/com/oreilly/testingscala/VariousGenCheckProperties. scala. Prop. forAll(Gen. choose(1, 52)) { card => card < fifty three && card > zero } Gen. opt for with Strings wouldn't determine of the field, seeing that there's there is not any algorithm verified on how you can create words—say, among Foo and Grok. that's not to assert it's very unlikely. on the way to use your working system’s dictionary (e.

In a Spec, after every one it clause the corresponding block returns pending for any try out that's not applied or simply no longer prepared. while I first had the belief for AlbumTest and was once jotting down attainable assessments to incorporate, pending may have made the try out seem like the subsequent: classification AlbumSpec extends FunSpec with ShouldMatchers with GivenWhenThen { describe("An Album") { it("can upload an Artist to the album at development time") {pending} it("can upload prefer to no longer have any artists at building time") {pending} } } The exams marked as pending will generate output marked pending whilst run both on the command recommended or via SBT.

A Java programmer coming to Scala can think about the thing as a constitution that homes tools and variables that may were static in Java. the subsequent instance exhibits a singleton category for The Boss—BruceSpringsteenFactory. The manufacturing facility is there to create items or a listing of Album gadgets that created Bruce Springsteen. The artist approach is used to create an Artist item representing Bruce. The discography strategy creates an inventory of Album gadgets representing a few of his previous works, and eventually the jukebox strategy creates a JukeBox item of all of Bruce’s paintings.

Specs2. Specification classification ArtistAcceptanceSpec extends Specification { def is = "An artist must have a center identify at building" ^ """An artist could be in a position to be built with a center identify and get it again calling 'middleName'""" ! makeAnArtistWithMiddleName ^ """An artist could be capable of have a whole identify made from the 1st and final identify given a primary and final identify at building time""" ! testFullNameWithFirstAndLast ^ """An artist will be in a position to have a whole identify made up of the 1st, center and final identify given a primary, heart, and final identify at development time""" !

Download PDF sample

Rated 4.66 of 5 – based on 14 votes