© 2014, Conversant, Inc. All rights reserved.
PRESENTED BY
6/1/15
Ultimate Automation - ScalaTest
Software Test Automation on JVM
Platform
lRajesh Thanavarapu
© 2014, Conversant, Inc. All rights reserved.
Software Test Automation
“In software testing, test automation is the use of special software (separate
from the software being tested) to control the execution of
tests and the comparison of actual outcomes
with predicted outcomes.[1] Test automation can automate
some repetitive but necessary tasks in a formalized testing process already
in place, or add additional testing that would be difficult to perform manually.”
- From Wikipedia, the free encyclopedia
© 2014, Conversant, Inc. All rights reserved.
Test Automation Classification
* Code-driven Test Automation
* GUI Test Automation
* API Test Automation <- Topic of discussion
© 2014, Conversant, Inc. All rights reserved.
Ultimate Automation Tool
* Associate Tests with Specs
* Facilitates simplistic Test design
* Provides complete Data abstraction
* Documentation that is up to date
* Allows for easy enhancements
* Provides visibility to all teams – Business,
Prod, Dev, QA & The Management
© 2014, Conversant, Inc. All rights reserved.
Too Many Names
TDD
ATDD
BDD
FDD
DDD
MDD
R2D2
C3PO
© 2014, Conversant, Inc. All rights reserved.
Specification Testing
* One Source - Tests, Specs, Documentation,
Reports
* Everybody wins – Stake holders, Business,
Product, Developers & Testers
© 2014, Conversant, Inc. All rights reserved.
Scala
* Shares the JVM Space
* JIT Compiled and Bytecode
* Supports both OOP & FP
* Higher Order Language
* Aggressive Road map – Java 8
* Complete Maven Integration
* BSD Licensed
© 2014, Conversant, Inc. All rights reserved.
Java => Scala
Improvements: A non-unified type system (primitives vs.
objects), type erasure (polymorphism), and checked
exceptions.
Additions: Functional programming concepts- type
inference, anonymous functions (closures), lazy
initialization etc.
© 2014, Conversant, Inc. All rights reserved.
Scala Is Concise
Java
public class Person
{
private String firstName;
private String lastName;
// Let's not forget the getters and setters OK?
Person(String firstName, String lastName) {
this.firstName=firstName;
this.lastName=lastName;
}
}
Scala
class Person(firstName:String, lastName:String)
© 2014, Conversant, Inc. All rights reserved.
Scala Is Implicit
Java
public class ClassicSingleton {
private static ClassicSingleton instance = null;
protected ClassicSingleton() {
// Exists only to defeat instantiation.
}
public static ClassicSingleton getInstance() {
if(instance == null) {
instance = new ClassicSingleton();
}
return instance;
}
}
Scala
Object ClassicSingleton{
}
© 2014, Conversant, Inc. All rights reserved.
Scala Is True FP
Lambda Expressions
val even = ( x :Int ) => {
X % 2 == 0 }
even(7)
Output: false
Theory
Var res=f(x)
Var res=f(x) + (f(y) + f(z))
Var res=f(x)=f(a) * f(b)
Best Curry(ing) in town...
© 2014, Conversant, Inc. All rights reserved.
Scala better than Java?
Scala will not replace Java, but it truly
Compliments!!
© 2014, Conversant, Inc. All rights reserved.
Big Companies using Scala
© 2014, Conversant, Inc. All rights reserved.
ScalaTest
* API based on Scala – external jar
* Simple and English like DSL
* Traits for everyone (Java 8 mixin)
FeatureSpec
FlatSpec
FunSpec and more...
© 2014, Conversant, Inc. All rights reserved.
Trait FeatureSpec
class SampleClass extends FeatureSpec {
feature("The user can pop an element off the top of the stack") {
scenario("pop is invoked on a non-empty stack") (pending)
scenario("pop is invoked on an empty stack"){
…
}
ignore("pop is invoked on an empty stack") {
…
}
}
}
© 2014, Conversant, Inc. All rights reserved.
Impl. Detailsprotected def feature(description: String)(fun: => Unit) {
try {
registerNestedBranch(Resources.feature(...), fun,...)
}
catch {
case e: exceptions.TestFailedException => throw new exceptions.(...)
...
}
}
protected def scenario(specText: String, testTags: Tag*)(testFun: FixtureParam => Registeration {
engine.registerTest(Resources.scenario(...)
}
protected def ignore(specText: String, testTags: Tag*)(testFun: FixtureParam => Registeration) {
engine.registerIgnoredTest(...)
}
© 2014, Conversant, Inc. All rights reserved.
Trait FunSpec
class SampleClass extends FunSpec {
describe("A Set") {
describe("when empty") {
it("should have size 0") {
assert(Set.empty.size == 0)
}
it("should produce NoSuchElementException when head is invoked") {
intercept[NoSuchElementException] {
Set.empty.head
}
}
}
}
© 2014, Conversant, Inc. All rights reserved.
Trait FlatSpec
class SampleClass extends FlatSpec {
"An empty Set" should "have size 0" in {
assert(Set.empty.size == 0)
}
it should "produce NoSuchElementException when head is invoked" in {
intercept[NoSuchElementException] {
Set.empty.head
}
}
}
© 2014, Conversant, Inc. All rights reserved.
Many Styles to Choose
http://scalatest.org/user_guide
/selecting_a_style
© 2014, Conversant, Inc. All rights reserved.
Test Development
* Define a comprehensive list of test Scenarios
* Initially write all tests as “Pending”
* Incrementally add test code (worker methods)
Language of choice: Scala, Java or Both
* Review test results
* Add more test Scenarios...
© 2014, Conversant, Inc. All rights reserved.
Sample Code
feature("Block user from registering to listed promotion codes") {
scenario("User should be blocked for a listed promo code and listed company") {
var code = blockReg.testPromoBlockCommand(5050, 77)
code should be("22")
}
scenario("User should not be blocked for a unlisted promo code and listed company") {
var code = blockReg.testPromoBlockCommand(5050, 123)
code should not be ("22")
}
scenario("User should not be blocked for a listed promo code and unlisted company") {
var code = blockReg.testPromoBlockCommand(5318, 77)
code should not be ("22")
}
scenario("User should not be blocked for a unlisted promo code and unlisted company") {
var code = blockReg.testPromoBlockCommand(5318, 123)
code should not be ("22")
}
© 2014, Conversant, Inc. All rights reserved.
Scala Runner
$ scala -cp scalatest_2.11-2.2.4.jar org.scalatest.run ExampleSpec
Discovery starting.
Discovery completed in 21 milliseconds.
Run starting. Expected test count is: 2
ExampleSpec:
A Stack
- should pop values in last-in-first-out order
- should throw NoSuchElementException if an empty stack is popped
Run completed in 76 milliseconds.
Total number of tests run: 2
Suites: completed 1, aborted 0
Tests: succeeded 2, failed 0, canceled 0, ignored 0, pending 0
All tests passed.
© 2014, Conversant, Inc. All rights reserved.
Reporting
Note: This report is generated by the FREE Scala Plugin of
Intellij
© 2014, Conversant, Inc. All rights reserved.
References / Links
http://www.scala-lang.org/download/
http://scalatest.org/download
http://doc.scalatest.org/2.2.4/index.html#org.scalatest.package
http://www.scala-lang.org/api/current/#package
http://scala-tools.org/mvnsites/maven-scala-plugin/
Downloads
Guides
Plugin
© 2014, Conversant, Inc. All rights reserved.
Q&A
Pls send your questions and comments
to
rthanavarapu@conversantmedia.com

Scala for Test Automation

  • 1.
    © 2014, Conversant,Inc. All rights reserved. PRESENTED BY 6/1/15 Ultimate Automation - ScalaTest Software Test Automation on JVM Platform lRajesh Thanavarapu
  • 2.
    © 2014, Conversant,Inc. All rights reserved. Software Test Automation “In software testing, test automation is the use of special software (separate from the software being tested) to control the execution of tests and the comparison of actual outcomes with predicted outcomes.[1] Test automation can automate some repetitive but necessary tasks in a formalized testing process already in place, or add additional testing that would be difficult to perform manually.” - From Wikipedia, the free encyclopedia
  • 3.
    © 2014, Conversant,Inc. All rights reserved. Test Automation Classification * Code-driven Test Automation * GUI Test Automation * API Test Automation <- Topic of discussion
  • 4.
    © 2014, Conversant,Inc. All rights reserved. Ultimate Automation Tool * Associate Tests with Specs * Facilitates simplistic Test design * Provides complete Data abstraction * Documentation that is up to date * Allows for easy enhancements * Provides visibility to all teams – Business, Prod, Dev, QA & The Management
  • 5.
    © 2014, Conversant,Inc. All rights reserved. Too Many Names TDD ATDD BDD FDD DDD MDD R2D2 C3PO
  • 6.
    © 2014, Conversant,Inc. All rights reserved. Specification Testing * One Source - Tests, Specs, Documentation, Reports * Everybody wins – Stake holders, Business, Product, Developers & Testers
  • 7.
    © 2014, Conversant,Inc. All rights reserved. Scala * Shares the JVM Space * JIT Compiled and Bytecode * Supports both OOP & FP * Higher Order Language * Aggressive Road map – Java 8 * Complete Maven Integration * BSD Licensed
  • 8.
    © 2014, Conversant,Inc. All rights reserved. Java => Scala Improvements: A non-unified type system (primitives vs. objects), type erasure (polymorphism), and checked exceptions. Additions: Functional programming concepts- type inference, anonymous functions (closures), lazy initialization etc.
  • 9.
    © 2014, Conversant,Inc. All rights reserved. Scala Is Concise Java public class Person { private String firstName; private String lastName; // Let's not forget the getters and setters OK? Person(String firstName, String lastName) { this.firstName=firstName; this.lastName=lastName; } } Scala class Person(firstName:String, lastName:String)
  • 10.
    © 2014, Conversant,Inc. All rights reserved. Scala Is Implicit Java public class ClassicSingleton { private static ClassicSingleton instance = null; protected ClassicSingleton() { // Exists only to defeat instantiation. } public static ClassicSingleton getInstance() { if(instance == null) { instance = new ClassicSingleton(); } return instance; } } Scala Object ClassicSingleton{ }
  • 11.
    © 2014, Conversant,Inc. All rights reserved. Scala Is True FP Lambda Expressions val even = ( x :Int ) => { X % 2 == 0 } even(7) Output: false Theory Var res=f(x) Var res=f(x) + (f(y) + f(z)) Var res=f(x)=f(a) * f(b) Best Curry(ing) in town...
  • 12.
    © 2014, Conversant,Inc. All rights reserved. Scala better than Java? Scala will not replace Java, but it truly Compliments!!
  • 13.
    © 2014, Conversant,Inc. All rights reserved. Big Companies using Scala
  • 14.
    © 2014, Conversant,Inc. All rights reserved. ScalaTest * API based on Scala – external jar * Simple and English like DSL * Traits for everyone (Java 8 mixin) FeatureSpec FlatSpec FunSpec and more...
  • 15.
    © 2014, Conversant,Inc. All rights reserved. Trait FeatureSpec class SampleClass extends FeatureSpec { feature("The user can pop an element off the top of the stack") { scenario("pop is invoked on a non-empty stack") (pending) scenario("pop is invoked on an empty stack"){ … } ignore("pop is invoked on an empty stack") { … } } }
  • 16.
    © 2014, Conversant,Inc. All rights reserved. Impl. Detailsprotected def feature(description: String)(fun: => Unit) { try { registerNestedBranch(Resources.feature(...), fun,...) } catch { case e: exceptions.TestFailedException => throw new exceptions.(...) ... } } protected def scenario(specText: String, testTags: Tag*)(testFun: FixtureParam => Registeration { engine.registerTest(Resources.scenario(...) } protected def ignore(specText: String, testTags: Tag*)(testFun: FixtureParam => Registeration) { engine.registerIgnoredTest(...) }
  • 17.
    © 2014, Conversant,Inc. All rights reserved. Trait FunSpec class SampleClass extends FunSpec { describe("A Set") { describe("when empty") { it("should have size 0") { assert(Set.empty.size == 0) } it("should produce NoSuchElementException when head is invoked") { intercept[NoSuchElementException] { Set.empty.head } } } }
  • 18.
    © 2014, Conversant,Inc. All rights reserved. Trait FlatSpec class SampleClass extends FlatSpec { "An empty Set" should "have size 0" in { assert(Set.empty.size == 0) } it should "produce NoSuchElementException when head is invoked" in { intercept[NoSuchElementException] { Set.empty.head } } }
  • 19.
    © 2014, Conversant,Inc. All rights reserved. Many Styles to Choose http://scalatest.org/user_guide /selecting_a_style
  • 20.
    © 2014, Conversant,Inc. All rights reserved. Test Development * Define a comprehensive list of test Scenarios * Initially write all tests as “Pending” * Incrementally add test code (worker methods) Language of choice: Scala, Java or Both * Review test results * Add more test Scenarios...
  • 21.
    © 2014, Conversant,Inc. All rights reserved. Sample Code feature("Block user from registering to listed promotion codes") { scenario("User should be blocked for a listed promo code and listed company") { var code = blockReg.testPromoBlockCommand(5050, 77) code should be("22") } scenario("User should not be blocked for a unlisted promo code and listed company") { var code = blockReg.testPromoBlockCommand(5050, 123) code should not be ("22") } scenario("User should not be blocked for a listed promo code and unlisted company") { var code = blockReg.testPromoBlockCommand(5318, 77) code should not be ("22") } scenario("User should not be blocked for a unlisted promo code and unlisted company") { var code = blockReg.testPromoBlockCommand(5318, 123) code should not be ("22") }
  • 22.
    © 2014, Conversant,Inc. All rights reserved. Scala Runner $ scala -cp scalatest_2.11-2.2.4.jar org.scalatest.run ExampleSpec Discovery starting. Discovery completed in 21 milliseconds. Run starting. Expected test count is: 2 ExampleSpec: A Stack - should pop values in last-in-first-out order - should throw NoSuchElementException if an empty stack is popped Run completed in 76 milliseconds. Total number of tests run: 2 Suites: completed 1, aborted 0 Tests: succeeded 2, failed 0, canceled 0, ignored 0, pending 0 All tests passed.
  • 23.
    © 2014, Conversant,Inc. All rights reserved. Reporting Note: This report is generated by the FREE Scala Plugin of Intellij
  • 24.
    © 2014, Conversant,Inc. All rights reserved. References / Links http://www.scala-lang.org/download/ http://scalatest.org/download http://doc.scalatest.org/2.2.4/index.html#org.scalatest.package http://www.scala-lang.org/api/current/#package http://scala-tools.org/mvnsites/maven-scala-plugin/ Downloads Guides Plugin
  • 25.
    © 2014, Conversant,Inc. All rights reserved. Q&A Pls send your questions and comments to rthanavarapu@conversantmedia.com