Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

print success messages for asserts in python

I am using assert in python. Every time an assert fails I get the failure message which I would have put there to be printed. I was wondering if there is a way to print a custom success message when the assert condition passes?

I am using py.test framework.

for instance:

assert self.clnt.stop_io()==1, "IO stop failed"

for the above assert I get message "IO stop failed" if assert fails but I am looking to have "IO stop succeeded" if assert passes. something like this:

 assert self.clnt.stop_io()==1, "IO stop failed", "IO stop succeeded"
like image 425
cool77 Avatar asked Apr 25 '16 07:04

cool77


People also ask

How do you print messages from assert?

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 assert statements in Python?

What Are Assertions? In Python, assertions are statements that you can use to set sanity checks during the development process. Assertions allow you to test the correctness of your code by checking if some specific conditions remain true, which can come in handy while you're debugging code.


4 Answers

Yes, the simplest is surely to place a print below the assert:

assert self.clnt.stop_io()==1, "IO stop failed"
print("IO stop passed at location ==1")
like image 171
Reblochon Masque Avatar answered Oct 12 '22 20:10

Reblochon Masque


Write a simple helper function:

def myfunc(msg='assert OK'):
    print msg
    return True

Include that in the condition on the right-side, after an and:

assert self.clnt.stop_io()==1 and myfunc("IO stop succeeded"), "IO stop failed"
like image 26
cdarke Avatar answered Oct 12 '22 20:10

cdarke


This has been added to the pytest module v5 as the hook pytest_assertion_pass

in your conftest.py

def pytest_assertion_pass(item, lineno, orig, expl):
    log.info("asserting that {}, {}, {}, {}".format(item, lineno, orig, expl))

and in pytest.ini

enable_assertion_pass_hook = true
like image 36
JeffCharter Avatar answered Oct 12 '22 21:10

JeffCharter


If you are really feeling adventurous, you can just write your own extensible assert wrapper - with which you can count the number of asserts failed or add functionality like testing in quiet or verbose modes eg:

def assert(condition, fail_str, suc_str):
     if condition:
          print fail_str
     else:
          num_tests_passed = num_tests_passed + 1
          if verbose_enabled:
              print suc_str
like image 36
WilliamComputerScience Avatar answered Oct 12 '22 21:10

WilliamComputerScience