Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?

RESTful API サーバの自動テスト案

更新:2013-02-28
バージョン:0.0.2
作者:@voluntas
URL:http://voluntas.github.com/

概要

fab コマンドを実行すると Webmachine サーバを起動して、その後 pytest 経由で requests による API テストを実行。その後 Webmachine を終了するというのを作ってみました。

まずはローカルで動かすためのコマンドです。

Webmachine のコード
https://github.com/voluntas/snowflake/tree/feature/webmachine

やってることはたいした事じゃ無いです。

  1. snowflake のフォルダまで移動して make devrel を実行する
  2. dev/snowflake/bin/snowflake start で Webmachine を起動する
  3. snowpack (Webmachine のテストが入っているフォルダ) まで移動して py.test を実行する
  4. 結果に関係なく snowflake のフォルダまで移動して Webmachine を終了する

実行結果

$ fab webmachine
[localhost] local: make devrel
==> snowflake (compile)
==> snowflake (xref)
mkdir -p dev
mkdir -p deps
(cd rel && rm -rf ../dev/snowflake && ../rebar generate target_dir=../dev/snowflake)
==> rel (generate)
rm -rf dev/snowflake/lib/eredis-* && ln -sf /Users/v/work/snowflake/deps/eredis dev/snowflake/lib;  rm -rf dev/snowflake/lib/erlando-* && ln -sf /Users/v/work/snowflake/deps/erlando dev/snowflake/lib;  rm -rf dev/snowflake/lib/hackney-* && ln -sf /Users/v/work/snowflake/deps/hackney dev/snowflake/lib;  rm -rf dev/snowflake/lib/ibrowse-* && ln -sf /Users/v/work/snowflake/deps/ibrowse dev/snowflake/lib;  rm -rf dev/snowflake/lib/lhttpc-* && ln -sf /Users/v/work/snowflake/deps/lhttpc dev/snowflake/lib;  rm -rf dev/snowflake/lib/mimetypes-* && ln -sf /Users/v/work/snowflake/deps/mimetypes dev/snowflake/lib;  rm -rf dev/snowflake/lib/mochiweb-* && ln -sf /Users/v/work/snowflake/deps/mochiweb dev/snowflake/lib;  rm -rf dev/snowflake/lib/webmachine-* && ln -sf /Users/v/work/snowflake/deps/webmachine dev/snowflake/lib;
rm -rf dev/snowflake/lib/snowflake-*
rm -rf dev/snowflake/lib/snowflake
mkdir dev/snowflake/lib/snowflake
ln -sf /Users/v/work/snowflake/ebin dev/snowflake/lib/snowflake/ebin
ln -sf /Users/v/work/snowflake/priv dev/snowflake/lib/snowflake/priv
[localhost] local: dev/snowflake/bin/snowflake start
[localhost] local: py.test
========================================= test session starts =========================================
platform darwin -- Python 2.7.3 -- pytest-2.3.4
plugins: cov
collected 1 items

users_test.py .

====================================== 1 passed in 0.42 seconds =======================================
[localhost] local: dev/snowflake/bin/snowflake stop
ok

Done.

コード

users_test.py

import simplejson as json

import requests

def test_users():
    """
    """

    url = 'http://localhost:8080/users/spam/'
    payload = {'password': 'eggs'}
    headers = {'content-type': 'application/json'}

    r = requests.put(url, data=json.dumps(payload), headers=headers)
    assert r.status_code == 201
    assert json.loads(r.text) == {'user_id': 'spam', 'password': 'eggs'}

    r = requests.get(url)
    assert r.status_code == 200
    assert json.loads(r.text) == {'user_id': 'spam', 'password': 'eggs'}

fabfile.py

from fabric.api import local, lcd

def webmachine():
    with lcd('/Users/v/work/snowflake'):
        local('make devrel')
        local('dev/snowflake/bin/snowflake start')

    with lcd('/Users/v/work/snowpack'):
        local('py.test')

    with lcd('/Users/v/work/snowflake'):
        local('dev/snowflake/bin/snowflake stop')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment