Skip to content

Comment on 3 percent of Python codebases we checked had silently failing unit tests

Comments

I'm no Python expert, but why does assertTrue() even accept two arguments?

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."

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.