Skip to content
Snippets Groups Projects
Commit 89da273b authored by Kathleen Moriarty's avatar Kathleen Moriarty
Browse files

add test for cli - but needs to be improved

parent 31915d91
No related branches found
No related tags found
1 merge request!17Issue 7
Pipeline #13907 failed
"""Test Functionality of Read Sequencing CLI.
This test needs to be modified correctly.
"""
import argparse
import importlib.util
import os
from pathlib import Path
import sys
import pytest
from src.read_sequencing.cli import (
main,
parse_args,
)
PACKAGE_DIR = Path(__file__).resolve().parents[1] / "src/read_sequencing"
TEST_FILE = Path.cwd() / 'tests/resources/test_terminal_fragments.txt',
class TestParseArgs:
"""Test ``parse_args()`` function."""
def test_help_option(self, monkeypatch):
"""Test help option."""
monkeypatch.setattr(
sys, 'argv', [
'htsinfer',
'--help',
]
)
with pytest.raises(SystemExit) as exc:
assert parse_args()
assert exc.value.code == 0
def test_one_positional_arg(self, monkeypatch):
"""Call with one positional arg."""
monkeypatch.setattr(
sys, 'argv', [
str(TEST_FILE),
]
)
ret_val = parse_args()
assert isinstance(ret_val, argparse.Namespace)
def test_too_many_positional_args(self, monkeypatch):
"""Call with two positional args."""
monkeypatch.setattr(
sys, 'argv', [
'htsinfer',
str(TEST_FILE), str(TEST_FILE),
]
)
with pytest.raises(SystemExit) as exc:
parse_args()
assert exc.value.code != 0
class TestMain:
"""Test ``main()`` function."""
def test_with_too_many_args(self, monkeypatch):
"""Call with too many positional args."""
monkeypatch.setattr(
sys, 'argv', [
'read_sequencing',
str(TEST_FILE), str(TEST_FILE),
]
)
with pytest.raises(SystemExit) as exc:
main()
assert exc.value.code != 0
def test_keyboard_interrupt(self, monkeypatch):
"""Test keyboard interrupt."""
class RaiseKeyboardInterrupt:
"""Helper class to raise `ValueError`."""
def __init__(self, *args, **kwargs):
"""Class constructor."""
raise KeyboardInterrupt
monkeypatch.setattr(
'src.read_sequencing.cli.parse_args',
RaiseKeyboardInterrupt,
)
with pytest.raises(SystemExit) as exc:
main()
assert exc.value.code >= 128
def test_main_as_script():
"""Run as script."""
main_file = PACKAGE_DIR / "cli.py"
fl = os.path.join(os.path.dirname(__file__), main_file)
spec = importlib.util.spec_from_file_location('__main__', fl)
module = importlib.util.module_from_spec(spec)
with pytest.raises(SystemExit) as exc:
spec.loader.exec_module(module) # type: ignore
assert exc.value.code != 0
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment