CATEGORY: PYTHON

unittest

概要

src(実装)と tests(テスト)を分けた構成。python3 -m unittest discovertest_*.py を自動発見して実行する。

mkdir project && cd project

mkdir src tests
touch src/modules.py

cat <<'EOF'> tests/test_sample.py
import unittest

from src import modules

class TestSample(unittest.TestCase):
    def setUp(self):
        print("setUp()")

    def tearDown(self):
        print("tearDown()")

    def test_mypackages(self):
        print("test1")
        self.assertEqual(True, True)
EOF

cat <<'EOFM'> makefile
test:
	python3 -m unittest discover tests/ "test_*.py"
EOFM

make test

make test は makefile に定義したテスト実行ターゲット。