I'm executing a simple testing script with pytest. I'm testing if the creation of a database connection succeeded. The function that creates the database connection requires a username and a password. During the testing, pytest prints out the username and password. Obviously, I don't want passwords to be printed out to the console! How can I prevent this from happening?
I've tried multiple solutions, including using contextlib and custom created NullWriters that suppress stderr and/or stdout (for example here https://stackoverflow.com/a/1810086). However, none of it seems to have any effect.
This is the test function:
def test_make_db2_connection():
# get username and password from the environmental variables
username = os.environ['username']
password = 'my_password' # would be: os.environ['pwd']
# create a connection with the database
connection = make_db2_connection(username, password)
# assert if the connection exists
assert connection, "Connection wasn't created."
See pytest's output below. As you can see, my username and (testing) password are printed. This is what I want to prevent.
============================================================================================================ FAILURES ============================================================================================================
____________________________________________________________________________________________________ test_make_db2_connection ____________________________________________________________________________________________________
def test_make_db2_connection():
# get username and password from the environmental variables
username = os.environ['username']
password = 'my_password' # would be: os.environ['pwd']
# create a connection with the database
> connection = make_db2_connection(username, password)
test_file.py:16:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
username = 'E001703', password = 'my_password'
def make_db2_connection(username, password):
"""Creates a connection with the database"""
dbalias = os.environ['dbalias']
> connection = connect(dbalias, username, password)
SQLCODE=-30082on: [IBM][CLI Driver] SQL30082N Security processing failed with reason "24" ("USERNAME AND/OR PASSWORD INVALID"). SQLSTATE=08001
=================================================================================================== 1 failed in 14.19 seconds ====================================================================================================
Solved by @hoefling! See the comments below the original question.
Solution was to use __tracebackhide__ = True. Downside of this solution is that you need to modify the original function that is being tested, instead of the testing script itself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With