SlideShare a Scribd company logo
Testing in Scala
Concepts: Types of Tests
Technology
Business
GlobalLocal
Unit Integration
Functional
Acceptance
Unit Tests
β€’ Check very specific functionality of your code
β€’ Assumes everything else works (mocking)
β€’ Test one specific/technical thing
β€’ e.g. test your Hadoop job locally, given money in DKK, currency
conversion to EUR is called once
Integration Tests
β€’ Check that all components work together
β€’ Includes real components, databases, files, etc.
β€’ Test the connectivity of components
β€’ e.g. run a job on a development Hadoop cluster, given sample data
expected output is written
Functional Tests
β€’ Test your product against the functionality you developed
β€’ Use real components, real data
β€’ E.g. run job on a live cluster, given a days worth of data the output is
correct (as expected according to specification)
Acceptance Tests
β€’ Final tests performed by your client before β€œaccepting the product”
β€’ Real components, real data, output is correct, performance is
acceptable, client is happy
Concepts: Testing Styles
β€’ TDD: Test Driven Development
β€’ BDD: Behavior Driven Development
Test Driven Development
β€’ Write an interface (trait) for the behavior
β€’ Write an empty implementation
β€’ Write your unit tests
β€’ Make the code compile and tests fail
β€’ Correct the implementation
β€’ Make the tests pass !
Behavior Driven Development
β€’ Same idea, just write more functional tests (define behavior)
Testing in Scala
β€’ For every class in src/main/scala write a test class in src/test/scala
β€’ Test classes ~ Test suites
β€’ Methods ~ Tests
β€’ Run the suit and see the output
Testing Libraries/Frameworks
β€’ Java: JUnit, TestNG (can be used with scala)
β€’ Scala: ScalaTest, Specs2
β€’ Mocking: ScalaMock, EasyMock, JMock, Mockito
β€’ Property Testing: ScalaCheck
β€’ UI Testing: Selenium
β€’ Very similar to Specs2, maybe more flexible, mainly choice of taste
β€’ Integrates with sbt, IntelliJ
β€’ Integrates with Mockito, ScalaCheck
libraryDependencies += "org.scalatest" %% "scalatest" % "2.2.1" % "test"
Using ScalaTest
import org.scalatest.FunSuite
class SetSuite extends FunSuite {
test("An empty Set should have size 0") {
assert(Set.empty.size == 0)
}
test("Invoking head on an empty Set should produce NoSuchElementException") {
intercept[NoSuchElementException] {
Set.empty.head
}
}
}
Testing in Scala by Adform research
Testing in Scala by Adform research
Testing in Scala by Adform research
Style Traits
β€’ ScalaTest allows you to mix in a testing style you like:
β€’ FunSuite
β€’ FlatSpec
β€’ FunSpec
β€’ WordSpec
β€’ PropSpec
β€’ etc.
β€’ http://www.scalatest.org/user_guide/selecting_a_style
FunSuite
FlatSpec
WordSpec
FeatureSpec
Testing Styles
β€’ ScalaTest recommends having an abstract class with the specs and
other traits you like
package com.mycompany.myproject
import org.scalatest._
abstract class UnitSpec extends FlatSpec with Matchers with OptionValues with Inside with Inspectors
package com.mycompany.myproject
import org.scalatest._
class MySpec extends UnitSpec {
// Your tests here
}
Fixtures
β€’ Your tests may need some setting up and tearing down
β€’ ScalaTest has many ways of doing this
β€’ http://www.scalatest.org/user_guide/sharing_fixtures
Testing in Scala by Adform research
Matchers
β€’ ScalaTest has lots of syntactic sugar to express your desires
result should equal (3)
result should have size 10
string should fullyMatch regex """(-)?(d+)(.d*)?"""
result1 should not be an [Orangutan]
sevenDotOh should equal (6.9 +- 0.2)
"howdy" should contain oneOf ('a', 'b', 'c', 'd')
(Array("Doe", "Ray", "Me") should contain oneOf ("X", "RAY", "BEAM")) (after being lowerCased)
List(0, 1, 2, 2, 99, 3, 3, 3, 5) should contain inOrder (1, 2, 3)
map should (contain key ("two") and not contain value (7))
This is Scala Code !!
Mocking
β€’ You should always code against interfaces (traits)
β€’ If you do, testing is easier, since you can mock your dependencies!
val m = mock[Turtle]
m.expects.forward(10.0) twice
m1 returns 42
m2 expects ("this", "that") returning "the other"
Property Based Testing
β€’ New and exciting way of testing your code!
β€’ Instead of examples, you specify properties (higher order testing!)
β€’ E.g. when you concatenate two lists, new lengths is sum of lengths
β€’ ScalaCheck then generates random inputs and runs many tests
β€’ This can catch MANY corner cases you never expect!
ScalaCheck
import org.scalacheck.Gen
val propConcatLists = forAll { (l1: List[Int], l2: List[Int]) =>
l1.size + l2.size == (l1 ::: l2).size
}
+ OK, passed 100 tests.
val propSqrt = forAll { (n: Int) => scala.math.sqrt(n*n) == n }
! Falsified after 1 passed tests:
> -1
ScalaCheck: Generators
val genLeaf = value(Leaf)
val genNode = for {
v <- arbitrary[Int]
left <- genTree
right <- genTree
} yield Node(left, right, v)
def genTree: Gen[Tree] = oneOf(genLeaf, genNode)
Option[Tree] = Some(Node(Leaf,Node(Node(Node(Node(Node(Node(Leaf,Leaf,-
71),Node(Leaf,Leaf,-49),17),Leaf,-20),Leaf,-7),Node(Node(Leaf,Leaf,26),Leaf,-
3),49),Leaf,84),-29))
is
AWESOME !!
Scoverage
β€’ Produces test coverage reports
β€’ Measures how many statements are tested
β€’ Only works with Scala 2.11
Testing in Scala by Adform research
Testing Scalding Jobs
HOMEWORK: use ScalaTest instead of Specs2
DEMO TIME
Slava takes the stage
Testing in Scala by Adform research

