From 1c854ed533f312e23a100ee68335b952035fd016 Mon Sep 17 00:00:00 2001 From: Fernando Bravo <39527354+fernando79513@users.noreply.github.com> Date: Thu, 16 Nov 2023 16:36:28 +0100 Subject: [PATCH] Mocked print and log functions for tests (Infra) (#827) Mocked print and log functions for tests Replaced print and log functions for mocks for the tests in the base provider --- providers/base/tests/test_camera_quality.py | 4 +++- providers/base/tests/test_camera_test.py | 7 ++----- providers/base/tests/test_dkms_info.py | 3 +++ providers/base/tests/test_recovery_info.py | 3 ++- 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/providers/base/tests/test_camera_quality.py b/providers/base/tests/test_camera_quality.py index 300b59b53..74c586dae 100644 --- a/providers/base/tests/test_camera_quality.py +++ b/providers/base/tests/test_camera_quality.py @@ -23,7 +23,7 @@ from pathlib import Path import time import unittest -from unittest.mock import patch +from unittest.mock import patch, MagicMock import cv2 @@ -38,6 +38,7 @@ @patch("bin.camera_quality_test.TIMEOUT", new=0.05) @patch("bin.camera_quality_test.MIN_INTERVAL", new=0.01) +@patch("builtins.print", new=MagicMock()) class CameraQualityTests(unittest.TestCase): """This class provides test cases for the camera_quality_test module.""" @@ -78,6 +79,7 @@ def test_get_score_from_device(self, mock_score): self.assertEqual(result, 0) mock_score.assert_called_with("video0", True) + @patch("logging.Logger.error", new=MagicMock()) def test_quality_evaluation(self): """ The test should pass if the image is good and fails if it has bad diff --git a/providers/base/tests/test_camera_test.py b/providers/base/tests/test_camera_test.py index de0938ef7..957850966 100644 --- a/providers/base/tests/test_camera_test.py +++ b/providers/base/tests/test_camera_test.py @@ -20,18 +20,15 @@ import unittest import io import sys -from unittest.mock import patch +from unittest.mock import patch, MagicMock from camera_test import CameraTest, v4l2_capability +@patch("builtins.print", new=MagicMock()) class CameraTestTests(unittest.TestCase): """This class provides test cases for the CameraTest class.""" def setUp(self): - # supress stdout to hide print message - suppress_text = io.StringIO() - sys.stdout = suppress_text - self.camera_instance = CameraTest(None) @patch('camera_test.CameraTest._supported_resolutions_to_string') diff --git a/providers/base/tests/test_dkms_info.py b/providers/base/tests/test_dkms_info.py index 30f3ae48e..0b658d245 100644 --- a/providers/base/tests/test_dkms_info.py +++ b/providers/base/tests/test_dkms_info.py @@ -21,10 +21,12 @@ import io import unittest from unittest import mock +from unittest.mock import patch, MagicMock import dkms_info +@patch("logging.Logger.info", new=MagicMock()) class SystemInfoTests(unittest.TestCase): """Tests for System Information Parsing and Collection.""" @@ -118,6 +120,7 @@ def test_match_patterns(self, mock_get_system_modaliases): matched_modalieses) +@patch("logging.Logger.info", new=MagicMock()) class DebianPackageHandlerTest(unittest.TestCase): """Test of DebianPackageHandler.""" diff --git a/providers/base/tests/test_recovery_info.py b/providers/base/tests/test_recovery_info.py index 19658712f..409820954 100644 --- a/providers/base/tests/test_recovery_info.py +++ b/providers/base/tests/test_recovery_info.py @@ -19,7 +19,7 @@ import os import unittest from unittest import mock -from unittest.mock import patch +from unittest.mock import patch, MagicMock import sys import recovery_info @@ -96,6 +96,7 @@ def test_with_of_MountedPartition(self, mock_subprocess_check_output): universal_newlines=True)]) +@patch("builtins.print", new=MagicMock()) class RecoveryInfoTests(unittest.TestCase): """Tests for RecoveryInfo."""