对预期会出现索引错误的函数执行测试

Performing test on a function which an Index error is expected from

我有一个 class,我希望它有一个索引错误,我想测试它。

我试过以下方法,但似乎不起作用:

input_data = []
my_class = StudentGrades()
result = my_class.calculate_score(input_data)
assert(IndexError, result)

如有任何帮助,我将不胜感激。

我假设你正在使用 pytest 因为你标记了它。

使用pytest.raises:

with pytest.raises(IndexError):
    my_class.calculate_score(input_data)