Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write tests for main.py #242

Open
K20shores opened this issue Sep 19, 2024 · 0 comments
Open

Write tests for main.py #242

K20shores opened this issue Sep 19, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@K20shores
Copy link
Collaborator

K20shores commented Sep 19, 2024

Currently main.py, our command line program, is only tested using some options in github actions. We should test all of the options

Acceptance criteria

  • important command line options are tested. We can exclude some options like --plot

Ideas

  • ChatGPT thinks we can mock some things with pytest. See if something like this works
from acom_music_box import parse_arguments, main, setup_logging

# Test parse_arguments to ensure it handles different arguments correctly
def test_parse_arguments_example(monkeypatch):
    test_args = ['music_box', '--example', 'example_name']
    monkeypatch.setattr('sys.argv', test_args)

    args = parse_arguments()
    assert args.example == 'example_name'
    assert args.config is None  # Ensure --config is ignored when --example is provided

def test_parse_arguments_config(monkeypatch):
    test_args = ['music_box', '--config', 'path/to/config.json']
    monkeypatch.setattr('sys.argv', test_args)

    args = parse_arguments()
    assert args.config == 'path/to/config.json'
    assert args.example is None

# Mock sys.exit to prevent it from halting the test suite
def test_main_example(monkeypatch):
    test_args = ['music_box', '--example', 'example_name']
    monkeypatch.setattr('sys.argv', test_args)

    with pytest.raises(SystemExit):  # Catch the exit to prevent test from stopping
        with mock.patch('acom_music_box.MusicBox') as MockMusicBox:
            mock_box = MockMusicBox.return_value
            mock_box.readConditionsFromJson.return_value = None
            mock_box.create_solver.return_value = None
            mock_box.solve.return_value = "test_output"

            main()  # Call the main function

            mock_box.readConditionsFromJson.assert_called_once_with('example_name')  # Verify behavior

# Test logging setup
def test_setup_logging(mock):
    with mock.patch('logging.basicConfig') as mock_basic_config:
        setup_logging(verbosity=2, color_output=True)
        mock_basic_config.assert_called_once()  # Check if logging was properly configured

@K20shores K20shores added the enhancement New feature or request label Sep 19, 2024
@K20shores K20shores added this to the Sancy's MusicBox Paper milestone Sep 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant