SlideShare uma empresa Scribd logo
1 de 20
Baixar para ler offline
Scala Test

Meetu Maltiar - mmaltiar@inphina.com
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 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)
    }

}
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): 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)
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.
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
Summary


Scala Test is a low risk way to get started
➢


with Scala
➢
 Scala Test shows what Scala can
leverage for our projects
References

➢
 Bill Venners presentation “Get Higher
With Scala Test”
➢
    Scala Test website www.scalatest.org

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

Code Kata: String Calculator in Flex
Code Kata: String Calculator in FlexCode Kata: String Calculator in Flex
Code Kata: String Calculator in Flex
 
Unit Testing with Python
Unit Testing with PythonUnit Testing with Python
Unit Testing with Python
 
All about unit testing using (power) mock
All about unit testing using (power) mockAll about unit testing using (power) mock
All about unit testing using (power) mock
 
Python testing
Python  testingPython  testing
Python testing
 
Introduction to JUnit
Introduction to JUnitIntroduction to JUnit
Introduction to JUnit
 
Python unit testing
Python unit testingPython unit testing
Python unit testing
 
05 junit
05 junit05 junit
05 junit
 
Java Unit Test - JUnit
Java Unit Test - JUnitJava Unit Test - JUnit
Java Unit Test - JUnit
 
Unit testing
Unit testingUnit testing
Unit testing
 
Unit testing with JUnit
Unit testing with JUnitUnit testing with JUnit
Unit testing with JUnit
 
TestNG vs Junit
TestNG vs JunitTestNG vs Junit
TestNG vs Junit
 
JUnit Pioneer
JUnit PioneerJUnit Pioneer
JUnit Pioneer
 
testng
testngtestng
testng
 
Junit and testNG
Junit and testNGJunit and testNG
Junit and testNG
 
Java Unit Testing
Java Unit TestingJava Unit Testing
Java Unit Testing
 
Unit Testing - Nakov's Talk @ VarnaConf 2013
Unit Testing - Nakov's Talk @ VarnaConf 2013Unit Testing - Nakov's Talk @ VarnaConf 2013
Unit Testing - Nakov's Talk @ VarnaConf 2013
 
New Microsoft Word Document.doc
New Microsoft Word Document.docNew Microsoft Word Document.doc
New Microsoft Word Document.doc
 
Krazykoder struts2 interceptors
Krazykoder struts2 interceptorsKrazykoder struts2 interceptors
Krazykoder struts2 interceptors
 
API Performance Testing
API Performance TestingAPI Performance Testing
API Performance Testing
 
JUnit 5
JUnit 5JUnit 5
JUnit 5
 

Destaque

OPEN Houston Financials - 2013
OPEN Houston Financials - 2013OPEN Houston Financials - 2013
OPEN Houston Financials - 2013Zahid Rahman
 
Managing Development
Managing DevelopmentManaging Development
Managing DevelopmentAbby CHI
 
Scala categorytheory
Scala categorytheoryScala categorytheory
Scala categorytheoryMeetu Maltiar
 
Data structures in scala
Data structures in scalaData structures in scala
Data structures in scalaMeetu Maltiar
 
Getting Started With Scala
Getting Started With ScalaGetting Started With Scala
Getting Started With ScalaMeetu Maltiar
 
Teaching Students with Emojis, Emoticons, & Textspeak
Teaching Students with Emojis, Emoticons, & TextspeakTeaching Students with Emojis, Emoticons, & Textspeak
Teaching Students with Emojis, Emoticons, & TextspeakShelly Sanchez Terrell
 
Hype vs. Reality: The AI Explainer
Hype vs. Reality: The AI ExplainerHype vs. Reality: The AI Explainer
Hype vs. Reality: The AI ExplainerLuminary Labs
 
Study: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving CarsStudy: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving CarsLinkedIn
 

Destaque (11)

OPEN Houston Financials - 2013
OPEN Houston Financials - 2013OPEN Houston Financials - 2013
OPEN Houston Financials - 2013
 
Managing Development
Managing DevelopmentManaging Development
Managing Development
 
Fitnesse With Scala
Fitnesse With ScalaFitnesse With Scala
Fitnesse With Scala
 
Scala categorytheory
Scala categorytheoryScala categorytheory
Scala categorytheory
 
Data structures in scala
Data structures in scalaData structures in scala
Data structures in scala
 
Akka 2.0 Reloaded
Akka 2.0 ReloadedAkka 2.0 Reloaded
Akka 2.0 Reloaded
 
Getting Started With Scala
Getting Started With ScalaGetting Started With Scala
Getting Started With Scala
 
Introducing Akka
Introducing AkkaIntroducing Akka
Introducing Akka
 
Teaching Students with Emojis, Emoticons, & Textspeak
Teaching Students with Emojis, Emoticons, & TextspeakTeaching Students with Emojis, Emoticons, & Textspeak
Teaching Students with Emojis, Emoticons, & Textspeak
 
Hype vs. Reality: The AI Explainer
Hype vs. Reality: The AI ExplainerHype vs. Reality: The AI Explainer
Hype vs. Reality: The AI Explainer
 
Study: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving CarsStudy: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving Cars
 

Semelhante a Scala test

Testing Spark and Scala
Testing Spark and ScalaTesting Spark and Scala
Testing Spark and Scaladatamantra
 
S313352 optimizing java device testing with automatic feature discovering
S313352 optimizing java device testing with automatic feature discoveringS313352 optimizing java device testing with automatic feature discovering
S313352 optimizing java device testing with automatic feature discoveringromanovfedor
 