More Related Content

PPTX
Spark intro by Adform Research
PDF
Akka lsug skills matter
PPTX
Akka.net versus microsoft orleans
PPTX
Ansible Devops North East - slides
PDF
Akka Cluster in Java - JCConf 2015
ZIP
5εˆ†γ§θͺ¬ζ˜Žγ™γ‚‹ Play! scala
KEY
2011/10/08_Playframework_GAE_to_Heroku
PDF
γ¨γ‚Šγ‚γˆγšδ½Ώγ†Scalaz
Spark intro by Adform Research
Akka lsug skills matter
Akka.net versus microsoft orleans
Ansible Devops North East - slides
Akka Cluster in Java - JCConf 2015
5εˆ†γ§θͺ¬ζ˜Žγ™γ‚‹ Play! scala
2011/10/08_Playframework_GAE_to_Heroku
γ¨γ‚Šγ‚γˆγšδ½Ώγ†Scalaz

What's hot (19)

PPTX
Akka Actor presentation
PDF
Full Stack Scala
PDF
Scala.js - yet another what..?
PPTX
"Walk in a distributed systems park with Orleans" Π•Π²Π³Π΅Π½ΠΈΠΉ Π‘ΠΎΠ±Ρ€ΠΎΠ²
Β 
KEY
Curator intro
PDF
First glance at Akka 2.0
PPTX
Extending ansible
PDF
Akka in Practice: Designing Actor-based Applications
Β 
PDF
Go database/sql
PDF
ChefConf 2014 - AWS OpsWorks Under The Hood
PDF
Real-time search in Drupal. Meet Elasticsearch
PPTX
Introduction to Akka - Atlanta Java Users Group
PPTX
Terraform day02
KEY
Wider than rails
PDF
Understanding Akka Streams, Back Pressure, and Asynchronous Architectures
PPTX
Scala.js for large and complex frontend apps
PDF
Load test REST APIs using gatling
PPTX
Building an aws sdk for Perl - Granada Perl Workshop 2014
PDF
20150627 bigdatala
Β 
Akka Actor presentation
Full Stack Scala
Scala.js - yet another what..?
"Walk in a distributed systems park with Orleans" Π•Π²Π³Π΅Π½ΠΈΠΉ Π‘ΠΎΠ±Ρ€ΠΎΠ²
Β 
Curator intro
First glance at Akka 2.0
Extending ansible
Akka in Practice: Designing Actor-based Applications
Β 
Go database/sql
ChefConf 2014 - AWS OpsWorks Under The Hood
Real-time search in Drupal. Meet Elasticsearch
Introduction to Akka - Atlanta Java Users Group
Terraform day02
Wider than rails
Understanding Akka Streams, Back Pressure, and Asynchronous Architectures
Scala.js for large and complex frontend apps
Load test REST APIs using gatling
Building an aws sdk for Perl - Granada Perl Workshop 2014
20150627 bigdatala
Β 
Ad

Similar to Testing in Scala by Adform research (20)

KEY
The Why and How of Scala at Twitter
PPTX
ScalaCheck
PPTX
Scala, Play 2.0 & Cloud Foundry
PDF
CBDW2014 - MockBox, get ready to mock your socks off!
PPTX
Qt test framework
Β 
PPTX
Scala final ppt vinay
PDF
Building XWiki
PPTX
How Scala promotes TDD
PDF
Quick tour to front end unit testing using jasmine
PPTX
Scala in the Wild
PPTX
Taxonomy of Scala
PDF
Introducing BoxLang : A new JVM language for productivity and modularity!
PPTX
Distributed Model Validation with Epsilon
PDF
Typesafe stack - Scala, Akka and Play
PDF
Spring Day | Spring and Scala | Eberhard Wolff
PDF
pull requests I sent to scala/scala (ny-scala 2019)
PDF
Scala Frustrations
PDF
Mastering PowerShell Testing with Pester
The Why and How of Scala at Twitter
ScalaCheck
Scala, Play 2.0 & Cloud Foundry
CBDW2014 - MockBox, get ready to mock your socks off!
Qt test framework
Β 
Scala final ppt vinay
Building XWiki
How Scala promotes TDD
Quick tour to front end unit testing using jasmine
Scala in the Wild
Taxonomy of Scala
Introducing BoxLang : A new JVM language for productivity and modularity!
Distributed Model Validation with Epsilon
Typesafe stack - Scala, Akka and Play
Spring Day | Spring and Scala | Eberhard Wolff
pull requests I sent to scala/scala (ny-scala 2019)
Scala Frustrations
Mastering PowerShell Testing with Pester
Ad

More from Vasil Remeniuk (20)

PPTX
Product Minsk - Π Π’Π‘ ΠΈ ΠŸΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ°Ρ‚ΠΈΠΊ
PDF
Π Π°Π±ΠΎΡ‚Π° с Akka Π‘luster, @afiskon, scalaby#14
PDF
Cake pattern. Presentation by Alex Famin at scalaby#14
PDF
Scala laboratory: Globus. iteration #3
PPTX
Spark Intro by Adform Research
PPTX
Types by Adform Research, Saulius Valatka
PPTX
Types by Adform Research
PPTX
Scalding by Adform Research, Alex Gryzlov
PPTX
Scalding by Adform Research, Alex Gryzlov
PPTX
Spark by Adform Research, Paulius
PPTX
Scala Style by Adform Research (Saulius Valatka)
PPTX
SBT by Aform Research, Saulius Valatka
PDF
Scala laboratory: Globus. iteration #2
PPTX
Testing in Scala. Adform Research
PDF
Scala laboratory. Globus. iteration #1
PDF
Cassandra + Spark + Elk
PDF
ΠžΠΏΡ‹Ρ‚ использования Spark, Основано Π½Π° Ρ€Π΅Π°Π»ΡŒΠ½Ρ‹Ρ… событиях
PDF
ETL со Spark
PDF
Funtional Reactive Programming with Examples in Scala + GWT
PDF
Vaadin+Scala
Product Minsk - Π Π’Π‘ ΠΈ ΠŸΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ°Ρ‚ΠΈΠΊ
Π Π°Π±ΠΎΡ‚Π° с Akka Π‘luster, @afiskon, scalaby#14
Cake pattern. Presentation by Alex Famin at scalaby#14
Scala laboratory: Globus. iteration #3
Spark Intro by Adform Research
Types by Adform Research, Saulius Valatka
Types by Adform Research
Scalding by Adform Research, Alex Gryzlov
Scalding by Adform Research, Alex Gryzlov
Spark by Adform Research, Paulius
Scala Style by Adform Research (Saulius Valatka)
SBT by Aform Research, Saulius Valatka
Scala laboratory: Globus. iteration #2
Testing in Scala. Adform Research
Scala laboratory. Globus. iteration #1
Cassandra + Spark + Elk
ΠžΠΏΡ‹Ρ‚ использования Spark, Основано Π½Π° Ρ€Π΅Π°Π»ΡŒΠ½Ρ‹Ρ… событиях
ETL со Spark
Funtional Reactive Programming with Examples in Scala + GWT
Vaadin+Scala

