📂my_python_tdd/
┣ 📂app/
┃ ┣ 📜__init__.py
┃ ┗ 📜app_logic.py
┣ 📂data/
┃ ┗ 📜data.xml
┣ 📂test/
┃ ┗ 📜test_app_logic.py
┗ 📜requirements.txt
app_logic.py
import xml.etree.ElementTree as ET
class AppLogic:
def __init__(self, file_path: str):
self.file_path = file_path
def load_data(self) -> dict:
"""
XMLファイルからデータを読み込む関数。
Returns:
dict: XMLデータを格納した辞書
"""
tree = ET.parse(self.file_path)
root = tree.getroot()
data = {child.tag: child.text for child in root}
return data
test_app_logic.py
import unittest
from app.app_logic import AppLogic
class TestAppLogic(unittest.TestCase):
def setUp(self):
self.app = AppLogic("data/data.xml")
def test_load_data(self):
result = self.app.load_data()
self.assertIsInstance(result, dict)
self.assertGreater(len(result), 0)
launch.json
を以下のように編集します。設定ファイルを作成するためにVSCodeのデバッグアクティビティの機能を利用する方法を以下に説明します。
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: <https://go.microsoft.com/fwlink/?linkid=830387>
"version": "0.2.0",
"configurations": [
{
"name": "Python: Unittest",
"type": "python",
"request": "launch",
"justMyCode": true,
"module": "unittest",
"cwd": "${workspaceFolder}",
"args": [
"-v"
]
}
]
}
これにより launch.json
ファイルが作成され、デバッグ設定が保存されます。設定されたデバッグを開始するには、"Run and Debug" 画面で "Python: Unittest" を選択して "Start Debugging" をクリックします。
以上で、VSCodeのデバッグ機能を利用してテスト設定を行いました。この設定により、テストは test
ディレクトリ内の全てのファイルに対して行われます。そしてテスト結果が VSCode のデバッグコンソールに表示されます。