Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Scala test


Published on

Scala test allows you to test your application written in Java and Scala.

Published in: Technology
  • D0WNL0AD FULL ▶ ▶ ▶ ▶ ◀ ◀ ◀ ◀
    Are you sure you want to  Yes  No
    Your message goes here
  • Be the first to like this

Scala test

  1. 1. Scala TestMeetu Maltiar -
  2. 2. Introduction
  3. 3. Introduction Continued ..With ScalaTest, we can test either Scala or Javacode.Integrates with popular tools such as JUnit,TestNG, Ant, and MavenDesigned to do different styles of testing likeBehavior Driven Design for example
  4. 4. Simple Junit4 Testpackage com.inphina.ibat.junitimport org.scalatest.junit.AssertionsForJUnitimport org.junit.Assert._import org.junit.Testimport org.junit.Beforeclass SimpleJunit4Demo extends AssertionsForJUnit { var sb: StringBuilder = _ @Before def initialize() { sb = new StringBuilder("ScalaTest is ") } @Test def verifyEasy() { sb.append("easy!") assertEquals("ScalaTest is easy!", sb.toString) }}
  5. 5. ConceptsThree concepts:● Suite: A collection of tests. A test is anything which has a nameand can succeed or fail.● Runner: ScalaTest provides a Runner application that can runSuites of tests● Reporter: As the tests are run, events are fired to a reporter, ittakes care of presenting results back to the user
  6. 6. Scala Test Is Customizable Suite <<trait>> def expectedTestCount(Filter): Int def testNames: Set[String] def tags: Map[String, Set[String]] def nestedSuites: List[Suite] def run(Option[String], Reporter, ...) def runNestedSuites(Reporter, ...) def runTests(Option[String], Reporter, ...) def runTest(Reporter, ...) def withFixture(NoArgTest)
  7. 7. Under The HoodWhen you run a Test in Scala Test you basically invokerun(Option[String], Reporter, .. ) on Suite Object.● It then calls runNestedSuites(Reporter, ..)● And it calls runTests(Option[String], Reporter, ..)runNestedSuites(Reporter, ..) :● invokes nestedSuites() : List[Suite] to get a List of nested SuitesrunTests(Option[String], Reporter, ..) will call def testNames:Set[String] to get a Set of test names it needs to run. For each testname it calls runTest(Reporter, ...) It wraps the test code as aFunction Object with a name and passes it to withFixture(NoArgTest)which actually runs the test.
  8. 8. Pick a Core Trait
  9. 9. Mix In Other Traits
  10. 10. Traits in Scala Test• Suite• Funsuite• Spec• FlatSpec• WordSpec• FeatureSpec• Assertions• ShouldMatchers• MustMatchers
  11. 11. Suite➢ Traits approach to writing tests. Simply createclasses extending Suite and define test methods.➢ Test methods have names testXXXX. All methodsmust be public.➢ Scala Test provides === operator. It is defined inTraits Assertions. Allows the failure report to includeboth right and left values.
  12. 12. Suite Demo
  13. 13. FunSuite➢ For writing Functional Tests use FunSuite Fun =>Functional Suite.➢ "test" is a method defined in FunSuite Trait. Test namegoes in parentheses and the test code goes in curlybraces.➢ The test code in curly braces is passed as a by-nameparameter to "test" method which registers for laterexecution➢ A FunSuites life-cycle has two phases: the registrationphase and the ready phase.
  14. 14. FunSuite Demo
  15. 15. Spec➢ Trait that facilitates a “behavior-driven” style ofdevelopment (BDD), in which tests are combined with textthat specifies the behavior the tests verify.➢ A Spec contains describe clauses and tests. We definea describe clause with describe, and a test with it. Bothdescribe and it are methods, defined in Spec, which willbe invoked by the primary constructor of StackSpec.
  16. 16. Spec Demo
  17. 17. FeatureSpec➢ A suite of tests in which each test represents onescenario of a feature.➢ FeatureSpec is intended for writing tests that are "higherlevel" than unit tests, for example, integration tests,functional tests, and acceptance tests.➢ A FeatureSpec contains feature clauses and scenarios.we define a feature clause with “feature”, and a scenariowith “scenario”. Both feature and scenario are methods,defined in FeatureSpec
  18. 18. FeatureSpec Demo
  19. 19. SummaryScala Test is a low risk way to get started➢with Scala➢ Scala Test shows what Scala canleverage for our projects
  20. 20. References➢ Bill Venners presentation “Get HigherWith Scala Test”➢ Scala Test website