AttributeError: 'ExceptionInfo' object has no attribute 'traceback' when using pytest to assert exceptions AttributeError: 'ExceptionInfo' object has no attribute 'traceback' when using pytest to assert exceptions python-3.x python-3.x

AttributeError: 'ExceptionInfo' object has no attribute 'traceback' when using pytest to assert exceptions


You can't use the ExceptionInfo inside the with pytest.raises context. Run the code that is expected to raise in the context, work with the exception info outside:

with pytest.raises(InvAmtValError) as e:    invoices = InvoiceStats()    invoices.addInvoice(-1.2)assert str(e) == 'The invoice amount(s) -1.2 is invalid since it is < $0.00'assert e.type == InvAmtValError  # etc

However, if you just want to assert the exception message, the idiomatic way would be passing the expected message directly to pytest.raises:

expected = 'The invoice amount(s) -1.2 is invalid since it is < $0.00'with pytest.raises(InvAmtValError, message=expected):    invoices = InvoiceStats()    invoices.addInvoice(-1.2)expected = 'The invoice amount(s) 100000000.1 is invalid since it is > $100,000,00.00'with pytest.raises(InvAmtValError, message=expected):    invoices = InvoiceStats()    invoices.addInvoice(100000000.1)

UPDATE. Tried the solution suggested, got:

>           invoices.addInvoice(-1.2)E           Failed: DID NOT RAISE

That's because the exception is indeed not raised in the addInvoice method - it is raised inside the try block and immediately catched afterwards. Either remove the try block altogether, or reraise the exception:

try:    raise InvAmtValError(amount)except InvAmtValError as e:    print(str(e))    raise e