[Python] How to fix discovery of module-under-test in test code with unittest?

Discussão em 'Python' iniciado por Stack, Março 29, 2025.

  1. Stack

    Stack Membro Participativo

    I've been struggling with this error and I have no idea of why it happens.


    ModuleNotFoundError: No module named 'lambdaFunction'
    ​

    This is the folder structure.

    [​IMG]

    And for some reason when I try to run the lambda_unit_test.py it keeps failing.

    This is the code in there:

    import unittest
    from lambdaFunction import index


    class LambdaTest(unittest.TestCase):
    def lambda_returns_Rick_Sanchez(self):
    self.assertEquals(index.lambda_handlerx(), "Should return Rick Sanchez")


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


    I've also tried things like import lambdaFunction without any luck.

    These are the command line outputs:

    python3 -m unittest test.lambda_unit_test.py
    E
    ======================================================================
    ERROR: lambda_unit_test (unittest.loader._FailedTest)
    ----------------------------------------------------------------------
    ImportError: Failed to import test module: lambda_unit_test
    Traceback (most recent call last):
    File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/unittest/loader.py", line 154, in loadTestsFromName
    module = __import__(module_name)
    ModuleNotFoundError: No module named 'test.lambda_unit_test'


    ----------------------------------------------------------------------
    Ran 1 test in 0.000s

    FAILED (errors=1)


    If I try running python3 -m unittest test.lambda_unit_test from the root folder it does not run any test:

    python3 -m unittest test.lambda_unit_test

    ----------------------------------------------------------------------
    Ran 0 tests in 0.000s

    OK


    Any ideas on how to fix this?

    Continue reading...

Compartilhe esta Página