Whenever a file imports import greengrasssdk
the unit tests fail, because the module greengrass_common
doesn't exists on my local machine and I cannot install it via pip.
I am executing the tests with PyCharm. The Greengrass lambda, I try to test, doesn't execute locally because of the same dependency problem (same exception). But as soon as the lambda is pushed to greengrass it works fine.
Here is the exception:
import greengrasssdk
File "C:\Python27\lib\site-packages\greengrasssdk\__init__.py", line 6,
in <module>
from .Lambda import StreamingBody
File "C:\Python27\lib\site-packages\greengrasssdk\Lambda.py", line 10, in
<module>
from greengrass_common.function_arn_fields import FunctionArnFields
ImportError: No module named greengrass_common.function_arn_fields
A simplified code example is this:
import greengrasssdk
import logging
greengrass_iot_client = greengrasssdk.client('iot-data')
logger = logging.getLogger('logger')
def handler(event, context):
logger.info('Event handler invoked with event: ' + str(event))
I get the following error message on the test (The test is excluded in a test folder, but no other dependency issues have been shown yet - I write this because some developers put their tests into the python code file. I heard that that tests outside of the source code file could result in import issues. Though this case is different since it happens also in the original code file.)
import unittest
import mock
import function
class SimpleTest(unittest.TestCase):
# NONE OF THE THREE PATCH WORK Not in combination nor single
@mock.patch('greengrass_common')
@mock.patch('greengrass_common.function_arn_fields')
@mock.patch('greengrasssdk')
def test_that(self):
pass
The test case is empty for simplification.
I expect the greengrass_common code to be existing outside the Greengrass code for me to write unit tests.
I come from the java world, but spoke to a few python developers. We didn't really find a solution. (Except try catching the import in the production code) , but that seems like the first step into bad software quality in the whole project.
I am very grateful for ideas/solutions/approaches and guidance.
Thank you very much :).