from unittest import TestCase
from unittest.mock import patch
from testfixtures import compare
from tempfile import NamedTemporaryFile
from my_module import add_function
class TestMyModule(TestCase):
def test_add_function(self):
result = add_function(2, 5) # Act
compare(result, expected) # Assert
with NamedTemporaryFile() as tmp:
# automatically erased on cleanup
@patch("distro.id", return_value="rhel")
def test_mock(self, mock_id):
# now will be mocked out inside this test function
def test_assert_logs(self):
with self.assertLogs(level="ERROR") as log:
self.assertEqual(len(log.output), 1)
self.assertEqual(len(log.records), 1)
self.assertIn('Log message', log.output[0])