29
31

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Pythonのパッケージ開発メモ

Posted at

ディレクトリツリー

bash-3.2$ tree
.
├── hoge
│   ├── Hoge.py
│   └── __init__.py
├── setup.py
└── test
    ├── TestHoge.py
    └── __init__.py

  • ./hogeにパッケージのソースを入れる
  • ./testに単体テストコードを入れる
  • それぞれに__init__.pyを用意(中身は空で良い)
    • Pythonは、sys.pathの各ディレクトリに置かれた「ファイルそれ自体」か、「サブディレクトリで__init__.pyファイルを含む」をimport対象のパッケージと認識する

setup.py

おまじない…。

setup.py
from setuptools import setup, find_packages



setup(
    name = "hoge",
    version = "0.1",
    packages = find_packages(),
    test_suite = 'test'
)

単体テストコード

unittestを使う。

TestHoge.py
import unittest
from hoge import Hoge


class TestPiyo(unittest.TestCase):
    def setUp(self):
        self.obj = Hoge.Piyo()

    def test_one(self):
        self.assertEqual(1, self.obj.one())

    def test_two(self):
        self.assertEqual("two", self.obj.two())

単体テストの実行

Hoge.pyを適当に開発して、

python setup.py test

で、単体テストが実行される。

29
31
0

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
29
31

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?