Recently uploaded (20)

PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
Β 
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Modernizing your data center with Dell and AMD
PDF
Approach and Philosophy of On baking technology
PPT
β€œAI and Expert System Decision Support & Business Intelligence Systems”
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
Cloud computing and distributed systems.
PDF
KodekX | Application Modernization Development
Β 
PPTX
MYSQL Presentation for SQL database connectivity
PPT
Teaching material agriculture food technology
PDF
cuic standard and advanced reporting.pdf
PPTX
A Presentation on Artificial Intelligence
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Machine learning based COVID-19 study performance prediction
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
The Rise and Fall of 3GPP – Time for a Sabbatical?
Β 
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Building Integrated photovoltaic BIPV_UPV.pdf
Modernizing your data center with Dell and AMD
Approach and Philosophy of On baking technology
β€œAI and Expert System Decision Support & Business Intelligence Systems”
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Cloud computing and distributed systems.
KodekX | Application Modernization Development
Β 
MYSQL Presentation for SQL database connectivity
Teaching material agriculture food technology
cuic standard and advanced reporting.pdf
A Presentation on Artificial Intelligence
Digital-Transformation-Roadmap-for-Companies.pptx
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Encapsulation_ Review paper, used for researhc scholars
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Machine learning based COVID-19 study performance prediction
Mobile App Security Testing_ A Comprehensive Guide.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...

