Remove the conftest defined fixtures as mock methods return mocks already removing the need for the defined mocks

This commit is contained in:
Julian Stirling 2025-08-24 19:22:19 +01:00
parent 867cd0cad1
commit e53f4e3b2b
4 changed files with 16 additions and 35 deletions

View file

@ -1,27 +0,0 @@
"""Provide fixtures that can be reused by the entire test suite.
For more information on this pattern see:
https://docs.pytest.org/en/stable/reference/fixtures.html#conftest-py-sharing-fixtures-across-multiple-files
"""
import pytest
@pytest.fixture
def mock_app(mocker):
"""Return a mock FastAPI app.
This is just a mock, where the get method returns a mock.
"""
wrapper = mocker.Mock()
app = mocker.Mock()
app.get.return_value = wrapper
return app
@pytest.fixture
def mock_server(mock_app, mocker):
"""Return a LabThings FastAPI server where .app is the mock_app fixture."""
server = mocker.Mock()
server.app = mock_app
return server

View file

@ -34,12 +34,13 @@ def mock_static_dir():
["fontfile.woff2", True],
],
)
def test_add_static_file(filename, allow_cache, mock_app):
def test_add_static_file(filename, allow_cache, mocker):
"""Test running add_static_file with both font and non-font files.
This should creates a wrapped function that returns a FileResponse, this
should be have the path on disk and the no cache headers if not a woff2 font.
"""
mock_app = mocker.Mock()
# Get the wrapper function from the mocked decorator
wrapper = mock_app.get.return_value
serve_static_files.add_static_file(mock_app, fname=filename, folder="bar")
@ -71,8 +72,9 @@ def test_add_static_file(filename, allow_cache, mock_app):
assert response.headers[key] == value
def test_add_static_with_no_static_dir(mock_app, mocker):
def test_add_static_with_no_static_dir(mocker):
"""Test that a FileNotFound error is raised if the static dir does not exist."""
mock_app = mocker.Mock()
# Mock STATIC_PATH to something that doesn't exist
mocker.patch(
"openflexure_microscope_server.server.serve_static_files.STATIC_PATH",
@ -116,8 +118,9 @@ def test_check_static_dir(mock_static_dir, mocker):
serve_static_files.check_static_dir()
def test_add_static_files(mock_app, mock_static_dir, mocker):
def test_add_static_files(mock_static_dir, mocker):
"""Check add_static_files mounts the internal webapp dirs, and sets endpoints for files."""
mock_app = mocker.Mock()
mocker.patch(
"openflexure_microscope_server.server.serve_static_files.STATIC_PATH",
mock_static_dir,
@ -158,8 +161,9 @@ def test_add_static_files(mock_app, mock_static_dir, mocker):
assert os.path.join(mock_static_dir, "js") in mounted_dirs
def test_add_static_files_with_scan_dir(mock_app, mock_static_dir, mocker):
def test_add_static_files_with_scan_dir(mock_static_dir, mocker):
"""Check the scan dir mounts if supplied."""
mock_app = mocker.Mock()
mocker.patch(
"openflexure_microscope_server.server.serve_static_files.STATIC_PATH",
mock_static_dir,

View file

@ -18,12 +18,13 @@ def test_no_config():
ofm_server.serve_from_cli([])
def test_successful_start(mock_server, mocker, caplog):
def test_successful_start(mocker, caplog):
"""Check some basic things from the microscope setup.
This test checks that logging is set up and that the actual function used for
handling shutdown events shuts down the mjpeg streams.
"""
mock_server = mocker.Mock()
# Create a mock for the camera so we can check the MJPEG streams are closed on
# shutdown.
mock_camera = mocker.Mock()
@ -69,11 +70,12 @@ def test_successful_start(mock_server, mocker, caplog):
assert str(caplog.records[0].msg) == "mock-54321"
def test_failed_customise(mock_server, mocker):
def test_failed_customise(mocker):
"""Check what happens if there is an error before the server is started by uvicorn.
This differs based on whether the --fallback flag is set.
"""
mock_server = mocker.Mock()
# Mock the LabThings function that returns the server so we have a mock server
mocker.patch(
"openflexure_microscope_server.server.lt.cli.server_from_config",

View file

@ -17,8 +17,9 @@ SIM_CONFIG = os.path.join(REPO_ROOT, "ofm_config_simulation.json")
@pytest.mark.parametrize("side_effect", [None, RuntimeError("Mock")])
def test_monkey_patched_handle_exit(side_effect, mock_app, mocker):
def test_monkey_patched_handle_exit(side_effect, mocker):
"""Test that handle exit is monkey_patched."""
mock_app = mocker.Mock()
# Setup the same for any side_effect
# Create mocks for FastAPI.Server, and for our custom shutdown function
mock_shutdown_func = mocker.Mock(side_effect=side_effect)
@ -49,8 +50,9 @@ def test_monkey_patched_handle_exit(side_effect, mock_app, mocker):
assert original_mock_handle_exit.call_count == 0
def test_customise_server(mock_server, mocker):
def test_customise_server(mocker):
"""Check that all server customisation stages are called."""
mock_server = mocker.Mock()
# Mock everything to be called in customisation, partly setup may not be complete,
# but also to limit excess coverage reporting.
mocked_add_static = mocker.patch.object(ofm_server, "add_static_files")