A yaml data-driven testing format together with golang testing library

Overview

Specimen

Yaml-based data-driven testing

Specimen is a yaml data format for data-driven testing. This enforces separation between feature being tested the data used for testing.

It comes with a golang implementation for loading the data, checking its format, running your golang test boxes (called code boxed) and comparing the result with the expected one.

It supports using the FOCUS and SKIP flag in the data to run only part of the test data.

Overview

overview of the way the specimen library works

Getting started

To get started, create a directory it/ and the three files it.go it_test.go and it_testdata.yaml. For each file, copy the content of linked section. Finally, run go test in the it/ directory.

mkdir it
cd it
touch it.go it_test.go it_testdata.yaml

Finally:

go mod init it
go mod tidy
go test

You should get an output like this one:

TestIt:
2 slab-s succeeded over 2. (0 failed)
PASS
ok      it

Yaml Data

The yaml data file looks like this:

content:
  -
    box: turn_page
    case:
      -
        flag: FOCUS
        input:
          book:
            title: aleph
            left_page: 0
            size: 90
          turn_page_count: 4
      -
        flag: SKIP
        input:
          book:
            title: aleph
            left_page: 0
            size: 90
          turn_page_count: 4
        output:
          title: aleph
          left_page: 8
          size: 90
        info:
          expected_left_page: 8
  -
    flag: FOCUS
    box: get_page
    case:
      -
        input:
          book:
            title: aleph
            left_page: 44
            size: 90
        output: 44

The input entry is required and must be a map. The info entry is optional but must be a map if present. Both the input map and the info map are passed to the test box. The output entry may be any value but it must only be present if the box returns a value. The flags are case insensitive, though it is recommanded to use UPPERCASE to make them more visible. The supported flags are FOCUS and SKIP. The flag entry may be present both at the slab level and at the case level.

Code box

A code box is an adapter between the parsed data and the library the code being tested. It takes as input the input map and the optional info map. A code box looks like this:

package it_test

import (
    "it"
    "testing"

    "github.com/ditrit/specimen/go/specimen"
)


func TestIt(t *testing.T) {
    specimen.Run(
        t,
        []specimen.File{
          specimen.ReadLocalFile("it_testdata.yaml"),
        },
        specimen.CodeboxSet(
            specimen.NewCodebox("turn_page", turn_page_box),
            specimen.NewCodebox("get_page", get_page_box),
        ),
    )
}

// what a code box can look like

func turn_page_box(s *specimen.S, input map[string]interface{}, info map[string]interface{}) interface{} {
    book_data := input["book"].(map[string]interface{})
    book := it.Book{
        Title:    book_data["title"].(string),
        LeftPage: book_data["left_page"].(int),
        Size:     book_data["size"].(int),
    }
    book.TurnPage(input["turn_page_count"].(int))

    return map[string]interface{}{
        "title":     book.Title,
        "left_page": book.LeftPage,
        "size":      book.Size,
    }
}


// the output of a code box can be anything

func get_page_box(s *specimen.S, input map[string]interface{}, info map[string]interface{}) interface{} {
    book_data := input["book"].(map[string]interface{})
    book := it.Book{
        Title:    book_data["title"].(string),
        LeftPage: book_data["left_page"].(int),
        Size:     book_data["size"].(int),
    }
    return book.GetPage()
}

// note: if no output comparison is expected by the box, the box must return nil

Example package code

package it

type Book struct {
	Title    string
	LeftPage int
	Size     int
}

func (b *Book) TurnPage(count int) {
	b.LeftPage += 2 * count

	if b.LeftPage < 0 {
		b.LeftPage = 0
	} else if b.LeftPage >= b.Size {
		b.LeftPage = b.Size - 1
	}
}

func (b *Book) GetPage() int {
	return b.LeftPage
}

Yaml Schema

The content of a yaml test data file must match the main rule of the lidy schema below:

main:
  _mapFacultative:
    flag: string # any test run flag associated with the file
    extra: any # any information you need to store. This can be used as a neutral place to store aliased data
  _map:
    content: {listOf: databox}
databox:
  _mapFacultative:
    name: string # the yaml name of the databox. It is mentioned in the error report if present
    flag: string # any test run flag associated with the databox
    extra: any
  _map:
    box: string # the name of the code box as declared in the code
    case: {_listOf: slab} # the data to be passed to the code box
slab:
  _mapFacultative:
    name: string
    flag: string
    info: {_mapOf: {string: any}}
    output: any
    extra: any
  _map:
    input: {_mapOf: {string: any}}

