0

I want to test the following function with pytest:

def createDF(path):
    df = pd.DataFrame()
    try:
        data = pd.read_csv(path, sep='\t')  
        #print(data)
        df = pd.DataFrame(data)
        return df   
    except pd.errors.EmptyDataError:
        logging.exception("CSV File Empty")
        sys.exit(0)

it shall except when reading in a empty csv and then exit.

The test looks like this. I iterated over a couple of csv files with the fixture. One of the files is empty so the SystemExit is raised in the test.

@pytest.mark.parametrize("csvs",glob.glob("./data/*"))
def test_parser(csvs, capsys):
    """
    Checking if table is empty
    """
    df = pd.DataFrame()  
    df=createDF(csvs)
    assert df.empty is False
    print("\nRunning createDF:", df.empty)

One test is failing with SystemExit exception but it shall not fail as its desired behavior

This case is not working for me because the other files in the fixture not raising the exception

Okkervile
  • 19
  • 1
  • 2
    [This should solve your problem](https://stackoverflow.com/q/23337471/7867968). But I have to ask, why `sys.exit(0)`? Clearly you've encountered an erroneous condition, I'd just `raise` the exception, since the program will error out anyways. Then you can use `with pytest.raises(pd.errors.EmptyDataError)` – C.Nivs Jan 31 '23 at 20:24

0 Answers0