Skip to content

Use pytest tmp_path for temporary test outputs in tests/test_adiftools.py#6

Merged
JS2IIU-MH merged 2 commits intomainfrom
copilot/refactor-test-fixture-usage
Feb 14, 2026
Merged

Use pytest tmp_path for temporary test outputs in tests/test_adiftools.py#6
JS2IIU-MH merged 2 commits intomainfrom
copilot/refactor-test-fixture-usage

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Feb 14, 2026

Tests were writing output files (.adi, .png, .txt) directly to the repository tree instead of temporary directories. This pollutes the working tree and can interfere with version control.

Changes

  • Removed custom txt_file fixture and unnecessary imports (tempfile, pathlib.Path, typing.Generator, typing.Any)
  • Migrated 4 tests to use pytest's tmp_path fixture: test_to_adi, test_plot_monthly, test_plot_band_percentage, test_call_to_txt
  • Improved assertions from assert True to assert out.exists() to verify file creation

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 test execution.

Original prompt

目的

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

変更内容(実装する差分)

  1. tests/test_adiftools.py の不要なインポートを削除

    • 削除: tempfile, pathlib.Path, typing.Generator(カスタム fixture 用)
  2. カスタム fixture txt_file を削除

    • 現在のコード:
      @pytest.fixture(scope='function')
      def txt_file() -> Generator[Path, Any, None]:
      path = Path(tempfile.NamedTemporaryFile(suffix='.txt', delete=False).name)
      yield path
      path.unlink()
    • 削除してコメントも削除
  3. pytest の組み込み fixture tmp_path を使用するように各テストを更新

    • test_to_adi(prep_instance) -> def test_to_adi(prep_instance, tmp_path):
      out = tmp_path / 'sample_out.adi'
      prep_instance.to_adi(str(out))
      assert out.exists()

    • test_plot_monthly(prep_instance) -> def test_plot_monthly(prep_instance, tmp_path):
      out = tmp_path / 'monthly_qso_test.png'
      prep_instance.plot_monthly(str(out))
      assert out.exists()

    • test_plot_band_percentage(prep_instance) -> 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()

    • test_call_to_txt(prep_instance, txt_file) -> def test_call_to_txt(prep_instance, tmp_path):
      out = tmp_path / 'calls.txt'
      prep_instance.call_to_txt(str(out))
      assert out.exists()

  4. ファイル末尾の TODO コメントを削除または置換

    • 置換例: "# (TODO solved: use pytest tmp_path for temporary files)" もしくは完全削除
  5. 既存のテストロジック(parametrize 部分など)はそのまま維持

追加の注意点

  • prep_instance fixture は tests/sample.adi を読み込むためそのまま残す。今回の変更はテストが生成するファイルをワーキングツリーに残さないことに主眼を置く。
  • 変更後、CI(pytest)でテストが通るか確認することを推奨する(今回の修正はテストの出力先変更に留めるため基本的には影響しないはずです)。

変更対象ファイル

  • tests/test_adiftools.py

その他のメタ情報

  • ブランチ名: refactor/use-tmp-path-tests
  • ベースブランチ: main

期待される PR タイトル

Use pytest tmp_path for temporary test outputs in tests/test_adiftools.py

よろしくお願いします。

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.

…ile fixture

Co-authored-by: JS2IIU-MH <146515386+JS2IIU-MH@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor tests to use pytest tmp_path fixture Use pytest tmp_path for temporary test outputs in tests/test_adiftools.py Feb 14, 2026
Copilot AI requested a review from JS2IIU-MH February 14, 2026 09:33
@JS2IIU-MH JS2IIU-MH marked this pull request as ready for review February 14, 2026 09:35
@JS2IIU-MH JS2IIU-MH merged commit 93ec9ac into main Feb 14, 2026
13 checks passed
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