Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add responses syntax to API tests #501

Open
stan-dot opened this issue Jun 12, 2024 · 1 comment
Open

Add responses syntax to API tests #501

stan-dot opened this issue Jun 12, 2024 · 1 comment
Labels
enhancement New feature or request tests Issues around increasing test coverage/fixing tests

Comments

@stan-dot
Copy link
Contributor

Replacing other test logic with responses? It seems neater

Originally posted by @DiamondJoseph in #409 (comment)

@stan-dot
Copy link
Contributor Author

stan-dot commented Sep 9, 2024


@responses.activate
@pytest.mark.handler
@patch("blueapi.service.handler.Handler")
@patch("blueapi.cli.cli.sleep", return_value=None)
def test_env_endpoint_interaction(
    mock_sleep: MagicMock, mock_handler: Mock, handler: Handler, runner: CliRunner
):
    # Setup mocked responses for the REST endpoints
    responses.add(
        responses.DELETE,
        "http://localhost:8000/environment",
        status=200,
        json=EnvironmentResponse(initialized=False).dict(),
    )
    responses.add(
        responses.GET,
        "http://localhost:8000/environment",
        json=EnvironmentResponse(initialized=False).dict(),
        status=200,
    )
    responses.add(
        responses.GET,
        "http://localhost:8000/environment",
        json=EnvironmentResponse(initialized=False).dict(),
        status=200,
    )
    responses.add(
        responses.GET,
        "http://localhost:8000/environment",
        status=200,
        json=EnvironmentResponse(initialized=True).dict(),
    )

    # Run the command that should interact with these endpoints
    result = runner.invoke(main, ["controller", "env", "-r"])

    # Check if the endpoints were hit as expected
    assert len(responses.calls) == 4  # Ensures that all expected calls were made



that is the syntax brought up there

@stan-dot stan-dot added enhancement New feature or request tests Issues around increasing test coverage/fixing tests labels Sep 9, 2024
@stan-dot stan-dot changed the title Replacing API response test logic with responses syntax Add responses syntax to API tests Sep 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request tests Issues around increasing test coverage/fixing tests
Projects
None yet
Development

No branches or pull requests

1 participant