13
13

More than 5 years have passed since last update.

Python と node.js のテストライブラリを簡単に比較してみた

Last updated at Posted at 2014-02-05

javascript のテストを書き始めて 4 日目のしょしんしゃです.
いろいろなテストフレームワークを調べながら今更? みたいなことばかりですが整理がてらメモしておきます.

以下書き足りない所も多いので随時更新していきます.

比較

言語 ライブラリ管理ツール テストフレームワーク 通信まわり(API のテストとか) モックライブラリ
Python pip, easy_install unittest, py.test requests mock
node.js npm mocha, chai supertest, superagent sinon

いまやっと触ったのはこれくらいです. testem とか phantomjs とか他にもいろいろあるみたいですがまだ追いついていません(涙)

テスト環境構築

外部依存しない環境を作ります.

Python

Python は virtualenv と pip を使っています.

mkdir PROJECT
cd PROJECT
virtualenv -p python2.7 venv
source venv/bin/activate
pip install py.test requests mock

node.js

node.js は npm ですね! 詳しいインストール方法をブログに書いたことがありました. 参考になれば.
そういえば coffeescript で書いています.

mkdir PROJECT
cd PROJECT
npm init
npm install coffee-script mocha chai supertest superagent sinon --save-dev

テストコードを走らせる

テストコードの例は以下 API 取得テスト, モックを使ったテストに書いていきます.
テストコードは test/ 以下に置いている想定です.

Python

source venv/bin/activate
py.test tests/*.py -v -k

node.js

./node_modules/mocha/bin/mocha test/*.coffee --compilers coffee:coffee-script/register -R list 

ちなみに coffee-script の version-1.7.0 から, コンパイルするときのオプションは coffee-script/register に変更しないとみたいですね.

通信周りのテスト

Python

# API 取得テスト
import unittest
import requests

class TestAPI(unittest.TestCase):

    def get_api(self):
        res = requests.get(
            '/api/status',
            headers={'Content-Type': 'application/json'},
            data='{"name":"hoge"}'
        )
        return res

    def test_sample(self):
        res = self.get_api()
        self.assertEqual(res.status_code, 200)
        self.assertIsInstance(res.json()['age'], int)
        self.assertEqual(res.json()['age'], 25)

node.js

# API 取得テスト
assert = require('assert')
request = require('superagent')

describe 'test: ', ->
  it 'testing for API', (done) ->
    request.get('/api/status')
      .set('Content-Type', 'application/json')
      .end (error, res) ->
        if error
          return done(err)
        assert.equal(res.status, 200)
        assert.typeOf(res.body['age'], 'number')
        assert.equal(res.body['age'], 25)
        done()
        return

モックを使ったテスト

ランダムな数字を返す関数をテストしたい場合, ランダム関数を定数に返すようにモックを定義してアサートする例を書きます.

Python

# テスト対象の関数
from random import random
def foo(x):
    return random() * x

# テスト
import unittest
from mock import Mock

class TestMock(unittest.TestCase):

    def test_sample(self):
        random = Mock()
        random.return_value = 0.5
        self.assertEqual(foo(10), 5) 

node.js

# テスト対象の関数
foo = (x) ->
    return Math.random() * x

# テスト
assert = require('assert')
sinon = require('sinon')

describe 'test: ', ->
  it 'testing for foo()', (done) ->
    stub = sinon.stub(Math, 'random')
    stub.returns(0.5)
    assert.equal(foo(10), 5)
    done()
    return

ざっくり相関してみました. GUI まわりのテストはこれから書くので手探りで調べていきます. ruby の RSpec のテストも比較してみたいなぁ. 気が向いた時にブログに書くかもしれません.

13
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
13