1

I need to test my methods using the Pytest library. I have written such tests.

def test_read_data_from_file(): # check for missing file.(1)
    try:
        read_data_from_file('example.csv')
    except FileNotFoundError:
        pytest.fail('File not found.')


def test_roots(): # check for lack of access to the file.(2)
    try:
        read_data_from_file('err.csv')
    except Permission Error:
        py test.fail('The file is not readable.')

The method itself

def read_data_from_file(filename):

# we will return it.
    rows_csv = []
    with open(filename) as csvfile:
        read_csv = csv.reader(csvfile)

# for convenience, when performing split, we will separate the data in lines with a space.
        for row in read_csv:
            rows_csv.append(' '.join(row))
    rows_csv.pop(0)
    return rows_csv

How can I change the tests so that "ExceptionInfo" is used in their implementation?(with pytest.raises(FileNotFoundError) as exception_info:)

  • 1
    You should use [tempfile](https://docs.python.org/3/library/tempfile.html) and mock `os.open` with [sideeffect](https://stackoverflow.com/questions/50964786/mock-exception-raised-in-function-using-pytest) – teedak8s Jun 05 '22 at 06:01

0 Answers0