Testing in Scala by Adform research

  • 2. Concepts: Types of Tests Technology Business GlobalLocal Unit Integration Functional Acceptance
  • 3. Unit Tests β€’ Check very specific functionality of your code β€’ Assumes everything else works (mocking) β€’ Test one specific/technical thing β€’ e.g. test your Hadoop job locally, given money in DKK, currency conversion to EUR is called once
  • 4. Integration Tests β€’ Check that all components work together β€’ Includes real components, databases, files, etc. β€’ Test the connectivity of components β€’ e.g. run a job on a development Hadoop cluster, given sample data expected output is written
  • 5. Functional Tests β€’ Test your product against the functionality you developed β€’ Use real components, real data β€’ E.g. run job on a live cluster, given a days worth of data the output is correct (as expected according to specification)
  • 6. Acceptance Tests β€’ Final tests performed by your client before β€œaccepting the product” β€’ Real components, real data, output is correct, performance is acceptable, client is happy
  • 7. Concepts: Testing Styles β€’ TDD: Test Driven Development β€’ BDD: Behavior Driven Development
  • 8. Test Driven Development β€’ Write an interface (trait) for the behavior β€’ Write an empty implementation β€’ Write your unit tests β€’ Make the code compile and tests fail β€’ Correct the implementation β€’ Make the tests pass !
  • 9. Behavior Driven Development β€’ Same idea, just write more functional tests (define behavior)
  • 10. Testing in Scala β€’ For every class in src/main/scala write a test class in src/test/scala β€’ Test classes ~ Test suites β€’ Methods ~ Tests β€’ Run the suit and see the output
  • 11. Testing Libraries/Frameworks β€’ Java: JUnit, TestNG (can be used with scala) β€’ Scala: ScalaTest, Specs2 β€’ Mocking: ScalaMock, EasyMock, JMock, Mockito β€’ Property Testing: ScalaCheck β€’ UI Testing: Selenium
  • 12. β€’ Very similar to Specs2, maybe more flexible, mainly choice of taste β€’ Integrates with sbt, IntelliJ β€’ Integrates with Mockito, ScalaCheck libraryDependencies += "org.scalatest" %% "scalatest" % "2.2.1" % "test"
  • 13. Using ScalaTest import org.scalatest.FunSuite class SetSuite extends FunSuite { test("An empty Set should have size 0") { assert(Set.empty.size == 0) } test("Invoking head on an empty Set should produce NoSuchElementException") { intercept[NoSuchElementException] { Set.empty.head } } }
  • 17. Style Traits β€’ ScalaTest allows you to mix in a testing style you like: β€’ FunSuite β€’ FlatSpec β€’ FunSpec β€’ WordSpec β€’ PropSpec β€’ etc. β€’ http://www.scalatest.org/user_guide/selecting_a_style
  • 22. Testing Styles β€’ ScalaTest recommends having an abstract class with the specs and other traits you like package com.mycompany.myproject import org.scalatest._ abstract class UnitSpec extends FlatSpec with Matchers with OptionValues with Inside with Inspectors package com.mycompany.myproject import org.scalatest._ class MySpec extends UnitSpec { // Your tests here }
  • 23. Fixtures β€’ Your tests may need some setting up and tearing down β€’ ScalaTest has many ways of doing this β€’ http://www.scalatest.org/user_guide/sharing_fixtures
  • 25. Matchers β€’ ScalaTest has lots of syntactic sugar to express your desires result should equal (3) result should have size 10 string should fullyMatch regex """(-)?(d+)(.d*)?""" result1 should not be an [Orangutan] sevenDotOh should equal (6.9 +- 0.2) "howdy" should contain oneOf ('a', 'b', 'c', 'd') (Array("Doe", "Ray", "Me") should contain oneOf ("X", "RAY", "BEAM")) (after being lowerCased) List(0, 1, 2, 2, 99, 3, 3, 3, 5) should contain inOrder (1, 2, 3) map should (contain key ("two") and not contain value (7)) This is Scala Code !!
  • 26. Mocking β€’ You should always code against interfaces (traits) β€’ If you do, testing is easier, since you can mock your dependencies! val m = mock[Turtle] m.expects.forward(10.0) twice m1 returns 42 m2 expects ("this", "that") returning "the other"
  • 27. Property Based Testing β€’ New and exciting way of testing your code! β€’ Instead of examples, you specify properties (higher order testing!) β€’ E.g. when you concatenate two lists, new lengths is sum of lengths β€’ ScalaCheck then generates random inputs and runs many tests β€’ This can catch MANY corner cases you never expect!
  • 28. ScalaCheck import org.scalacheck.Gen val propConcatLists = forAll { (l1: List[Int], l2: List[Int]) => l1.size + l2.size == (l1 ::: l2).size } + OK, passed 100 tests. val propSqrt = forAll { (n: Int) => scala.math.sqrt(n*n) == n } ! Falsified after 1 passed tests: > -1
  • 29. ScalaCheck: Generators val genLeaf = value(Leaf) val genNode = for { v <- arbitrary[Int] left <- genTree right <- genTree } yield Node(left, right, v) def genTree: Gen[Tree] = oneOf(genLeaf, genNode) Option[Tree] = Some(Node(Leaf,Node(Node(Node(Node(Node(Node(Leaf,Leaf,- 71),Node(Leaf,Leaf,-49),17),Leaf,-20),Leaf,-7),Node(Node(Leaf,Leaf,26),Leaf,- 3),49),Leaf,84),-29))
  • 31. Scoverage β€’ Produces test coverage reports β€’ Measures how many statements are tested β€’ Only works with Scala 2.11
  • 33. Testing Scalding Jobs HOMEWORK: use ScalaTest instead of Specs2