Strategy-driven Test Generation with Open Source Frameworks
Strategy-driven Test Generation with Open Source FrameworksStrategy-driven Test Generation with Open Source Frameworks
Strategy-driven Test Generation with Open Source FrameworksDimitry Polivaev
 
Spring 3.1 and MVC Testing Support
Spring 3.1 and MVC Testing SupportSpring 3.1 and MVC Testing Support
Spring 3.1 and MVC Testing SupportSam Brannen
 
We Are All Testers Now: The Testing Pyramid and Front-End Development
We Are All Testers Now: The Testing Pyramid and Front-End DevelopmentWe Are All Testers Now: The Testing Pyramid and Front-End Development
We Are All Testers Now: The Testing Pyramid and Front-End DevelopmentAll Things Open
 
Grails unit testing
Grails unit testingGrails unit testing
Grails unit testingpleeps
 
Renaissance of JUnit - Introduction to JUnit 5
Renaissance of JUnit - Introduction to JUnit 5Renaissance of JUnit - Introduction to JUnit 5
Renaissance of JUnit - Introduction to JUnit 5Jimmy Lu
 
How Scala promotes TDD
How Scala promotes TDDHow Scala promotes TDD
How Scala promotes TDDShai Yallin
 
Describe's Full of It's
Describe's Full of It'sDescribe's Full of It's
Describe's Full of It'sJim Lynch
 
Unit Testing in .NET Core 7.0 with XUnit.pptx
Unit Testing in .NET Core 7.0 with XUnit.pptxUnit Testing in .NET Core 7.0 with XUnit.pptx
Unit Testing in .NET Core 7.0 with XUnit.pptxKnoldus Inc.
 
MT_01_unittest_python.pdf
MT_01_unittest_python.pdfMT_01_unittest_python.pdf
MT_01_unittest_python.pdfHans Jones
 
Quick tour to front end unit testing using jasmine
Quick tour to front end unit testing using jasmineQuick tour to front end unit testing using jasmine
Quick tour to front end unit testing using jasmineGil Fink
 
Intro to Testing in Zope, Plone
Intro to Testing in Zope, PloneIntro to Testing in Zope, Plone
Intro to Testing in Zope, PloneQuintagroup
 

Semelhante a Scala test (20)

Testing Spark and Scala
Testing Spark and ScalaTesting Spark and Scala
Testing Spark and Scala
 
S313352 optimizing java device testing with automatic feature discovering
S313352 optimizing java device testing with automatic feature discoveringS313352 optimizing java device testing with automatic feature discovering
S313352 optimizing java device testing with automatic feature discovering
 
Strategy-driven Test Generation with Open Source Frameworks
Strategy-driven Test Generation with Open Source FrameworksStrategy-driven Test Generation with Open Source Frameworks
Strategy-driven Test Generation with Open Source Frameworks
 
Good Practices On Test Automation
Good Practices On Test AutomationGood Practices On Test Automation
Good Practices On Test Automation
 
Gallio Crafting A Toolchain
Gallio Crafting A ToolchainGallio Crafting A Toolchain
Gallio Crafting A Toolchain
 
Spring 3.1 and MVC Testing Support
Spring 3.1 and MVC Testing SupportSpring 3.1 and MVC Testing Support
Spring 3.1 and MVC Testing Support
 
We Are All Testers Now: The Testing Pyramid and Front-End Development
We Are All Testers Now: The Testing Pyramid and Front-End DevelopmentWe Are All Testers Now: The Testing Pyramid and Front-End Development
We Are All Testers Now: The Testing Pyramid and Front-End Development
 
Grails unit testing
Grails unit testingGrails unit testing
Grails unit testing
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Annotations
AnnotationsAnnotations
Annotations
 
Automation tips
Automation tipsAutomation tips
Automation tips
 
Renaissance of JUnit - Introduction to JUnit 5
Renaissance of JUnit - Introduction to JUnit 5Renaissance of JUnit - Introduction to JUnit 5
Renaissance of JUnit - Introduction to JUnit 5
 
TestNG Framework
TestNG Framework TestNG Framework
TestNG Framework
 
How Scala promotes TDD
How Scala promotes TDDHow Scala promotes TDD
How Scala promotes TDD
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
Describe's Full of It's
Describe's Full of It'sDescribe's Full of It's
Describe's Full of It's
 
Unit Testing in .NET Core 7.0 with XUnit.pptx
Unit Testing in .NET Core 7.0 with XUnit.pptxUnit Testing in .NET Core 7.0 with XUnit.pptx
Unit Testing in .NET Core 7.0 with XUnit.pptx
 
MT_01_unittest_python.pdf
MT_01_unittest_python.pdfMT_01_unittest_python.pdf
MT_01_unittest_python.pdf
 
Quick tour to front end unit testing using jasmine
Quick tour to front end unit testing using jasmineQuick tour to front end unit testing using jasmine
Quick tour to front end unit testing using jasmine
 
Intro to Testing in Zope, Plone
Intro to Testing in Zope, PloneIntro to Testing in Zope, Plone
Intro to Testing in Zope, Plone
 

Último

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesBoston Institute of Analytics
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 

Último (20)

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 

Scala test

  • 1. Scala Test Meetu Maltiar - mmaltiar@inphina.com
  • 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.
  • 8. Pick a Core Trait
  • 9. Mix In Other Traits
  • 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
  • 19. Summary Scala Test is a low risk way to get started ➢ with Scala ➢ Scala Test shows what Scala can leverage for our projects
  • 20. References ➢ Bill Venners presentation “Get Higher With Scala Test” ➢ Scala Test website www.scalatest.org