I am hitting a problem when writing unit tests for a plugin. The issue surfaces itself with any code that imports openc3.script. Here is a made up unit test that I am using as an example:
import unittest
import openc3.script
class MyTest(unittest.TestCase):
def test_check(self) -> None:
self.assertEqual(1, 1)
I get the following error:
E requests.exceptions.ConnectionError: HTTPConnectionPool(host='openc3-cosmos-cmd-tlm-api', port=2901): Max retries exceeded with url: /openc3-api/auth/verify (Caused by NameResolutionError("HTTPConnection(host='openc3-cosmos-cmd-tlm-api', port=2901): Failed to resolve 'openc3-cosmos-cmd-tlm-api' ([Errno -2] Name or service not known)"))
Looking into the traceback I end up in the __init__.py file for openc3.script where it attempts to make a connection to the API server.
|
API_SERVER = ApiServerProxy() |
|
SCRIPT_RUNNER_API_SERVER = ScriptServerProxy() |
I have come up with methods to mock the entirety of anything imported through openc3.script amongst other things, but this has a lot of consequences, like breaking other unit tests that were passing before, or I now have to recreate the mocked object if I was using an object out of openc3.script which at this point I am not even unit testing the code. Is there a cleaner way of mocking openc3.script? My gut reaction is that making connection calls through __init__.py complicates a lot of things as the code is executed where ever openc3.script is imported. It may be nested in another COSMOS import so everything gets tangled very quickly.
I am hitting a problem when writing unit tests for a plugin. The issue surfaces itself with any code that imports
openc3.script. Here is a made up unit test that I am using as an example:I get the following error:
Looking into the traceback I end up in the
__init__.pyfile foropenc3.scriptwhere it attempts to make a connection to the API server.cosmos/openc3/python/openc3/script/__init__.py
Lines 20 to 21 in 88c0d63
I have come up with methods to mock the entirety of anything imported through
openc3.scriptamongst other things, but this has a lot of consequences, like breaking other unit tests that were passing before, or I now have to recreate the mocked object if I was using an object out ofopenc3.scriptwhich at this point I am not even unit testing the code. Is there a cleaner way of mockingopenc3.script? My gut reaction is that making connection calls through__init__.pycomplicates a lot of things as the code is executed where everopenc3.scriptis imported. It may be nested in another COSMOS import so everything gets tangled very quickly.