python - 如何防止pytest在测试期间打印局部变量

标签 python pytest

我正在使用 pytest 执行一个简单的测试脚本。我正在测试创建数据库连接是否成功。
创建数据库连接的函数需要用户名和密码。在测试过程中,pytest 会打印出用户名和密码。显然,我不希望将密码打印到控制台!我怎样才能防止这种情况发生?

我尝试了多种解决方案,包括使用 contextlib 和自定义创建的 NullWriters 来抑制 stderr 和/或 stdout(例如这里 https://stackoverflow.com/a/1810086 )。然而,似乎没有任何效果。

这是测试功能:

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

请参阅下面的 pytest 输出。如您所见,打印了我的用户名和(测试)密码。这是我想要防止的。
============================================================================================================ 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 ====================================================================================================

最佳答案

由@hoefling 解决!请参阅原始问题下方的评论。

解决方案是使用 __tracebackhide__ = True .此解决方案的缺点是您需要修改正在测试的原始函数,而不是测试脚本本身。

关于python - 如何防止pytest在测试期间打印局部变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56324275/

相关文章:

python - 我可以在 Python 的 DB API 中使用 .description() 获取表名和列名吗?

python - Django 嵌套序列化器未序列化内部模型

python - 在pytest中使用键盘中断钩子(Hook)

python - 在 Python 3 中,使用 Pytest,我们如何测试退出代码 : exit(1) and exit(0) for a python program?

command-line - 如何计算用pytest编写的测试用例?

python - 尝试使用 `OrderedDict` 数据结构编写复杂算法

python - "resolve_variable"在 Django 中做什么? ("template.Variable")

python - 如果我计划最终使用 Jython,我应该将我的 Python 代码保留在 2.x 还是迁移到 3.x?

python - 如何覆盖 Python 模块的默认日志记录级别?

python - 仅在使用 --collect-only 时运行方法