A quick comparison of Python and node.js test libraries

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..

Comparison

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).

Test environment construction

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

Run the test code

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.

Testing around communication

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

Testing with a mock

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

A quick comparison of Python and node.js test libraries
Build and test a CI environment for multiple versions of Python
Connect a lot of Python or and and
Thorough comparison of three Python morphological analysis libraries
Simple comparison of Python libraries that operate Excel
Comparison of R and Python writing (Euclidean algorithm)
[Python] A rough understanding of iterators, iterators, and generators
A discussion of the strengths and weaknesses of Python
Comparison of Python and Ruby (Environment / Grammar / Literal)
List of Python libraries for data scientists and data engineers
Comparison table of frequently used processes of Python and Clojure
Comparison of CoffeeScript with JavaScript, Python and Ruby grammar
Python --Quick start of logging
Comparison of 4 Python web frameworks
Python debug and test module
Python 3 sorted and comparison functions
Comparison of Apex and Lamvery
Source installation and installation of Python
Detect objects of a specific color and size with Python
Comparison of how to use higher-order functions in Python 2 and 3
Python: Create a dictionary from a list of keys and values
The websocket of toio (nodejs) and python / websocket do not connect.
Python a + = b and a = a + b are different
A record of patching a python package
Environment construction of python and opencv
The story of Python and the story of NaN
A good description of Python decorators
Speed comparison of Wiktionary full text processing with F # and Python
Java and Python basic grammar comparison
Installation of SciPy and matplotlib (Python)
[Python] A memorandum of beautiful soup4
A brief summary of Python collections
This and that of python properties
[Python] return A [or / and] B
A quick guide to PyFlink that combines Apache Flink and Python
Speed comparison of Python XML parsing
Summary of differences between Python and PHP (comparison table of main items)
Coexistence of Python2 and 3 with CircleCI (1.0)
Summary of Python indexes and slices
A Python beginner first tried a quick and easy analysis of weather data for the last 10 years.
[Python] Basic pattern and usage of if statement (comparison operator and Boolean operator)
Reputation of Python books and reference books
Test & Debug Tips: Create a file of the specified size in Python
Python code to train and test with Custom Vision of Cognitive Service
Code reading of faker, a library that generates test data in Python
A rough understanding of python-fire and a memo
(Java, JavaScript, Python) Comparison of string processing
Write the test in a python docstring
Display a list of alphabets in Python 3
Extraction of tweet.js (json.loads and eval) (Python)
Make a relation diagram of Python module
6 Python libraries for faster development and debugging
Test of uniqueness in paired comparison method
Comparison of class inheritance and constructor description
[Python] A quick web application with Bottle!
[python] Get a list of instance variables
[python] [meta] Is the type of python a type?
A story about Python pop and append
Comparison of Python serverless frameworks-Zappa vs Chalice
Comparison of L1 regularization and Leaky Relu
Easy introduction of python3 series and OpenCV3