dimanche 22 mars 2020

python: How can I write a test for this function?

I have my function and testing part.

def func(file_name, fields_number=None, sep='-'):
    result = []
    with open(file_name) as file_ptr:
        for line in file_ptr:
            if line.strip():
                fields = tuple(line.strip().split(sep))
                if fields_number and len(fields) != fields_number:
                    msg(f'{fields}')
                    die(f'Illegal number of fields:{len(fields)} found in {file_name}')
                else:
                    result.append(fields)

    return result

This is my function.

@pytest.mark.parametrize('file_name, num_fields=None, sep='-', expected_result',
                [('file_prog', 5, '-', True),
                 ('file_prog', None ,'-', False)])

def test_func(input_str, expected_result):
    result = func(file_name, num_fields=None, sep='-')
    assert result == expected_result

testing part, But I get this kind of error, how can I fix it, do I need to use type casting?

TypeError: unsupported operand type(s) for -: 'str' and 'str'

For example I have in my file aa-bb-cc. It means that I have 3 fields, and if I want to get true I need to pass fields_number=3

Aucun commentaire:

Enregistrer un commentaire