Hacker News new | ask | show | jobs
by pc86 1582 days ago
I'm no Python expert, but why does assertTrue() even accept two arguments?
4 comments

Explained later down in the post:

> assertTrue also accepts a second argument, which is the custom error message to show if the first argument is not truthy. This call signature allows the mistake to be made and the test to pass and therefore possibly fail silently.

The 2nd argument is the 'msg' argument.

With modern features in python you could change the signature to

  assertTrue(expr, *, msg=None)
which would prevent that issue.
or just:

    assert expr, "custom message"
though given the verbose api, it is ok to require the explicit msg kwarg (duplication in the tests is ok if it makes them more robust)
The second argument is the message that is displayed if the value is not truthy.
Second arg is a string message to display when test fails
Indeed, from TFA;

"assertTrue also accepts a second argument, which is the custom error message to show if the first argument is not truthy. This call signature allows the mistake to be made and the test to pass and therefore possibly fail silently."