Click here to load reader
Upload
meetu-maltiar
View
722
Download
2
Embed Size (px)
Citation preview
Scala Test
Meetu Maltiar - [email protected]
Introduction
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
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) }
}
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
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)
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.
Pick a Core Trait
Mix In Other Traits
Traits in Scala Test
• Suite• Funsuite• Spec• FlatSpec• WordSpec• FeatureSpec• Assertions• ShouldMatchers• MustMatchers
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.
Suite Demo
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.
FunSuite Demo
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.
Spec Demo
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
FeatureSpec Demo
➢Scala Test is a low risk way to get started with Scala
➢Scala Test shows what Scala can leverage for our projects
Summary
➢ Bill Venners presentation “Get Higher With Scala Test”
➢ Scala Test website www.scalatest.org
References