Skip to content

Commit

Permalink
improved main tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Tjalling-dejong committed Nov 19, 2024
1 parent 9a06213 commit 814ca05
Showing 1 changed file with 8 additions and 60 deletions.
68 changes: 8 additions & 60 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import getopt
import os
import shutil
import re

import pytest

Expand Down Expand Up @@ -41,7 +42,7 @@ def teardown_class(Main):
if os.path.exists(outputtestdir):
os.rmdir(outputtestdir)

def test_when_incorrect_args_then_systemexit_risen_with_expected_message(self):
def test_when_incorrect_args_then_systemexit_raised_with_expected_message(self):
# 1. Set up test data
mainArgs = [""]

Expand All @@ -57,7 +58,7 @@ def test_when_incorrect_args_then_systemexit_risen_with_expected_message(self):
assert isinstance(pytest_wrapped_e.type, SystemExit)
assert pytest_wrapped_e.value.code == expectedMssg

def test_when_incorrect_input_args_systemexit_risen_with_expected_message(self):
def test_when_incorrect_input_args_systemexit_raised_with_expected_message(self):
# 1. Set up test data
mainArgs = ["-o", "test1"]
opts, args = getopt.getopt(mainArgs, "hi:o:", ["ifile=", "ofile="])
Expand All @@ -68,73 +69,20 @@ def test_when_incorrect_input_args_systemexit_risen_with_expected_message(self):
expectedMssg = "Error: {0}".format(reason)

# 3. Run test
with pytest.raises(SystemExit) as pytest_wrapped_e:
with pytest.raises(SystemExit, match=re.escape(expectedMssg)):
main.main(mainArgs)

# 4. Verify expectations
assert isinstance(pytest_wrapped_e.type, SystemExit)
assert pytest_wrapped_e.value.code == expectedMssg


def test_when_giving_correct_arguments_then_does_not_raise_systemexit(self):
# 1. Set up test data
test_dir = "output_test_main_unit"
outputtestdir = TestUtils.get_local_test_data_dir(test_dir)
mainArgs = ["-i", "test1", "-i", "test2", "--i", "test3", "--o", outputtestdir]
opts, args = getopt.getopt(mainArgs, "hi:o:", ["ifile=", "ofile="])

# 2. Set up expectations
reasons = [
"Not all arguments were given.",
"The first three arguments should be input files.\n"
+ " Given: {}\n{}\n{}\n".format(opts[0], opts[1], opts[2]),
"The last argument should be the output directory.",
]

# 3. Run test
with pytest.raises(SystemExit) as pytest_wrapped_e:
# 2. Run test
with pytest.raises(SystemExit, match="Not all arguments were given."):
main.main(mainArgs)

# 4. Verify expectations (it should actually not get here)
assert isinstance(pytest_wrapped_e.type, SystemExit)
for reason in reasons:
expectedMssg = "Error: {0}".format(reason)
assert pytest_wrapped_e.value.code != expectedMssg



def ARCHIVED_test_when_giving_non_existent_input_file_then_raises_io_exception(
self,
):
# 1. Set up test data
file_path = "test1"
mainArgs = ["-i", file_path]

# 2. Set up expectations
reason = "The given file path {} could not be found.".format(file_path)

# 3. Run test
with pytest.raises(IOError) as e_info:
main.main(mainArgs)

# 4. Verify final expectations
exception_message = str(e_info.value)
assert (
exception_message == reason
), "" + "Expected exception message {}, retrieved {}".format(
reason, exception_message
)

def ARCHIVED_test_when_giving_existent_empty_input_file_then_does_not_raise_io_exception(
self,
):
# 1. Set up test data
test_dir = TestUtils.get_local_test_data_dir("main_test_data")
file_name = "test_ini_file.ini"
file_path = os.path.join(test_dir, file_name)
mainArgs = ["-i", file_path]

# 2. Set up expectations
assert os.path.exists(file_path)

# 3. Run test
main.main(mainArgs)

0 comments on commit 814ca05

Please sign in to comment.