pytest在调试模式下运行时会通过,但否则会失败

发布于 2025-01-21 20:09:41 字数 1210 浏览 1 评论 0原文

以下是我执行的pytest代码来测试添加连接命令。

 def test_cli_connection_add(self, cmd, expected_output, expected_conn):
        runner = CliRunner()
        result = runner.invoke(connections, cmd)
        assert result.output.strip() == expected_output

它的问题可能会说出预期的输出应该具有新线。

>       assert result.output.strip() == expected_output
E       AssertionError: assert ('Successfully added `conn_id`=new4 : '\n 'hive_metastore://airflow:******@host:9083/airflow') == ('Successfully added `conn_id`=new4 :   \n'\n 'hive_metastore://airflow:******@host:9083/airflow')
E         + Successfully added `conn_id`=new4 : hive_metastore://airflow:******@host:9083/airflow
E         - Successfully added `conn_id`=new4 :
E         - hive_metastore://airflow:******@host:9083/airflow

tests/cli/commands/test_connection.py:558: AssertionError

为了进一步调试此问题,我在代码中添加了set_trace:

 def test_cli_connection_add(self, cmd, expected_output, expected_conn):
        pytest.set_trace()
        runner = CliRunner()
        result = runner.invoke(connections, cmd)
        assert result.output.strip() == expected_output

当我通过debugger执行代码时,主张通过。谁能帮我为什么这样做?

Following is the pytest code that I execute to test the add connection command.

 def test_cli_connection_add(self, cmd, expected_output, expected_conn):
        runner = CliRunner()
        result = runner.invoke(connections, cmd)
        assert result.output.strip() == expected_output

It fails with issue that tells the expected output should have newline.

>       assert result.output.strip() == expected_output
E       AssertionError: assert ('Successfully added `conn_id`=new4 : '\n 'hive_metastore://airflow:******@host:9083/airflow') == ('Successfully added `conn_id`=new4 :   \n'\n 'hive_metastore://airflow:******@host:9083/airflow')
E         + Successfully added `conn_id`=new4 : hive_metastore://airflow:******@host:9083/airflow
E         - Successfully added `conn_id`=new4 :
E         - hive_metastore://airflow:******@host:9083/airflow

tests/cli/commands/test_connection.py:558: AssertionError

To debug this issue further I added set_trace in the code:

 def test_cli_connection_add(self, cmd, expected_output, expected_conn):
        pytest.set_trace()
        runner = CliRunner()
        result = runner.invoke(connections, cmd)
        assert result.output.strip() == expected_output

When I execute the code via debugger, the assertion pass. Can anyone help me why this works this way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文