[python] Migrate to Pytest#66
Open
hayat01sh1da wants to merge 4 commits intomasterfrom
Open
Conversation
53cfe8a to
e4753c1
Compare
c9872d0 to
5a47a48
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
1. Problem Overview
Running
pytest .in the coding-tests/python directory produced 6ModuleNotFoundErrorimport failures:2. Root Causes
sys.path.append('./src')assuming execution from the project root, but pytest runs from individual test module directoriescalculator_cli_app/src/application.pyvsletter_inspection/src/application.pycalculator_cli_app/test/test_application.pyvsletter_inspection/test/test_application.pycalculator_cli_app/src/application.pyhad globalsys.path.append()statements that interfered with other modules3. Solutions Implemented
3-1. Fixed Import Paths in All Test Files
Updated 6 test files to use
__file__-based absolute paths:Files Updated:
calculator_cli_app/test/test_application.pycalculator_cli_app/test/queries/test_calculation_query.pyfibonacci_sequence/test/test_fibonacci.pyfizzbuzz/test/test_fizzbuzz.pyletter_inspection/test/test_letter_inspection_application.pynabeatsu/test/test_nabeatsu.pyPattern Applied:
3-2. Added Module Cache Clearing
Before each import, cleared cached modules to prevent cross-module interference:
This ensures each test imports the correct
application.pyfrom its own package directory.3-3. Renamed Duplicate Test File
Renamed
letter_inspection/test/test_application.pytoletter_inspection/test/test_letter_inspection_application.pyto avoid pytest module name collision.3-4. Removed Global sys.path Modifications
Removed problematic code from
calculator_cli_app/src/application.py:Dependencies are now handled through proper test-level sys.path management.
3-5. Added all Exports
Added explicit
__all__definitions to modules with wildcard imports:fizzbuzz/src/fizzbuzz.py:__all__ = ['fizzbuzz_in_if', 'fizzbuzz_in_ternary']nabeatsu/src/nabeatsu.py:__all__ = ['go_crazy_in_if', 'go_crazy_in_ternary']fibonacci_sequence/src/fibonacci.py:__all__ = ['fibonacci']3-6. Updated pyproject.toml
Added pytest configuration to properly handle module discovery:
4. Results
✅ All 12 tests now pass successfully
calculator_cli_app/test/queries/test_calculation_query.py::TestArgumentZero::test_f_with_zero PASSED calculator_cli_app/test/queries/test_calculation_query.py::TestArgumentTwo::test_f_with_two PASSED calculator_cli_app/test/queries/test_calculation_query.py::TestArgumentFour::test_f_with_four PASSED calculator_cli_app/test/test_application.py::TestApplication::test_run_success PASSED fibonacci_sequence/test/test_fibonacci.py::TestCase1::test_fibonacci PASSED fibonacci_sequence/test/test_fibonacci.py::TestCase2::test_fibonacci PASSED fizzbuzz/test/test_fizzbuzz.py::TestIfStatement::test_fizzbuzz PASSED fizzbuzz/test/test_fizzbuzz.py::TestTernaryStatement::test_fizzbuzz PASSED letter_inspection/test/test_letter_inspection_application.py::TestRegularCase::test_exactly_equal_size_and_included_1 PASSED letter_inspection/test/test_letter_inspection_application.py::TestCase1::test_exactly_equal_size_and_included_3 PASSED nabeatsu/test/test_nabeatsu.py::TestIfStatement::test_go_crazy PASSED nabeatsu/test/test_nabeatsu.py::TestTernaryStatement::test_go_crazy PASSED 12 passed in 4.24s5. Key Takeaways
__file__-based paths for test imports, not working directory-relative pathssys.pathmodifications in source files__all__definitions for wildcard imports