I have a method that raises an error if any cells in a csv row are blank. However the test does not assert that the error has raised, even though the method does raise it and logs it. I think it has something to do with it being in a try/except but I am not sure. I looked at this but couldn't get it to work.
pytest output
====================================================================================================== FAILURES ======================================================================================================
___________________________________________________________________________________ TestCSVFileMethods.test_verify_cell_not_blank ____________________________________________________________________________________
self = <tests.test_csv_methods.TestCSVFileMethods testMethod=test_blank_cell>
def test_blank_cell(self):
# Add another entry to the name column with a blank value
with open(self.temp_file, "a", newline="") as f:
writer = csv.DictWriter(f, fieldnames=["id","name"])
writer.writerow({"id": "2", "name": ""})
> with pytest.raises(BlankCellError):
E Failed: DID NOT RAISE <class 'src.csv_methods.BlankCellError'>
tests/test_csv_methods.py:161: Failed
------------------------------------------------------------------------------------------------ Captured stdout call ------------------------------------------------------------------------------------------------
BlankCell: Missing cell value for ID: '2' - 'Column: name'
------------------------------------------------------------------------------------------------- Captured log call --------------------------------------------------------------------------------------------------
WARNING root:csv_methods.py:294 Missing cell value for ID: '2' - 'Column: name'
Missing cell value for ID: '2' - 'Column: name'
============================================================================================== short test summary info ===============================================================================================
FAILED tests/test_csv_methods.py::TestCSVFileMethods::test_blank_cell - Failed: DID NOT RAISE <class 'src.csv_methods.BlankCellError'>
============================================================================================ 1 failed, 6 passed in 0.10s =============================================================================================
Error and Method
class BlankCellError(Exception):
pass
csv_file = "csv_data/assets.csv"
def blank_cell(csv_file):
try:
with open(csv_file, newline="") as file:
reader = csv.DictReader(file)
for row_idx, row_v in enumerate(reader, start=1): # Start at 1 to skip the csv column headers
for k, v in row_v.items():
if v.strip() == "":
raise BlankCellError(f"Missing cell value for ID: '{row_v['id']}' - 'Column: {k}'")
else:
continue
except BlankCellError as e:
print(f"BlankCell: {e}")
Test
import csv
import unittest
import pytest
def create_test_file(file_name, content):
with open(file_name, "a") as f:
writer = csv.DictWriter(f, fieldnames=["id","name"])
writer.writeheader()
writer.writerows(content)
class TestCSVFileMethods(unittest.TestCase):
def setUp(self):
self.data = [
{
"id": "1",
"name": "name1"
},
]
self.temp_file = os.path.join("tests", "tmp_csv.csv")
create_test_file(self.temp_file, self.data)
def blank_cell(self):
with open(self.temp_file, "a", newline="") as f:
writer = csv.DictWriter(f, fieldnames=["id","name"])
writer.writerow({"id": "2", "name": ""})
with pytest.raises(BlankCellError):
blank_cell(self.temp_file)
UPDATE
Based on the feedback I changed the test to this but I still get an error:
def test_blank_cell(self):
# Add another entry to the name column with a blank value
with open(self.temp_file, "a", newline="") as f:
writer = csv.DictWriter(f, fieldnames=fieldnames)
writer.writerow({"id": "2", "name": ""})
with pytest.raises(BlankCellError) as excinfo:
blank_cell(self.temp_file)
assert str(excinfo.value) == "Missing cell value for ID"
Error after update
The only change in the test error output:
E Failed: DID NOT RAISE <class 'src.csv_methods.BlankCellError'>