Skip to content

Reduce duplicate test code with centralized TEST_FILE_PATH constant - #191

Merged
arm61 merged 2 commits into
mainfrom
copilot/reduce-duplicate-test-code
Oct 21, 2025
Merged

Reduce duplicate test code with centralized TEST_FILE_PATH constant#191
arm61 merged 2 commits into
mainfrom
copilot/reduce-duplicate-test-code

Conversation

Copilot AI commented Oct 20, 2025

Copy link
Copy Markdown
Contributor

Problem

Test files throughout the codebase contained significant code duplication with the same path construction pattern repeated 30+ times:

import os
import kinisi

file_path = os.path.join(os.path.dirname(kinisi.__file__), 'tests/inputs/example_XDATCAR.gz')

This approach had several drawbacks:

  • Verbose and repetitive code across multiple test files
  • Difficult to maintain if the test data location changes
  • Mixed use of os.path instead of modern pathlib

Solution

Implemented @PythonFZ's suggestion to create a centralized TEST_FILE_PATH constant:

  1. Added a constant in kinisi/tests/__init__.py:
from pathlib import Path

TEST_FILE_PATH = Path(__file__).parent / 'inputs'
  1. Updated all 9 test files to import and use this constant:
from kinisi.tests import TEST_FILE_PATH

# Now much cleaner:
file_path = TEST_FILE_PATH / 'example_XDATCAR.gz'
msd_load = sc.io.load_hdf5(TEST_FILE_PATH / 'example_msd2.hdf5')

Benefits

  • Single source of truth: Test file paths are defined in one place
  • Reduced duplication: Eliminated 30+ instances of the same pattern
  • Cleaner code: Shorter, more readable path references using pathlib's / operator
  • Better maintainability: Easier to update test data location if needed in the future
  • Modern approach: Uses pathlib.Path instead of os.path operations

Changes

  • Created TEST_FILE_PATH constant in kinisi/tests/__init__.py
  • Updated 9 test files: test_analyzer.py, test_ase.py, test_conductivity_analyzer.py, test_diffusion.py, test_diffusion_analyzer.py, test_jump_diffusion_analyzer.py, test_mdanalysis.py, test_parser.py, test_pymatgen.py
  • Removed unnecessary kinisi module imports where only used for path construction
  • Net change: 49 insertions(+), 50 deletions(-) - actually reduced total lines of code

Co-authored-by: PythonFZ 46721498+PythonFZ@users.noreply.github.com

Original prompt

This section details on the original issue you should resolve

<issue_title>Reduce duplicate test code</issue_title>
<issue_description>@PythonFZ has suggested the following as a way to reduce code duplication in tests.

from pathlib import Path

TEST_FILE_PATH = Path(__file__).parent / "inputs"

def test_():
   sc.io.load_hdf5(TEST_FILE_PATH / "example_msd2.hdf5")
```</issue_description>

## Comments on the Issue (you are @copilot in this section)

<comments>
</comments>

Fixes #190


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

@github-actions

github-actions Bot commented Oct 20, 2025

Copy link
Copy Markdown

☂️ Python Coverage

current status: ✅

Overall Coverage

Lines Covered Coverage Threshold Status
1941 1844 95% 0% 🟢

New Files

No new covered files...

Modified Files

File Coverage Status
kinisi/tests/_init_.py 100% 🟢
kinisi/tests/test_analyzer.py 100% 🟢
kinisi/tests/test_ase.py 100% 🟢
kinisi/tests/test_conductivity_analyzer.py 100% 🟢
kinisi/tests/test_diffusion.py 100% 🟢
kinisi/tests/test_diffusion_analyzer.py 100% 🟢
kinisi/tests/test_jump_diffusion_analyzer.py 100% 🟢
kinisi/tests/test_mdanalysis.py 100% 🟢
kinisi/tests/test_parser.py 100% 🟢
kinisi/tests/test_pymatgen.py 100% 🟢
TOTAL 100% 🟢

updated for commit: 9a87be7 by action🐍

Co-authored-by: PythonFZ <46721498+PythonFZ@users.noreply.github.com>
Comment thread kinisi/tests/__init__.py
Copilot AI changed the title [WIP] Refactor test code to reduce duplication Reduce duplicate test code with centralized TEST_FILE_PATH constant Oct 20, 2025
Copilot AI requested a review from PythonFZ October 20, 2025 15:17
@PythonFZ
PythonFZ marked this pull request as ready for review October 21, 2025 08:06

@PythonFZ PythonFZ left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@PythonFZ
PythonFZ requested a review from arm61 October 21, 2025 08:06
@arm61
arm61 merged commit cb71b09 into main Oct 21, 2025
8 checks passed
@arm61
arm61 deleted the copilot/reduce-duplicate-test-code branch October 30, 2025 09:20
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.

Reduce duplicate test code

3 participants