# Behavior-driven-development with daml-cucumber

**URL:** <https://forum.canton.network/t/behavior-driven-development-with-daml-cucumber/7115>\
**Category:** Outreach\
**Tags:** daml, test, test-script\
**Created:** [February 15, 2024, 4:08pm UTC](https://forum.canton.network/t/behavior-driven-development-with-daml-cucumber/7115 "2024-02-15T16:08:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ali-abrar](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/ali-abrar/32/7038_2.png) [@ali-abrar](https://forum.canton.network/u/ali-abrar)\
**Post date:** [February 15, 2024, 4:08pm UTC](https://forum.canton.network/t/behavior-driven-development-with-daml-cucumber/7115/1 "2024-02-15T16:08:16Z")

</div>

We ([Obsidian Systems](https://obsidian.systems)) have been using [cucumber gherkin specifications](https://cucumber.io/docs/gherkin/reference/) on some of our daml-based projects. They’re great for building specifications that can be read both by a human and by gherkin tooling. What we lacked, however, was a tool that allowed us to write gherkin test implementations in daml script.

We decided to create such a tool, and are pleased to announce the release of [daml-cucumber](https://github.com/obsidiansystems/daml-cucumber). Here’s a small example (using the default skeleton daml app) of how the tool works. First, you’ll need a gherkin feature file:

```haskell
Feature: Asset

  Scenario: An asset can be issued
    Given an issuer party
    When the issuer creates an asset with the issuer as the owner
    Then an asset is created owned by the issuer

```

Next, you’ll need to import the [cucumber library for daml](https://github.com/obsidiansystems/daml-cucumber/blob/c47753c7a3790eb5537d078bf5aa425810a4c969/daml/Cucumber.daml).

Now you can start implementing your gherkin scenario steps in daml script:

```haskell
-- Given an issuer party
given_an_issuer : Cucumber Ctx ()
given_an_issuer = do
  p <- liftScript $ allocateParty "issuer"
  modify $ \ctx -> ctx { issuer = Some p }

-- When the issuer creates an asset with the issuer as the owner
issuer_creates_asset_for_self : Cucumber Ctx (ContractId M.Asset)
issuer_creates_asset_for_self = do
  Some p <- gets issuer
  assetId <- liftScript $ submit p $ createCmd M.Asset with
    issuer = p
    owner = p
    name = "TV"
  modify $ \ctx -> ctx { knownAssetId = Some assetId }
  pure assetId

-- Then an asset is created owned by the issuer
asset_owner_is_issuer : Cucumber Ctx ()
asset_owner_is_issuer = do
  Some issuer <- gets issuer
  [asset] <- liftScript $ query @M.Asset issuer
  assertMsg "Expected asset owner to be the issuer" $
    asset._2.owner == issuer

```

The `Ctx` there is scenario context that you can read from and write to as you perform steps (so that you can, e.g., store the party you created in a previous step and look it up again later).

The daml-cucumber executable allows you to run tests associated with your feature file. It will report successes, failures (with error messages), and missing steps. E.g.:

```haskell
Feature: Asset

  Scenario: An asset can be issued
    Given an issuer party => OK
    When the issuer creates an asset with the issuer as the owner => OK
    Then an asset is created owned by the issuer => OK
...

Tests Passed ✅
  Features: 1 of 1 passed
  Scenarios: 8 of 8 passed
  Steps: 29 of 29 passed

```

You can also use daml-cucumber to generate the a daml script that can be inspected in vscode or run with `daml test`, in case you need to dig into why a particular test is failing.

For more information about setting up daml-cucumber, and a more detailed example, check out the github repository: [GitHub - obsidiansystems/daml-cucumber: Cucumber testing for daml scripts](https://github.com/obsidiansystems/daml-cucumber)

---

<div class="post-metadata">

**Author:** ![bernhard](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/bernhard/32/356_2.png) [@bernhard](https://forum.canton.network/u/bernhard)\
**Post date:** [February 21, 2024, 12:11pm UTC](https://forum.canton.network/t/behavior-driven-development-with-daml-cucumber/7115/2 "2024-02-21T12:11:44Z")

</div>

Hi @ali-abrar , Thank you so much for sharing this with the community and expanding the Daml ecosystem. It looks incredibly useful and I can’t wait to try it out myself some time.
