fitnesse with scala

28
FitNesse With Scala Meetu Maltiar Principal Consultant Email: [email protected] Twitter:@meetumaltiar

Upload: meetu-maltiar

Post on 15-Jan-2015

1.954 views

Category:

Technology


4 download

DESCRIPTION

My Knowledge session at Knoldus on Fitnesse with Scala

TRANSCRIPT

Page 1: Fitnesse With Scala

FitNesse With Scala

Meetu MaltiarPrincipal Consultant

Email: [email protected]:@meetumaltiar

Page 2: Fitnesse With Scala

FitNesseIt is an open-source automated framework created

for software testing

It is excellent tool for collaboration in software development enabling developers, testers and customers to collaborate by writing tests

FitNesse is based on Ward Cunningham's framework for Integration Test (FIT) now developed by Robert C Martin

Page 3: Fitnesse With Scala

FitNesse Getting Stared

Download fitnesse-standalone.jar from fitnesse.org

Starting Fitnesse is by executing the jar file:

java -jar fitnesse-standalone.jar -p 9090

Note: Doing first time will create directories

You can now access FitNesse on URL: localhost:9090

Page 4: Fitnesse With Scala

FitNesse Overview

WikiTestsSlimRunner

FixturesSystemUnder Test

Customer readableTests

FitNesse

Results

Page 5: Fitnesse With Scala

FitNesse Overview..

FitNesse works by executing Wiki pages which call custom written Fixtures

Fixtures act as a bridge between wiki pages and the System Under Test

Fixtures can be written many programming languages like java, Scala...

Page 6: Fitnesse With Scala

Tests And suitesFitNesse has Suite. It is a collection of Tests. When we execute one Suite all tests are run in a Suite.

For creating a TaxCalculationSuite, go to localhost:9090/TaxCalculationSuite.

FitNesse is smart enough to open a new Suite page. Save it by not adding any contents.

Page 7: Fitnesse With Scala
Page 8: Fitnesse With Scala
Page 9: Fitnesse With Scala

FitNesse Wiki Test

To create FitNesse wiki test, hover on add button and select “Test”

It will open a new test page. Provide TaxCalculationTest name for the Test for now, and save it.

Page 10: Fitnesse With Scala
Page 11: Fitnesse With Scala
Page 12: Fitnesse With Scala
Page 13: Fitnesse With Scala

FitNesse Wiki Test

Writing Wiki Test page is simple. We create wiki tables. Each row in a table is separated by a pipe character.

For example a table header of id, name and age is created like this|id|name|age|

Page 14: Fitnesse With Scala

FitNesse Wiki TestFor TaxCalculation test we want to calculate tax on annual salary.

Here is a simple rule of Tax. If annual income is more than 100,000 it is 15%, or else it is 10%.

The test in FitNesse Wiki must provide input to Fixture code in Scala and we must assert to compare output matched with actual results.

|the tax for annual income|200000|is|30000|

Page 15: Fitnesse With Scala

FitNesse Wiki Test|the tax for annual income|200000|is|30000| is a specification of intent from a Tester or customer. Now, we can build support around this.

For us developers, we need to specify jars, classes and Fixture class that we will be executing methods on.

For finding libraries and classes we use !path in FitNesse wiki.!define TEST_SYSTEM {slim}!path /Users/meetumaltiar/.sbt/boot/scala-2.9.1/lib/scala-library.jar!path ../lib/fitlibrary-20080812.jar

Add above content at top of the test page and save. You will notice proper classpath set.

Page 16: Fitnesse With Scala

FitNesse Wiki TestWe will have to load a specific class on which the methods will be executed.

If our Fixture scala class is TaxCalculationFixture in com.knoldus.fixture package. Then, we will have to write following in FitNesse Wiki

|import| |com.knoldus.fixture|

!|script | TaxCalculationFixture|

Page 17: Fitnesse With Scala

FitNesse Wiki TestWe need to write our test having assertions. We will use DoFixture semantics in our Wiki.

|import| |com.knoldus.fixture|

!|script | TaxCalculationFixture||the tax for annual income|200000|is|30000|

Above definition means that we have a method in scala class TaxCalculationFixture:

def theTaxForAnnualIncomeIs(annualIncome: Double, tax: Double): Boolean

Page 18: Fitnesse With Scala

FitNesse Wiki Test|import| |com.knoldus.fixture|

!|script | TaxCalculationFixture||the tax for annual income|200000|is|30000|

If we use DoFixture then each odd column is a part of a method and each even column is parameter to the method.

If method is written in camel case we can write method in FitNesse wiki like a DSL

Page 19: Fitnesse With Scala

Completed Wiki Test

!define TEST_SYSTEM {slim}!path /Users/meetumaltiar/.sbt/boot/scala-2.9.1/lib/scala-library.jar!path ../lib/fitlibrary-20080812.jar

|import| |com.knoldus.fixture|

!|script | TaxCalculationFixture||the tax for annual income|200000|is|30000||the tax for annual income|100000|is|10000|

Page 20: Fitnesse With Scala
Page 21: Fitnesse With Scala
Page 22: Fitnesse With Scala

Using DoFixture

To start with add a dependency of fitnesse and fitlibrary in your SBT project containing the fixture

libraryDependencies += "org.fitnesse" % "fitnesse" % "20121220"

libraryDependencies += "org.fitnesse" % "fitlibrary" % "20080812"

Page 23: Fitnesse With Scala

DoFixture

DoFixture is a Scala class extending DoFixture class in FitLibrary

DoFixture is a Fixture and acts as a bridge between wiki FitNesse tests and System under test

Each odd column in table in wiki corresponds to part of method definition.

Each even column has parameters we can pass in function defined in a DoFixture

Page 24: Fitnesse With Scala

TaxCalculationFixture

package com.knoldus.fixture

import fitlibrary.DoFixtureimport com.knoldus.tax.TaxCalculatorService

class TaxCalculationFixture extends DoFixture {

val taxCalculatorService = new TaxCalculatorService

def theTaxForAnnualIncomeIs(annualIncome: Double, tax: Double) = taxCalculatorService.getTaxForAnnualIncome(annualIncome) == tax}

Page 25: Fitnesse With Scala

TaxCalculatorService

package com.knoldus.tax

class TaxCalculatorService {

def getTaxForAnnualIncome(annualIncome: Double): Double = (annualIncome <= 100000.0) match { case true => annualIncome * 0.10 case false => annualIncome * 0.15 }}

Page 26: Fitnesse With Scala

Running Tests

We can execute an individual FitNesse wiki Test

We can execute all tests by running a Suite. Running a suite will run all tests in a Suite

We can organize Suites depending on behavioral design of our application.

Page 27: Fitnesse With Scala
Page 28: Fitnesse With Scala

Code is available on Knoldus Github:https://github.com/knoldus/ScalaFitnesse

Thanks!!