1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] python unittest own folder relative import ubuntu

Discussão em 'Python' iniciado por Stack, Setembro 11, 2024.

  1. Stack

    Stack Membro Participativo

    There are similar questions but none of them have a completly sibling test folder with main.py in the src folder and none of them work. Currently I use the following approach

    #project/tests/aaa/bbb/test_foo.py
    import unittest
    import sys
    sys.path.append("....")
    from src.aaa.bbb import foo as foo
    #...


    and end up with a

    ModuleNotFoundError: No module named 'src'


    (I tested profoundly if the amount of points in sys.path.append("....") is correct) Maybe I do not understand python but my folder structures (which I would like to keep) goes:

    /project
    /src
    main.py
    /aaa
    /bbb
    foo.py
    /tests
    /aaa
    /bbb
    test_foo.py


    I execute it in the project folder and for the normal not-test files that works fine.

    A former Try was (in test_foo.py):

    from ....src.aaa.bbb import foo as foo


    This is the approach that works in the normal files but for the test files I got:

    ImportError: attempted relative import with no known parent package
    ``
    I tried it with and without the ``` -m``` option.
    Also with and without


    if __name__ == '__main__':
    unittest.main()

    Continue reading...

Compartilhe esta Página