pyenv install --list インストールできるも縺?
pyenv install 3.8.8 指定verをインス繧?
pyenv global 3.8.8 デフォルトに指定
.python-versionファイルをGITに鐔??せ管理したい?
pipenv縺?Pipfile縺?Pipfile.lockを利用しpip縺?requrements.txtを用いるよりも強力
Pipfile縺?Pipfile.lock縺?requirementsをGITに鐔??せ管理したい?
pipenv --python 3.8.8 など最初縺?pyバージョンをpipfileに鐔??載
pipenv install "google-cloud-tasks==1.5.0" バージョン無しでも有りでも入れられる
Pipfileを書き觸??える方觸??
[packages]
google-cloud-tasks = "==1.5.0"
protobuf = "*"
そして臀??記cmdでインス繧?
pipenv install PipefileからインストールしPipefile.lockを更譁?
pipenv sync Pipfile.lockの最新を藹??得し環藹??更譁?(Pipefileは使繧?ない)
pipenv shell 仮想環藹??を起動
pipenv run python main.py
他縺?
pipenv uninstall google-cloud-tasks アンインス繧?
Pipfile, Pipfile.lockがあれ縺? pip sync縺?OKだがrequirements.txtも使える
pipenv lock -r > requirements.txt 生成
pipenv install -r requirements.txt
pipenvのバージョンが古いと臀??存関臀??、Ver整合性で問題が起きやすい
pipenv --version
pip install pipenv
pipenv update
pipenv upgrade <パ繧?>でやり直す
■assertでテスト
assert文は軆??み込み藹??謨?__debug__がTrueの時のみ藹??行されます
実行コマンドにオプション縺?-Oをつける縺?__debug__がFalseになりassert文が無効縺?
def func_so(a, b):
c = a * b
return
def test():
assert(func_so(1,2) == 2)
if __name__ == "__main__":
test()
main()
■テスト駆動
PyTest を LLMに書いてもらいたい。下記のようなプロンプトで觸??備できるので縺??
https://aaaa にアクセスし名前欄縺?aaaと入力すると名前欄に英数が入っていますとエラーが出る
■PyTest
assert
成立すべき蠑?(Trueになるべき蠑?) をassert文で鐔??霑?
テストの觸??備と藹??処理
@pytest.fixtureデコレータをつける
実行(ディレクトリ縺?test、testファイル、test 関数が対象)
pytest
テストカバレッジを確鐔??:tests/ディレクトリ内の全テストを実行し現在のディレクトリ内のコードについてどれだけテストでカバーされているかを測藹??
pytest -covs=. tests/
■test app.pyでエラー表示を拾ってテスト
import pytest
from app import app
@pytest.fixture
def client():
app.config['TESTING'] = True
with app.test_client() as client:
yield client
def test_valid_input(client):
response = client.post(
'/',
data={'name': 'TestUser', 'email': 'Test@example.com'},
follow_redirects=True
)
assert b'OKでっせ' in response.data
def test_invalid_name(client):
response = client.post(
'/',
data={'name': 'ThisNameIsTooLong', 'email': 'test@example.com'},
follow_redirects=True
)
assert b'name At most 10 characters long' in response.data
def test_invalid_email(client):
response = client.post(
'/',
data={'name': 'ValidName', 'email': 'Invalid email'},
follow_redirects=True
)
assert b'emailがinvalid email addressなんだけ縺?' in response.data
■パラメータを複数種類
import pytest
@pytest.mark.parametrize(
"x, y", [
("aaa", "bbb"),
("aaa", "aaa"),
("bbb", "bbb")
]
)
def test_1(x, y):
assert x == y
■fixture: fixture@yieldまでの処理> テスト本臀??> fixture縺?yield後からreturnまでの処理
import pytest
from pathlib import Path
import shutil
def create_file(path):
# 指定されたパスにファイルを作成する関謨?
path.touch()
# 一時ディレクトリを作成するフィクスチ繝?
@pytest.fixture()
def create_tmp_dir():
# 一時ディレクトリを作成
tmp_dir = Path("/tmp/test")
if not tmp_dir.exists():
tmp_dir.mkdir()
yield tmp_dir
# 一時ディレクトリを削髯?
shutil.rmtree(tmp_dir)
def test_create_file(create_tmp_dir):
target_file = create_tmp_dir / "test.txt"
create_file(target_file)
assert target_file.exists()
■個蛻?
import dataclasses
import datetime
pip install pyyaml > import yaml
pip install requests > import requests
Python + VSCode の環藹??構築 20240604 (zenn.dev)↓本家
/// BANGBOO BLOG /// - Python