Skip to content

Problems with Writing Unit Tests that Link Back to openc3.script #3177

@stephen-ritter

Description

@stephen-ritter

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions