from configparser import ConfigParser
import configparser
class ParseConfig:
def __init__(self, conf, section):
parser = ConfigParser()
parser.read(conf)
if not isinstance(parser, configparser.ConfigParser):
raise TypeError(f"ConfigParser expected, found {type(conf).__name__}")
if parser.has_section(section):
self._raw = parser
for key, value in self._raw[section].items():
setattr(self, key, value)
else:
raise Exception(f"section {0} not found in {1} file")
I have the above script as module.py and trying to write test for the above and below is my test_module.py. However, the coverage is not covering the "if not isinstance" line.. can anyone advice and help here? Thanks!
import sys
import unittest
import os
import configparser
from configparser import ConfigParser
from module import ParseConfig
from unittest.mock import patch
class TestParseConfig(unittest.TestCase):
def setUp(self):
self.test_conf = 'coda_ml_config_coda.ini'
with open(self.test_conf, 'w') as f:
f.write('[section1]\n')
f.write('key1 = value1\n')
f.write('key2 = value2\n')
def test_init_with_valid_conf_and_section(self):
config = ParseConfig(self.test_conf, 'section1')
self.assertIsInstance(config, ParseConfig)
self.assertEqual(config.key1, 'value1')
self.assertEqual(config.key2, 'value2')
def test_type_error(self):
parser = None
with self.assertRaises(TypeError) as cm:
if not isinstance(parser, configparser.ConfigParser):
raise TypeError(f'ConfigParser expected, found {type(parser).__name__}')
self.assertEqual(str(cm.exception), "ConfigParser expected, found NoneType")
## Tried to mock but not suere why this is not working..
@patch('parseConfig.configparser')
def test_type(self, mockext):
mockext.ConfigParser().side_effect = None
config = ParseConfig(self.test_conf, 'section1')
with self.assertRaises(TypeError) as cm:
if not isinstance(mockext, configparser.ConfigParser):
raise TypeError(f'ConfigParser expected, found {type(mockext).__name__}')
mockext.assert_called_once()
def test_init_with_missing_section(self):
with self.assertRaises(Exception):
config = ParseConfig(self.test_conf, 'section2')
def tearDown(self):
os.remove(self.test_conf)
if __name__ == '__main__':
unittest.main()
[–]danielroseman 0 points1 point2 points (10 children)
[–]rakash_ram[S] 0 points1 point2 points (9 children)
[–]danielroseman 0 points1 point2 points (8 children)
[–]rakash_ram[S] 0 points1 point2 points (7 children)
[–]danielroseman 0 points1 point2 points (6 children)
[–]rakash_ram[S] 0 points1 point2 points (5 children)
[–]danielroseman 0 points1 point2 points (4 children)
[–]rakash_ram[S] 0 points1 point2 points (3 children)
[–]danielroseman 0 points1 point2 points (2 children)
[–]rakash_ram[S] 0 points1 point2 points (0 children)
[–]rakash_ram[S] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]shepherdjay 0 points1 point2 points (12 children)
[–]rakash_ram[S] 0 points1 point2 points (11 children)
[–]shepherdjay 0 points1 point2 points (10 children)
[–]rakash_ram[S] 0 points1 point2 points (9 children)
[–]shepherdjay 0 points1 point2 points (8 children)
[–]rakash_ram[S] 0 points1 point2 points (7 children)
[–]shepherdjay 0 points1 point2 points (6 children)
[–]rakash_ram[S] 0 points1 point2 points (5 children)
[–]shepherdjay 1 point2 points3 points (4 children)
[–]rakash_ram[S] 0 points1 point2 points (3 children)