Several databoxes can contribute their case to the same code box, though each databox contributes only to one code box

You might also like...
Gostresslib - A golang library for stress testing.

GoStressLib A golang library for stress testing. Install go get github.com/tenhan/gostresslib Usage package main import ( "github.com/tenhan/gostres

:exclamation:Basic Assertion Library used along side native go testing, with building blocks for custom assertions

Package assert Package assert is a Basic Assertion library used along side native go testing Installation Use go get. go get github.com/go-playground/

Library created for testing JSON against patterns.

Gomatch Library created for testing JSON against patterns. The goal was to be able to validate JSON focusing only on parts essential in given test cas

A Go library help testing your RESTful API application

RESTit A Go micro-framework to help writing RESTful API integration test Package RESTit provides helps to those who want to write an integration test

HTTP load testing tool and library. It's over 9000!
HTTP load testing tool and library. It's over 9000!

Vegeta Vegeta is a versatile HTTP load testing tool built out of a need to drill HTTP services with a constant request rate. It can be used both as a

A WebDriver client and acceptance testing library for Go
A WebDriver client and acceptance testing library for Go

Agouti Agouti is a library for writing browser-based acceptance tests in Google Go. It provides Gomega matchers and plays nicely with Ginkgo or Spec.

Go library for testing async behavior

Helpers for testing async behavior.

Tesuto - a little library for testing against HTTP services

tesuto import "github.com/guregu/tesuto" tesuto is a little library for testing

A modern generic testing assertions library for Go

test test is a generics based testing assertions library for Go. There are two packages, test and must. test - assertions that mark the test for failu

Owner
Design it, Run it
DevOps for real IT
Design it, Run it
Markdown based document-driven RESTful API testing.

silk Markdown based document-driven web API testing. Write nice looking Markdown documentation (like this), and then run it using the silk command Sim

Mat Ryer 933 Nov 13, 2022
Simple test driven approach in "GOLANG"

Testing in GOLANG Usage Only test go test -v Coverage go test -cover or go test -coverprofile=coverage.out go tool cover -html=coverage.out Benchmark

mahdi imani 0 Dec 5, 2021
Behaviour Driven Development tests generator for Golang

gherkingen It's a Behaviour Driven Development (BDD) tests generator for Golang. It accept a *.feature Cucumber/Gherkin file and generates a test boil

Maxim Krivchun 50 Sep 27, 2022
ESME is a go library that allows you to mock a RESTful service by defining the configuration in json format

ESME is a go library that allows you to mock a RESTful service by defining the configuration in json format. This service can then simply be consumed by any client to get the expected response.

Sumit Tokkar 3 Mar 2, 2021
Learn Go with test-driven development

Learn Go with Tests Art by Denise Formats Gitbook EPUB or PDF Translations 中文 Português 日本語 한국어 Support me I am proud to offer this resource for free,

Chris James 18.8k Nov 27, 2022
This repository includes consumer driven contract test for provider, unit test and counter api.

This repository includes consumer driven contract test for provider, unit test and counter api.

Ahmet Zümberoğlu 0 Feb 1, 2022
Test your code without writing mocks with ephemeral Docker containers 📦 Setup popular services with just a couple lines of code ⏱️ No bash, no yaml, only code 💻

Gnomock – tests without mocks ??️ Spin up entire dependency stack ?? Setup initial dependency state – easily! ?? Test against actual, close to product

Yury Fedorov 987 Nov 21, 2022
Configuration management tool inspired by Ansible, but with no yaml - just Go

GOSSH This repo is an experiement with creating a declarative IT automation and configuration management package for Golang. Think Ansible, but no Yam

Kristoffer Lorentsen 26 Oct 31, 2022
siusiu (suite-suite harmonics) a suite used to manage the suite, designed to free penetration testing engineers from learning and using various security tools, reducing the time and effort spent by penetration testing engineers on installing tools, remembering how to use tools.

siusiu (suite-suite harmonics) a suite used to manage the suite, designed to free penetration testing engineers from learning and using various security tools, reducing the time and effort spent by penetration testing engineers on installing tools, remembering how to use tools.

Re 288 Nov 10, 2022
Fortio load testing library, command line tool, advanced echo server and web UI in go (golang). Allows to specify a set query-per-second load and record latency histograms and other useful stats.

Fortio Fortio (Φορτίο) started as, and is, Istio's load testing tool and now graduated to be its own project. Fortio is also used by, among others, Me

Fortio (Φορτίο) 2.7k Nov 17, 2022