improve test messages

This commit is contained in:
Sina Atalay 2023-10-12 20:09:01 +02:00
parent 57bdce86a0
commit bb27b7a1e5
3 changed files with 83 additions and 45 deletions

View File

@ -40,12 +40,11 @@ jobs:
pip install pytest pytest-cov
pytest --cov-report html --cov="." tests/
continue-on-error: true
- name: Upload artifacts
if: ${{matrix.os == 'ubuntu-latest' && matrix.python-version == '3.10'}}
uses: actions/upload-artifact@v3
with:
name: reference_files
path: ./tests/reference_files
# - name: Upload referebce files as artifacts
# uses: actions/upload-artifact@v3
# with:
# name: reference_files
# path: ./tests/reference_files
- name: Upload coverage data to smokeshow
if: ${{matrix.os == 'ubuntu-latest' && matrix.python-version == '3.12'}} # upload coverage once
run: |

View File

@ -102,9 +102,13 @@ class TestDataModel(unittest.TestCase):
}
with self.subTest(msg="valid date with start_date"):
event = data_model.Event(**input)
self.assertEqual(event.start_date, Date.fromisoformat(input["start_date"]))
self.assertEqual(event.end_date, "present")
self.assertEqual(event.date, None)
self.assertEqual(
event.start_date,
Date.fromisoformat(input["start_date"]),
msg="Start date is not correct.",
)
self.assertEqual(event.end_date, "present", msg="End date is not correct.")
self.assertEqual(event.date, None, msg="Date is not correct.")
input = {
"start_date": "2020-01-01",
@ -113,9 +117,13 @@ class TestDataModel(unittest.TestCase):
}
with self.subTest(msg="valid date with start_date and end_date=present"):
event = data_model.Event(**input)
self.assertEqual(event.start_date, Date.fromisoformat(input["start_date"]))
self.assertEqual(event.end_date, "present")
self.assertEqual(event.date, None)
self.assertEqual(
event.start_date,
Date.fromisoformat(input["start_date"]),
msg="Start date is not correct.",
)
self.assertEqual(event.end_date, "present", msg="End date is not correct.")
self.assertEqual(event.date, None, msg="Date is not correct.")
input = {
"start_date": None,
@ -124,9 +132,9 @@ class TestDataModel(unittest.TestCase):
}
with self.subTest(msg="valid date with custom date"):
event = data_model.Event(**input)
self.assertEqual(event.start_date, None)
self.assertEqual(event.end_date, None)
self.assertEqual(event.date, input["date"])
self.assertEqual(event.start_date, None, msg="Start date is not correct.")
self.assertEqual(event.end_date, None, msg="End date is not correct.")
self.assertEqual(event.date, input["date"], msg="Date is not correct.")
input = {
"start_date": None,
@ -135,9 +143,13 @@ class TestDataModel(unittest.TestCase):
}
with self.subTest(msg="valid date with ISO date"):
event = data_model.Event(**input)
self.assertEqual(event.start_date, None)
self.assertEqual(event.end_date, None)
self.assertEqual(event.date, Date.fromisoformat(input["date"]))
self.assertEqual(event.start_date, None, msg="Start date is not correct.")
self.assertEqual(event.end_date, None, msg="End date is not correct.")
self.assertEqual(
event.date,
Date.fromisoformat(input["date"]),
msg="Date is not correct.",
)
input = {
"start_date": "2020-01-01",
@ -146,9 +158,15 @@ class TestDataModel(unittest.TestCase):
}
event = data_model.Event(**input)
with self.subTest(msg="start_date, end_date, and date are all provided"):
self.assertEqual(event.date, None)
self.assertEqual(event.start_date, Date.fromisoformat(input["start_date"]))
self.assertEqual(event.end_date, input["end_date"])
self.assertEqual(event.date, None, msg="Date is not correct.")
self.assertEqual(
event.start_date,
Date.fromisoformat(input["start_date"]),
msg="Start date is not correct.",
)
self.assertEqual(
event.end_date, input["end_date"], msg="End date is not correct."
)
input = {
"start_date": "2020-01-01",
@ -157,9 +175,9 @@ class TestDataModel(unittest.TestCase):
}
event = data_model.Event(**input)
with self.subTest(msg="start_date and date are provided"):
self.assertEqual(event.start_date, None)
self.assertEqual(event.end_date, None)
self.assertEqual(event.date, input["date"])
self.assertEqual(event.start_date, None, msg="Start date is not correct.")
self.assertEqual(event.end_date, None, msg="End date is not correct.")
self.assertEqual(event.date, input["date"], msg="Date is not correct.")
input = {
"start_date": None,
@ -168,9 +186,9 @@ class TestDataModel(unittest.TestCase):
}
event = data_model.Event(**input)
with self.subTest(msg="end_date and date are provided"):
self.assertEqual(event.start_date, None)
self.assertEqual(event.end_date, None)
self.assertEqual(event.date, input["date"])
self.assertEqual(event.start_date, None, msg="Start date is not correct.")
self.assertEqual(event.end_date, None, msg="End date is not correct.")
self.assertEqual(event.date, input["date"], msg="Date is not correct.")
input = {
"start_date": None,
@ -179,16 +197,20 @@ class TestDataModel(unittest.TestCase):
}
event = data_model.Event(**input)
with self.subTest(msg="only date is provided"):
self.assertEqual(event.start_date, None)
self.assertEqual(event.end_date, None)
self.assertEqual(event.date, Date.fromisoformat(input["date"]))
self.assertEqual(event.start_date, None, msg="Start date is not correct.")
self.assertEqual(event.end_date, None, msg="End date is not correct.")
self.assertEqual(
event.date,
Date.fromisoformat(input["date"]),
msg="Date is not correct.",
)
# Inputs without dates:
with self.subTest(msg="no dates"):
event = data_model.Event(**{})
self.assertEqual(event.start_date, None)
self.assertEqual(event.end_date, None)
self.assertEqual(event.date, None)
self.assertEqual(event.start_date, None, msg="Start date is not correct.")
self.assertEqual(event.end_date, None, msg="End date is not correct.")
self.assertEqual(event.date, None, msg="Date is not correct.")
# Inputs with invalid dates:
input = {
@ -535,7 +557,7 @@ class TestDataModel(unittest.TestCase):
expected = "https://doi.org/10.1103/PhysRevB.76.054309"
publication = data_model.PublicationEntry(**input)
result = publication.doi_url
self.assertEqual(result, expected)
self.assertEqual(result, expected, msg="DOI URL is not correct.")
def test_data_connection_url(self):
# Github link:

View File

@ -170,13 +170,13 @@ class TestDataModel(unittest.TestCase):
"102.4 ex",
]
for length, exp in zip(lengths, expected):
with self.subTest(length=length):
with self.subTest(length=length, msg="valid input"):
self.assertEqual(rendering.divide_length_by(length, divider), exp)
def test_get_today(self):
expected = date.today().strftime("%B %d, %Y")
result = rendering.get_today()
self.assertEqual(expected, result)
self.assertEqual(expected, result, msg="Today's date is not correct.")
def test_get_path_to_font_directory(self):
font_name = "test"
@ -188,7 +188,7 @@ class TestDataModel(unittest.TestCase):
font_name,
)
result = rendering.get_path_to_font_directory(font_name)
self.assertEqual(expected, result)
self.assertEqual(expected, result, msg="Font directory path is not correct.")
def test_render_template(self):
test_input = {
@ -366,7 +366,9 @@ class TestDataModel(unittest.TestCase):
# Check if the output file exists:
output_folder_path = os.path.join(os.path.dirname(__file__), "output")
output_file_path = os.path.join(output_folder_path, "John_Doe_CV.tex")
self.assertTrue(os.path.exists(output_file_path))
self.assertTrue(
os.path.exists(output_file_path), msg="LaTeX file couldn't be generated."
)
# Compare the output file with the reference file:
reference_file_path = os.path.join(
@ -378,11 +380,15 @@ class TestDataModel(unittest.TestCase):
reference = file.read()
reference = reference.replace("REPLACETHISWITHTODAY", rendering.get_today())
self.assertEqual(output, reference)
self.assertEqual(
output, reference, msg="LaTeX file didn't match the reference."
)
# Check if the font directory exists:
font_directory_path = os.path.join(output_folder_path, "fonts")
self.assertTrue(os.path.exists(font_directory_path))
self.assertTrue(
os.path.exists(font_directory_path), msg="Font directory doesn't exist."
)
required_files = [
"EBGaramond-Italic.ttf",
@ -393,7 +399,11 @@ class TestDataModel(unittest.TestCase):
font_files = os.listdir(font_directory_path)
for required_file in required_files:
with self.subTest(required_file=required_file):
self.assertIn(required_file, font_files)
self.assertIn(
required_file,
font_files,
msg=f"Font file ({required_file}) is missing.",
)
# Remove the output directory:
shutil.rmtree(output_folder_path)
@ -407,20 +417,27 @@ class TestDataModel(unittest.TestCase):
pdf_file = rendering.run_latex(latex_file_path)
# Check if the output file exists:
self.assertTrue(os.path.exists(pdf_file))
self.assertTrue(
os.path.exists(pdf_file), msg="PDF file couldn't be generated."
)
# Compare the pdf file with the reference pdf file:
reference_pdf_file = pdf_file.replace(".pdf", "_reference.pdf")
self.assertTrue(
os.path.getsize(pdf_file) == os.path.getsize(reference_pdf_file)
reference_pdf_file_size = os.path.getsize(reference_pdf_file)
pdf_file_size = os.path.getsize(pdf_file)
ratio = min(reference_pdf_file_size, pdf_file_size) / max(
reference_pdf_file_size, pdf_file_size
)
self.assertTrue(ratio > 0.99, msg="PDF file didn't match the reference.")
nonexistent_latex_file_path = os.path.join(
os.path.dirname(__file__), "reference_files", "nonexistent.tex"
)
with self.subTest(msg="Nonexistent file name"):
with self.assertRaises(FileNotFoundError):
with self.assertRaises(
FileNotFoundError, msg="File not found error didn't raise."
):
rendering.run_latex(nonexistent_latex_file_path)