LoginSignup
0
0

Djangoの自動テストを実行する方法について

Last updated at Posted at 2023-07-29

「Djangoで自動テストをする方法は?」

  • 独りよがりな先輩が後輩に教えています。
  • たまに後輩からの突っ込みが入ります。直接先輩には言いませんが、、、
    • ()と太字で書いています。例, (ほげほげ) (なんでやねん!)

Windowsならコマンドプロンプトで次のコマンドを実行してね!
でも「manage.py」があるフォルダーというかディレクトリで実行してね。
(全然違うディレクトリで実行していて一晩を費やした過去の自分に教えてあげたい)
(あとこの先輩、MacとWindows、Linuxだったらコマンドが違うのに全然解説せんやん、、これはググる必要があるな「Django テスト Mac」で検索っと)

bash windows バージョン
py manage.py test

これを実行するとこんな感じの画面に切り替わるよ!いっちょ前に成功したやつを載せたい!

実行結果
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
..
----------------------------------------------------------------------
Ran 2 tests in 0.070s

OK
Destroying test database for alias 'default'...

(.venv) C:\Users\taiyo\Documents\love_app\mysite>py manage.py test
Found 3 test(s).
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
...
----------------------------------------------------------------------
Ran 3 tests in 0.042s

OK
Destroying test database for alias 'default'...

でもこれって事前に自分でテストを書いておいていかないといけないわけ。
どこのファイルに書くかっていうと、、、、

「test.py」という名前のところに書くね。
場所はここであるよ!
(なんか急に中国風)

ファイル構成図
polls
│  │  admin.py
│  │  apps.py
│  │  models.py
│  │  tests.py (←ここだよ!ここ、ここ!)
│  │  urls.py
│  │  views.py
│  │  __init__.py
│  │
│  ├─migrations
│  │  │  __init__.py
│  │  │
│  │  └─__pycache__
│  │          __init__.cpython-39.pyc
│  │
│  ├─templates
│  │  └─polls
│  │          index.html
│  │          question_1.html
│  │          question_10.html
│  │          question_2.html
│  │          question_3.html
│  │          question_4.html
│  │          question_5.html
│  │          question_6.html
│  │          question_7.html
│  │          question_8.html
│  │          question_9.html
│  │          result.html
│  │
│  └─__pycache__
│          admin.cpython-39.pyc
│          apps.cpython-39.pyc
│          models.cpython-39.pyc
│          tests.cpython-39.pyc
│          urls.cpython-39.pyc
│          views.cpython-39.pyc
│          __init__.cpython-39.pyc
│
└─mysite
    │  asgi.py
    │  settings.py
    │  urls.py
    │  wsgi.py
    │  __init__.py

じゃあ、さっそく中身を書いていこうか。
何をしたいかによって、書く内容は変わるよ。(そりゃそうだろ)

どんな内容を書いたらいいかは是非ググってみてね。(急にぶん投げやがった)

でもマジで、テストしたい内容に応じて変わるんだもん。(あれ、俺の心の突っ込み漏れてね??)

次回に続く!!

参考文献

Writing and running tests
Djangoのテストでrequest.POSTに任意の値を設定する方法

0
0
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
0
0