[PYTHON] I tried using parameterized

parameterized Auxiliary library for Python unit tests. When you want to test the same method with different values, you don't have to write a different test method.

It supports unittest, pytest, nose, etc. The writing style is slightly different between unittest and pytest.

I've only used unittest, so how to use it with unittest.

install

pip install parameterized

usage

Code to be tested

Just addition.

def add(a, b):
    return a + b

Test code

import unittest
from parameterized import parameterized


class TestAdd1(unittest.TestCase):

    @parameterized.expand([
        (2, 3, 5),
        (1, 1, 2),
        (3, -1, 2)
    ])

    def test_add(self, a, b, exp):
        self.assertEqual(add(a, b), exp)

Values are passed to a, b, and exp, respectively. If you want to correspond to the keyword argument, do the following. Use the param function.

import unittest
from parameterized import parameterized, param


class TestAdd2(unittest.TestCase):

    @parameterized.expand([
        param(2, -2),
        param(1, -1),
        param(3, -1, exp=2)
    ])

    def test_add(self, a, b, exp=0):
        self.assertEqual(add(a, b), exp)

Recommended Posts

I tried using parameterized
I tried using argparse
I tried using mimesis
I tried using anytree
I tried using aiomysql
I tried using Summpy
I tried using coturn
I tried using Pipenv
I tried using matplotlib
I tried using "Anvil".
I tried using Hubot
I tried using ESPCN
I tried using openpyxl
I tried using Ipython
I tried using PyCaret
I tried using cron
I tried using ngrok
I tried using face_recognition
I tried using Jupyter
I tried using PyCaret
I tried using Heapq
I tried using doctest
I tried using folium
I tried using jinja2
I tried using folium
I tried using time-window
[I tried using Pythonista 3] Introduction
I tried using easydict (memo).
I tried face recognition using Face ++
I tried using Random Forest
I tried using BigQuery ML
I tried using Amazon Glacier
I tried using git inspector
[Python] I tried using OpenPose
I tried using magenta / TensorFlow
I tried using AWS Chalice
I tried using Slack emojinator
I tried using Rotrics Dex Arm # 2
I tried using Rotrics Dex Arm
I tried using GrabCut of OpenCV
I tried using Thonny (Python / IDE)
I tried server-client communication using tmux
I tried reinforcement learning using PyBrain
I tried deep learning using Theano
Somehow I tried using jupyter notebook
[Kaggle] I tried undersampling using imbalanced-learn
I tried shooting Kamehameha using OpenPose
I tried using the checkio API
[Python] I tried using YOLO v3
I tried asynchronous processing using asyncio
I tried scraping
I tried PyQ
I tried AutoKeras
I tried papermill
I tried django-slack
I tried Django
I tried spleeter
I tried cgo
I tried using Amazon SQS with django-celery
I tried using Azure Speech to Text.
I tried using Twitter api and Line api