20

Click here to load reader

Scala test

Embed Size (px)

Citation preview

Page 1: Scala test

Scala Test

Meetu Maltiar - [email protected]

Page 2: Scala test

Introduction

Page 3: Scala test

Introduction Continued ..

With ScalaTest, we can test either Scala or Java code.

Integrates with popular tools such as JUnit, TestNG, Ant, and Maven

Designed to do different styles of testing like Behavior Driven Design for example

Page 4: Scala test

Simple Junit4 Testpackage com.inphina.ibat.junit

import org.scalatest.junit.AssertionsForJUnitimport org.junit.Assert._import org.junit.Testimport org.junit.Before

class 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) }

}

Page 5: Scala test

Concepts

Three concepts:

● Suite: A collection of tests. A test is anything which has a name and can succeed or fail.

● Runner: ScalaTest provides a Runner application that can run Suites of tests

● Reporter: As the tests are run, events are fired to a reporter, it takes care of presenting results back to the user

Page 6: Scala test

Scala Test Is Customizable

Suite<<trait>>

def expectedTestCount(Filter): Intdef 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)

Page 7: Scala test

Under The Hood

When you run a Test in Scala Test you basically invoke run(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 Suites

runTests(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 a Function Object with a name and passes it to withFixture(NoArgTest) which actually runs the test.

Page 8: Scala test

Pick a Core Trait

Page 9: Scala test

Mix In Other Traits

Page 10: Scala test

Traits in Scala Test

• Suite• Funsuite• Spec• FlatSpec• WordSpec• FeatureSpec• Assertions• ShouldMatchers• MustMatchers

Page 11: Scala test

Suite

➢ Traits approach to writing tests. Simply create classes extending Suite and define test methods.

➢ Test methods have names testXXXX. All methods must be public.

➢ Scala Test provides === operator. It is defined in Traits Assertions. Allows the failure report to include both right and left values.

Page 12: Scala test

Suite Demo

Page 13: Scala test

FunSuite➢ For writing Functional Tests use FunSuite Fun => Functional Suite.

➢ "test" is a method defined in FunSuite Trait. Test name goes in parentheses and the test code goes in curly braces.

➢ The test code in curly braces is passed as a by-name parameter to "test" method which registers for later execution

➢ A FunSuite's life-cycle has two phases: the registration phase and the ready phase.

Page 14: Scala test

FunSuite Demo

Page 15: Scala test

Spec

➢ Trait that facilitates a “behavior-driven” style of development (BDD), in which tests are combined with text that specifies the behavior the tests verify.

➢ A Spec contains describe clauses and tests. We define a describe clause with describe, and a test with it. Both describe and it are methods, defined in Spec, which will be invoked by the primary constructor of StackSpec.

Page 16: Scala test

Spec Demo

Page 17: Scala test

FeatureSpec

➢ A suite of tests in which each test represents one scenario of a feature.

➢ FeatureSpec is intended for writing tests that are "higher level" 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 scenario with “scenario”. Both feature and scenario are methods, defined in FeatureSpec

Page 18: Scala test

FeatureSpec Demo

Page 19: Scala test

➢Scala Test is a low risk way to get started with Scala

➢Scala Test shows what Scala can leverage for our projects

Summary

Page 20: Scala test

➢ Bill Venners presentation “Get Higher With Scala Test”

➢ Scala Test website www.scalatest.org

References