雑多な技術系メモ

自分用のメモ。内容は保証しません。よろしくお願いします。

pytestについてのメモ

PyTestの階層問題

以下のようなディレクトリ構成の時、

.
├── main.py
└── tests
    └── test_main.py

ルートディレクトリから

pytest

を行うためには、tests/に"init"ファイルを置いておくと 上記のコードを実行できる

つまり、以下のようになる。

.
├── main.py
└── tests
    ├── __init__.py  # new
    └── test_main.py

簡単なテストサンプル

main.py

from flask import Flask, render_template

app = Flask(__name__)

@app.route("/", methods=["GET"])
def index():
    if request.method == "GET":
        return render_template("index.html")


if __name__ == "__main__":
    app.run(threaded=True)

test_main.py

import unittest
import sys
import main


class TestMain(unittest.TestCase):

    def setUp(self):
        self.app = main.app.test_client()

    def test_index(self):
        response = self.app.get('/')
        assert response.status_code == 200

if __name__ == '__main__':
    unittest.main()