25
© 2014, Conversant, Inc. All rights reserved. PRESENTED BY 6/1/15 Ultimate Automation - ScalaTest Software Test Automation on JVM Platform l Rajesh Thanavarapu

Scala for Test Automation

Embed Size (px)

Citation preview

Page 1: Scala for Test Automation

© 2014, Conversant, Inc. All rights reserved.

PRESENTED BY

6/1/15

Ultimate Automation - ScalaTestSoftware Test Automation on JVM Platform

l Rajesh Thanavarapu

Page 2: Scala for Test Automation

© 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

Page 3: Scala for Test Automation

© 2014, Conversant, Inc. All rights reserved.

Test Automation Classification

* Code-driven Test Automation* GUI Test Automation* API Test Automation <- Topic of discussion

Page 4: Scala for Test Automation

© 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

Page 5: Scala for Test Automation

© 2014, Conversant, Inc. All rights reserved.

Too Many NamesTDDATDDBDDFDDDDDMDDR2D2C3PO

Page 6: Scala for Test Automation

© 2014, Conversant, Inc. All rights reserved.

Specification Testing* One Source - Tests, Specs, Documentation, Reports

* Everybody wins – Stake holders, Business, Product, Developers & Testers

Page 7: Scala for Test Automation

© 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

Page 8: Scala for Test Automation

© 2014, Conversant, Inc. All rights reserved.

Java => ScalaImprovements: 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.

Page 9: Scala for Test Automation

© 2014, Conversant, Inc. All rights reserved.

Scala Is ConciseJavapublic 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; }}

Scalaclass Person(firstName:String, lastName:String)

Page 10: Scala for Test Automation

© 2014, Conversant, Inc. All rights reserved.

Scala Is ImplicitJavapublic 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; }}

ScalaObject ClassicSingleton{}

Page 11: Scala for Test Automation

© 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...

Page 12: Scala for Test Automation

© 2014, Conversant, Inc. All rights reserved.

Scala better than Java?

Scala will not replace Java, but it truly Compliments!!

Page 13: Scala for Test Automation

© 2014, Conversant, Inc. All rights reserved.

Big Companies using Scala

Page 14: Scala for Test Automation

© 2014, Conversant, Inc. All rights reserved.

ScalaTest* API based on Scala – external jar* Simple and English like DSL* Traits for everyone (Java 8 mixin)

FeatureSpecFlatSpecFunSpec and more...

Page 15: Scala for Test Automation

© 2014, Conversant, Inc. All rights reserved.

Trait FeatureSpecclass 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") { … } }}

Page 16: Scala for Test Automation

© 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(...) }

Page 17: Scala for Test Automation

© 2014, Conversant, Inc. All rights reserved.

Trait FunSpecclass 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 } } }}

Page 18: Scala for Test Automation

© 2014, Conversant, Inc. All rights reserved.

Trait FlatSpecclass 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 } }}

Page 19: Scala for Test Automation

© 2014, Conversant, Inc. All rights reserved.

Many Styles to Choose

http://scalatest.org/user_guide/selecting_a_style

Page 20: Scala for Test Automation

© 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...

Page 21: Scala for Test Automation

© 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") }

Page 22: Scala for Test Automation

© 2014, Conversant, Inc. All rights reserved.

Scala Runner$ scala -cp scalatest_2.11-2.2.4.jar org.scalatest.run ExampleSpecDiscovery starting.Discovery completed in 21 milliseconds.Run starting. Expected test count is: 2ExampleSpec:A Stack- should pop values in last-in-first-out order- should throw NoSuchElementException if an empty stack is poppedRun completed in 76 milliseconds.Total number of tests run: 2Suites: completed 1, aborted 0Tests: succeeded 2, failed 0, canceled 0, ignored 0, pending 0All tests passed.

Page 23: Scala for Test Automation

© 2014, Conversant, Inc. All rights reserved.

Reporting

Note: This report is generated by the FREE Scala Plugin of Intellij

Page 24: Scala for Test Automation

© 2014, Conversant, Inc. All rights reserved.

References / Linkshttp://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

Page 25: Scala for Test Automation

© 2014, Conversant, Inc. All rights reserved.

Q&A

Pls send your questions and comments to

[email protected]