LoginSignup
0

More than 5 years have passed since last update.

Pythonでテストを動かす

Last updated at Posted at 2018-03-20

やること

今さらだけどPythonでテストを動かす。ここではunittestを使う。

環境

  • macOS 10.13.3
    • Python 3.6.4
    • Python 2.7

実装

ディレクトリ構成

./
 ├ calc.py
 └ tests/
  └ test_calc.py

本体の実装

calc.py
def add3(n):
    return n + 3

テストの実装

tests/test_calc.py
from unittest import TestCase
from calc import *

class CalcTest(TestCase):
    def test_add3(self):
        self.assertEqual(5, add3(2))

実行

# 単一ファイルのテストを実行するとき
$ python -m unittest tests/test_calc.py

# testsディレクトリ以下をまとめて実行するとき
$ python -m unittest discover -s tests

# 困ったとき
$ python -m unittest -h

ただし、Python2.7のとき、単一ファイル指定の方法だとImportErrorが発生した。

まとめ

ここまででは特にハマるようなケースがなかった。

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
0