Coder Social home page Coder Social logo

cornichon-gherkin's Introduction

cornichon-gherkin

Build Status

Cornichon - Gherkin integration. Combines clean extensible functional core from Cornichon with great BDD feature definition language - Gherkin.

Basic setup

Add following SBT dependency:

libraryDependencies += "com.github.olegilyenko" %% "cornichon-gherkin" % "0.0.0"

As version indicates, it's still POC.

Now you can create a normal test class and extend GherkinBasedFeature to. The name of the feature file is inferred based on the test class name. So, for instance, for StarWarsFeature the feature fine would be starWars.feature. You can override this behaviour by overriding featureFile method. I would recommend to your *.feature files in src/test/resources folder.

Here is an example of basic test class StarWarsFeature:

class StarWarsFeature extends GherkinBasedFeature {
  lazy val stepDefinitions = Steps {
    step"I get ${strArg.ph}" { url 
      When I get(url.toString)
    }

    step"response code is $intArg" { code 
      Then assert status.is(code)
    }

    // ...

    After("@showSession") {
      show_session
    }
  }
}

And complementary feature file starWars.feature:

Feature: Star Wars API

  Scenario: check out Luke Skywalker
    When I get http://swapi.co/api/people/1/
    Then response code is 200
    And response body with whitelisting is
    """
    {
      "name": "Luke Skywalker",
      "height": "172",
      "mass": "77",
      "hair_color": "blond",
      "skin_color": "fair",
      "eye_color": "blue",
      "birth_year": "19BBY",
      "gender": "male",
      "homeworld": "http://swapi.co/api/planets/1/"
    }
    """
    And I save path 'homeworld' as 'homeworld-url'

    ...

Step Definitions

Step definitions are based on regular expressions, but StringContext-based step macro makes it type safe. Regular expressions (variable parts of the step) can only be expressed with string-interpolation variable which must be of type RegExpExtractor.

Here is an example:

step"response body at path $strArg $whitelist is: $strTableArg" { (path, wl, value) 
  Then assert body.copy(whitelist = wl).path(path).is(value)
}

lazy val whitelist: RegExpExtractor[Boolean] =
  strArg("with whitelisting").opt.transform(v  Right(v.isDefined))

As you can see, you can also transform the arguments. Each argument also can be made optional with arg.opt or accept a placeholder (like <foo-bar>) in place of this argument with arg.ph (resulting type is of type Argument[T] which can be either Value[T] or a Placeholder[T]).

Doc strings and data tables are also supported. They must appear at the end of the step definition string proceeded by colon (e.g. "...: $foo"). This last argument must be either of type TableExtractor[T] or DocStringExtractor[T].

Here is an example:

step"response body $whitelist is: $docStrArg" { (whitelist, respBody) 
  Then assert body.copy(whitelist = whitelist).is(respBody)
}

The body of step definition is just normal cornichon Step. if you would like to define several cornichon steps, just use Attach step:

step"I get ${strArg.ph}" { url 
  Attach {
    When I get(url.toString)
    Then assert status.is(200)
  }
}

Tags

Out-of-the-box supported tags:

  • @ignore - ignore Scenario or Feature
  • @pending - mark Scenario or Feature as pending (they would be ignored as well)
  • @focus - only focused scenarios are executed. All other Scenarios without this tag would be ignored.

You can define additional steps Before, After or Around scenarios annotated with particular tag. Here is an example:

Steps {
  Before("@debug") {
    show_session
  }

  After("@debug") {
    show_session
  }

  Around("@moDebugging") { steps 
    StepList {
      Then I print_step("Printing before")

      steps

      Then I print_step("Printing after")
    }
  }
}

cornichon-gherkin's People

Contributors

olegilyenko avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

telesoftas

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.