Giter Site home page Giter Site logo

liujianping / silk Goto Github PK

View Code? Open in Web Editor NEW

This project forked from matryer/silk

0.0 2.0 0.0 865 KB

Markdown based document-driven RESTful API testing.

Home Page: http://silktest.org/

License: GNU General Public License v2.0

Shell 3.61% Go 96.39%

silk's Introduction

Silk logo

silk Build Status

Markdown based document-driven web API testing.

Learn more

Video of Mat Ryer speaking about Silk

(VIDEO) Watch the talk about Silk (with slides) or read about Silk in this blog post.

Example Silk test file

Markdown API

Tests are made up of documents written in Markdown.

  • # Group - Top level headings represent groups of requests
  • ## GET /path - Second level headings represent a request
  • Code blocks with three back tics represent bodies
  • * Field: value - Lists describe headers and assertions
  • * ?param=value - Request parameters
  • === seperators break requests from responses
  • Comments (starting with //) are ignored
  • Plain text is ignored to allow you to add documentation
  • Inline back tics are ignored and are available for formatting

Document structure

A document is made up of:

  • A request
  • === seperator
  • Assertions

Requests

A request starts with ## and must have an HTTP method, and a path:

## METHOD /path

Examples include:

## GET /people

## POST /people/1/comments

## DELETE /people/1/comments/2

Request body (optional)

To specify a request body (for example for POST requests) use a codeblock using backtics (```):

```
{"name": "Silk", "release_year": 2016}
```

Request headers (optional)

You may specify request headers using lists (prefixed with *):

* Content-Type: "application/json"
* X-Custom-Header: "123"

Request parameters (optional)

Adding parameters to the path (like GET /path?q=something) can be tricky, especially when you consider escaping etc. To address this, Silk supports parameters like lists:

* ?param=value

The parameters will be correctly added to the URL path before the request is made.

Assertions

Following the === separator, you can specify assertions about the response. At a minimum, it is recommended that you assert the status code to ensure the request succeeded:

  * Status: 200

You may also specify response headers in the same format as request headers:

  * Content-Type: "application/json"
  * X-MyServer-Version: "v1.0"

If any of the headers do not match, the test will fail.

Validating data

You can optionally include a verbatim body using code blocks surrounded by three back tics. If the response body does not exactly match, the test will fail:

```
{"id": 1, "name": "Silk", "release_year": 2016}
```

You may also make any number of regex assertions against the body using the Body object:

  * Body: /Hello world/
  * Body: /This should be found too/
  * Body: /and this/

Alternatively, you can specify a list (using *) of data fields to assert accessible via the Data object:

  * Status: 201
  * Content-Type: "application/json"
  * Data.name: "Silk"
  * Data.release_year: 2016
  * Data.tags[0]: "testing"
  * Data.tags[1]: "markdown"
  * Data[0].name: "Mat"
  * Data[1].name: "David"
  • NOTE: Currenly this feature is only supported for JSON APIs.

Regex

Values may be regex, if they begin and end with a forward slash: /. The assertion will pass if the value (after being turned into a string) matches the regex.

  * Status: /^2.{2}$/
  * Content-Type: /application/json/

The above will assert that:

  • The status looks like 2xx, and
  • The Content-Type contains application/json

Command line

The silk command runs tests against an HTTP endpoint.

Usage:

silk -silk.url="{endpoint}" {testfiles}
  • {url} the endpoint URL (e.g. http://localhost:8080)
  • {testfiles} path to test files (e.g. ./testfiles)

Notes:

  • Omit trailing slash from url
  • {testfiles} can include a pattern (e.g. /path/*.silk.md)

Golang

Silk is written in Go and integrates seamlessly into existing testing tools and frameworks. Import the runner package and use RunGlob to match many test files:

package project_test

import (
  "testing"
  "github.com/matryer/silk/runner"
)

func TestAPIEndpoint(t *testing.T) {
  // start a server
  s := httptest.NewServer(yourHandler)
  defer s.Close()
  
  // run all test files
  runner.New(t, s.URL).RunGlob(filepath.Glob("../testfiles/failure/*.silk.md"))
}

Credit

  • Special thanks to @dahernan for his contributions and criticisms of Silk
  • Silk logo by Chris Ryer

silk's People

Contributors

matryer avatar buddhamagnet avatar dahernan avatar katre avatar

Watchers

JayL avatar James Cloos avatar

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.