It's been 4 days since I started writing javascript tests. While investigating various test frameworks, it's all about things like what's happening now, but I'll make a note of it when it's organized.
There are many places that are missing below, so I will update it from time to time..
language | Library management tool | Test framework | Communication around(API testing) | Mock library |
---|---|---|---|---|
Python | pip, easy_install | unittest, py.test | requests | mock |
node.js | npm | mocha, chai | supertest, superagent | sinon |
That's all I've finally touched. It seems that there are testems, phantomjs, and many other things, but I haven't caught up yet (tears).
Create an environment that does not depend on the outside.
Python
Python uses virtualenv and pip.
mkdir PROJECT
cd PROJECT
virtualenv -p python2.7 venv
source venv/bin/activate
pip install py.test requests mock
node.js
node.js is npm! I once wrote a detailed installation method on blog. For reference. If it becomes. Speaking of which, I wrote it in coffeescript.
mkdir PROJECT
cd PROJECT
npm init
npm install coffee-script mocha chai supertest superagent sinon --save-dev
An example of the test code will be written in the API acquisition test and the test using the mock below. The test code is assumed to be under 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
By the way, from version-1.7.0 of coffee-script
, it seems that you have to change the option when compiling to coffee-script / register.
Python
#API acquisition test
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 acquisition test
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
If you want to test a function that returns a random number, write an example that defines and asserts a mock to return a random function to a constant.
Python
#Function to be tested
from random import random
def foo(x):
return random() * x
#test
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
#Function to be tested
foo = (x) ->
return Math.random() * x
#test
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
I tried to correlate roughly. I will write the tests around the GUI from now on, so I will explore it by groping. I would like to compare the RSpec test of ruby. When I feel like it, [blog](http://pydiary.bitbucket. May write in org /).
Recommended Posts