3. 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
4. Simple Junit4 Test
package com.inphina.ibat.junit
import org.scalatest.junit.AssertionsForJUnit
import org.junit.Assert._
import org.junit.Test
import 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)
}
}
5. 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
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. 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 test
name 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.
10. Traits in Scala Test
•
Suite
•
Funsuite
•
Spec
•
FlatSpec
•
WordSpec
•
FeatureSpec
•
Assertions
•
ShouldMatchers
•
MustMatchers
11. 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.
13. 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.
15. 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.
17. 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