I was trying to implement unit tests for a YAML configuration class.

These tests work properly, however I intended to use as a testing framework, while for file I came up with a solution using unittest library.

Is it something to be avoided? Is it sub-optimal in terms of resource usage or maintainability?

Below is a sample test:

import pytest
from unittest.mock import patch, mock_open
from configurationfile import ConfigurationFile

def mock_configuration():
    initial_mock_data = """---
                           s3_bucket_name: s3_bucket_name_value
                           s3_object_name: s3_object_name_value"""
    with patch('builtins.open', mock_open(read_data=initial_mock_data)):
        return ConfigurationFile()

def test_read_yaml_file(mock_configuration):
    assert mock_configuration.configuration['s3_bucket_name'] == 's3_bucket_name_value'
    assert mock_configuration.configuration['s3_object_name'] == 's3_object_name_value'

Source link https://sqa.stackexchange.com/questions/35375/mocking-file--functions-in-pytest


Please enter your comment!
Please enter your name here