Python: ¿Unittest puede mostrar valores esperados y reales?

If I have an assert in a unittest.TestCase as shown below:

self.assertTrue( person.age == 42, "age incorrect")

When it fails, it gives the "age incorrect" message. What I would also like to see is the expected and actual values. What's the best way to go about doing this? Is it something unittest can do?

EDITAR I would like to see something like:

"age incorrect: expected value 42 actual value 39"

preguntado el 08 de enero de 11 a las 15:01

self.assert is a syntax error. You mean self.assert_. -

Thanks. Yes, I've meant to write assertTrue...updated. -

3 Respuestas

You should use a workaround to this problem, like this:

self.assertEqual(person.age, 42, 'age incorrect: expected value {0} actual value {1}'.format(42, person.age))

But i think not providing the "msg" parameter is the best option, since it generates the text:

first != equal

Most(*) tools for running tests also shows directly which line failed, thus you should be able to understand which test failed and why without using an extra message.

(*) read "all".

Respondido el 08 de enero de 11 a las 19:01

Thanks. I hadn't realised that not adding the message gave actual and expected. - addicted-to-coding

Esto viola el SECO principle. - Rockallita

@Bakuriu Showing directly which line failed doesn't mean you are able to know the expected and actual values, especially when the test falla. And showing the msg parameter along with asserting values isn't that hard. You don't need to write the values twice. Just set the longMessage propiedad de la TestCase instancia a True. - Rockallita

@Bakuriu The question is asking how to show the error message y the asserting values at the same time. (See the last sentence of the question.) Also I think showing the error message is an intuitive way to let user understand what is going on. It's just as important as the asserting values. Besides, there's a very easy way to implement this, as I mentioned. - Rockallita

A valid answer does not make a good answer, and it is certainly at @Rockallite's discretion how poor it needs to be to downvote. I wouldn't downvote it personally, but it does make me frown. - DylanYoung

puedes configurar el longMessage atribuir a True

expected_age = 42
actual_age = person.age # 39
self.longMessage = True
self.assertEqual(expected_age, actual_age, 'age incorrect')

you would get something like:

AssertionError: 42 != 39 : age incorrect

referencia: https://docs.python.org/2/library/unittest.html#unittest.TestCase.longMessage

Respondido el 08 de Septiembre de 15 a las 22:09

eso es exactamente what I felt should exist. Excellent answer! - Karl P

ver: assertEqual

self.assertEqual(person.age, 42, 'age incorrect')

or with the default message (to answer the comment):

self.assertEqual(person.age, 42)

Respondido el 13 de enero de 11 a las 05:01

This didn't work for me. It still printed out "age incorrect", but not the actual value i.e., the value of person.age. - addicted-to-coding

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.