Skip to content

unit tests: move unit test files into module #2

@lundybernard

Description

@lundybernard

Executing tests from the top level of the repository fails. It is common practice for developers to execute unit tests on the repository while making changes, and Unit Tests in particular should be executable without installing the module.

This change will streamline development work, and simplify automated testing (for CI/CD), and improve code portability (simplify refactoring).

Suggested change:
Move existing test files from the top level of the repo into tests folders at the same level as the .py files they test.
Use relative imports in the test files.

|── braidgenerator
| |---- tests
| | |---- brainword_test.py
| | |---- markovchain_test.py
│ ├── init.py
│ ├── braidword.py
│ └── markovchain.py
│ ├── decorators
| | |---- tests
| | | |---- brainword_test.py
| | | |---- markovchain_test.py
│ │ ├── init.py
│ │ ├── braidword.py
│ │ └── markovchain.py

Example Error:

> pytest
======================================= test session starts ========================================
platform linux -- Python 3.9.5, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: ~/projects/Knots
collected 0 items / 2 errors                                                                       

============================================== ERRORS ==============================================
_____________________________ ERROR collecting tests/test_braidword.py _____________________________
ImportError while importing test module '~/projects/Knots/tests/test_braidword.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
../../miniconda3/envs/knots/lib/python3.9/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
tests/test_braidword.py:2: in <module>
    from braidgenerator import BraidWord
E   ModuleNotFoundError: No module named 'braidgenerator'
____________________________ ERROR collecting tests/test_markovchain.py ____________________________
ImportError while importing test module '~/projects/Knots/tests/test_markovchain.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
../../miniconda3/envs/knots/lib/python3.9/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
tests/test_markovchain.py:2: in <module>
    from braidgenerator import MarkovChain
E   ModuleNotFoundError: No module named 'braidgenerator'
===================================== short test summary info ======================================
ERROR tests/test_braidword.py
ERROR tests/test_markovchain.py
!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 2 errors during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
======================================== 2 errors in 0.08s =========================================

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions