Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get assert to print a success message?

Here is an assertion that I have written

assert.equal(0,0,"Test Passed);

I was hoping that it would print the message Test passed but it is not happening. However if the assertion fails the message is displayed along with the error.

So is there any way to print the message if the test is successful?

like image 789
Akshat Jiwan Sharma Avatar asked Jun 17 '13 19:06

Akshat Jiwan Sharma


People also ask

How do you print assert statements?

try { Assert. assertEquals(actualString, expectedString); } catch (AssertionError e) { System. out. println("Not equal"); throw e; } System.

How do you print assert in Python?

Example: >>> assert (1==2, 1==1) <stdin>:1: SyntaxWarning: assertion is always true, perhaps remove parentheses? Example: >>> assert (1==2), ("This condition returns a %s value.") % "False" Traceback (most recent call last): File "<stdin>", line 1, in <module> AssertionError: This condition returns a False value.

How do you assert text in Python?

The assert keyword is used when debugging code. The assert keyword lets you test if a condition in your code returns True, if not, the program will raise an AssertionError. You can write a message to be written if the code returns False, check the example below.

What are assertion messages?

From a test readability perspective, assertion messages are code comments. Instead of relying on them, refactor tests to be self-documenting. In terms of ease of diagnostics, a better alternative to assertion messages is: Making tests verify a single unit of behavior.


1 Answers

According to the source, the message is only printed when the assertion fails.

assert.equal = function equal(actual, expected, message) {
  if (actual != expected) fail(actual, expected, message, '==', assert.equal);
};

Just for completeness here is the definition of fail.

function fail(actual, expected, message, operator, stackStartFunction) {
  throw new assert.AssertionError({
    message: message,
    actual: actual,
    expected: expected,
    operator: operator,
    stackStartFunction: stackStartFunction
  });
}
like image 101
James Avatar answered Sep 19 '22 14:09

James