[Python] Python unittest failing on function importing local file

Discussão em 'Python' iniciado por Stack, Janeiro 22, 2025.

  1. Stack

    Stack Membro Participativo

    I have a Python package with a directory that looks like this:

    -root
    |
    |-src
    | |
    | -app
    | |
    | |-__init__.py
    | |-__main__.py
    | |-file1.py
    | |-file2.py
    |
    |-tests
    |
    |-__init__.py
    |-test_function.py


    file1.py looks like this

    from app import file2

    def function():
    return file2.another_function()


    test_function.py looks like this

    import unittest

    from src.app.file1 import function

    class TestFunction(unittest.TestCase):
    def test_case(self):
    self.assertTrue(function())


    However, whilst as a package this works fine, when running the test I get a ModuleNotFoundError: No module named 'app' on from app import file2.

    It works when I put src. in front of app in file1.py but then the package breaks. Looking around, there isn't really a clear way to fix this so unsure what my path should be looking like.

    Continue reading...

Compartilhe esta Página