DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

Trending

  • How to Load Cypress Chrome Extension
  • AWS Multi-Region Resiliency Aurora MySQL Global DB With Headless Clusters
  • SRE vs. DevOps
  • Microservices: Quarkus vs Spring Boot
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. A Simple TDD Environment in Haskell

A Simple TDD Environment in Haskell

Learn the code to create a home for Test Driven Development in Haskell as well as where to get the resources to make it easier.

Liam Griffin user avatar by
Liam Griffin
·
Jun. 05, 16 · Tutorial
Like (1)
Save
Tweet
Share
4.44K Views

Join the DZone community and get the full member experience.

Join For Free

I recently implemented the bowling kata in Haskell. In the process, I found out how set up my environment to comfortably do Test Driven Development. Hopefully, others might find this post helpful to begin their journey with the language. I used the following components:

  • Haskell installation: Haskell Platform. This also gives you GHCi which you can use as an interactive environment and type inspector.
  • IDE: Any editor would suffice, but I used Visual Studio Code as they have an extension for Haskell that gave me some basic IntelliSense features.
  • Test libraries: Hspec, which is based on RSpec. This can be installed using Haskell's package manager, cabal, from the command line with cabal install hspec.
  • Helper libraries: Printf for colourful command line output.

Using the example from Hspec's documentation, I began with this structure for my code:

BowlingTests.hs

module BowlingTests where

import Bowling

import Test.Hspec
import Text.Printf (printf)

testScoreGame :: String -> Int -> Spec
testScoreGame game score =
  it (printf “should return the score for game : %s → %d \n” game score) $
    scoreGame game `shouldBe` score

main = hspec $ do
  describe "scoreGame" $ do 
    testScoreGame "--------------------" 0

So to test a function, you add a function in your test file, usually the same name with a ‘test’ prefix. This function takes the inputs to your function under test and the expected output as parameters. Then using Hspec you describe what you are testing. As you can see the ‘it’ part is written in the test function. You can of course omit this helper function and write all your tests under main = hspec $ do, which may be nicer if you want to describe in more detail what each individual test is testing.

Bowling.hs:

These files are in the same directory, now I can run my tests from the command line.

$ runhaskell BowlingTests.hs

scoreGame
should return the score for game : — — — — — — — — — — → 0

Finished in 0.0000 seconds
1 example, 0 failures

There you have it. Now I can focus on writing a failing test and making it pass.

This article was first published on the Codurance blog.

Haskell (programming language)

Published at DZone with permission of , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • How to Load Cypress Chrome Extension
  • AWS Multi-Region Resiliency Aurora MySQL Global DB With Headless Clusters
  • SRE vs. DevOps
  • Microservices: Quarkus vs Spring Boot

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: