Skip to content

Use pytest tmp_path fixture instead of writing test outputs to repository#5

Draft
Copilot wants to merge 3 commits intomainfrom
copilot/refactor-test-fixtures-to-tmp-path
Draft

Use pytest tmp_path fixture instead of writing test outputs to repository#5
Copilot wants to merge 3 commits intomainfrom
copilot/refactor-test-fixtures-to-tmp-path

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Feb 14, 2026

Tests in test_adiftools.py were writing output files directly to the tests/ directory, leaving artifacts in the repository tree. This replaces the custom txt_file fixture with pytest's built-in tmp_path fixture.

Changes

  • Removed custom fixture: Deleted txt_file fixture and unused imports (tempfile, Path, Generator, Any)
  • Modified test functions: Updated 4 functions to accept tmp_path parameter:
    • test_to_adi
    • test_plot_monthly
    • test_plot_band_percentage
    • test_call_to_txt
  • Improved assertions: Changed assert True to assert out.exists() to verify file creation
  • Removed TODO comment: Resolved "use test fixture to create a temporary file"

Example

# Before
def test_to_adi(prep_instance):
    prep_instance.to_adi('tests/sample_out.adi')
    assert True

# After
def test_to_adi(prep_instance, tmp_path):
    out = tmp_path / 'sample_out.adi'
    prep_instance.to_adi(str(out))
    assert out.exists()

Test outputs now write to pytest-managed temporary directories that are automatically cleaned up after execution.

Original prompt

目的

  • tests/test_adiftools.py にある TODO コメント "use test fixture to create a temporary file" を解消し、テストがリポジトリツリー内にファイルを書き出さないように修正する。

変更内容(要実装)

  1. 一時ファイル用の独自 fixture を廃止し、標準の pytest tmp_path fixture を使うようにする。これによりテストは一時ディレクトリに出力を行い、テスト実行ごとにクリーンに保たれる。

  2. 以下のテスト関数を修正して出力先に tmp_path を使う:

    • test_to_adi(prep_instance) -> 引数に tmp_path を追加し、出力ファイルを tmp_path / 'sample_out.adi' に変更。テストのアサーションをファイルが作成されていることに変更(assert out.exists())。
    • test_plot_monthly(prep_instance) -> tmp_path を受け取り、出力ファイルを tmp_path / 'monthly_qso_test.png' に変更。作成確認を追加。
    • test_plot_band_percentage(prep_instance) -> tmp_path を受け取り、出力ファイルを tmp_path / 'percentage_band_test.png' に変更。作成確認を追加。
    • test_call_to_txt(prep_instance, txt_file) -> txt_file fixture を使わず tmp_path を受け取り、出力ファイルを tmp_path / 'calls.txt' に変更。作成確認を追加。
  3. カスタム fixture txt_file とそれに関連する import(tempfile, Path, typing.Generator)を削除する。

  4. ファイル末尾の TODO コメントを削除する。

  5. 既存の他テストはそのまま維持(parametrize 部分、gl2latlon 等)。

期待される差分(一例)

--- a/tests/test_adiftools.py
+++ b/tests/test_adiftools.py
@@
-import pytest
-import tempfile
-from pathlib import Path
-from typing import Any, Generator
+import pytest
+from typing import Any
@@
-@pytest.fixture(scope='function')
-def txt_file() -> Generator[Path, Any, None]:

  • ''' csv tempfile '''
  • path = Path(tempfile.NamedTemporaryFile(suffix='.txt', delete=False).name)
  • yield path
  • delete tempfile after test

  • path.unlink()
    +# use pytest tmp_path fixture for temporary files
    @@
    -def test_to_adi(prep_instance):
  • prep_instance.to_adi('tests/sample_out.adi')
  • assert True
    +def test_to_adi(prep_instance, tmp_path):
  • out = tmp_path / 'sample_out.adi'
  • prep_instance.to_adi(str(out))
  • assert out.exists()
    @@
    -def test_plot_monthly(prep_instance):
  • prep_instance.plot_monthly('tests/monthly_qso_test.png')
  • assert True
    +def test_plot_monthly(prep_instance, tmp_path):
  • out = tmp_path / 'monthly_qso_test.png'
  • prep_instance.plot_monthly(str(out))
  • assert out.exists()
    @@
    -def test_plot_band_percentage(prep_instance):
  • prep_instance.plot_band_percentage('tests/percentage_band_test.png')
  • assert True
    +def test_plot_band_percentage(prep_instance, tmp_path):
  • out = tmp_path / 'percentage_band_test.png'
  • prep_instance.plot_band_percentage(str(out))
  • assert out.exists()
    @@
    -def test_call_to_txt(prep_instance, txt_file):
  • prep_instance.call_to_txt(txt_file)
  • assert True
    +def test_call_to_txt(prep_instance, tmp_path):
  • out = tmp_path / 'calls.txt'
  • prep_instance.call_to_txt(str(out))
  • assert out.exists()
    @@
    -# TODO: use test fixture to create a temporary file
    +# (TODO solved: use pytest tmp_path for temporary files)

注意点

  • prep_instance によるファイル読み取り(tests/sample.adi)はそのまま fixtures が参照するため問題ありません。今回の修正はテストが生成するファイルをリポジトリツリーに残さないことに主眼を置きます。
  • 変更後は CI 上でテストが失敗しないか確認してください(依存環境により生成処理で外部コマンドや GUI 要素に依存していないか等)。

作業内容(PR 作成時の要約)

  • tests/test_adiftools.py: テストでの一時ファイル作成を pytest の tmp_path fixture に置き換え、リポジトリ内にファイルを残さないように修正。不要なカスタム fixture と TODO コメントを削除。

よろしくお願いします。

This pull request was created from Copilot chat.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits February 14, 2026 09:25
…epository

Co-authored-by: JS2IIU-MH <146515386+JS2IIU-MH@users.noreply.github.com>
Co-authored-by: JS2IIU-MH <146515386+JS2IIU-MH@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor test functions to use pytest tmp_path fixture Use pytest tmp_path fixture instead of writing test outputs to repository Feb 14, 2026
Copilot AI requested a review from JS2IIU-MH February 14, 2026 09:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants