I would like to write unittest for Extractor run main function, Please help me to find mistakes in my code.
Hi Team,
Please help me to find the mistakes in my code.
Let me showcase the dummy code
main code:
def main() -> None:
"""
Main entrypoint
"""
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
config_values_vault.set_vara()
with Extractor(
name="SAP_Extractor",
description="An extractor to extract asset hierarchy from SAP based on root node",
config_class=SapConfig,
# version=__version__,
# debugger
version="1.0.0",
run_handle=run,
metrics=metrics,
config_file_path= os.path.join(BASE_DIR, 'config.yaml'),
) as extractor:
extractor.run()
I have build code unittest for above code:
def test_main():
with patch('os.path') as path_mock:
with patch.object(path_mock, 'abspath') as mock_abspath:
with patch.object(path_mock, 'dirname') as mock_dirname:
with patch.object(path_mock,'join') as mock_join:
mock_abspath.return_value = '/path/to/yaml_file.yaml'
mock_dirname.side_effect = lambda x: x
with patch('sap_extractor.config_values_vault.set_vara'):
with patch('cognite.extractorutils.Extractor.run') as extractor_mock:
with patch('sap_extractor.config.SapConfig'):
with patch('sap_extractor.metrics'):
with patch('builtins.open',create=True) as mock_open:
mock_file = Mock()
mock_file.read.return_value = 'abc'
mock_open.return_value.__enter__.return_value = mock_file
with patch('sap_extractor.extractor.run') as run_mock:
run_mock.return_value = Mock()
extractor.main()
When I try to execute it got stuck not going forward and neither it’s fail or pass. I feel something I miss or did mistake in mocking the objects. Please help me to resolve it.
Thanks in Advance
Harshita
Check the
documentation
Ask the
Community
Take a look
at
Academy
Cognite
Status
Page
Contact
Cognite Support
