【问题标题】:How to see exactly what went wrong in Behave如何准确查看 Behave 中出了什么问题
【发布时间】:2014-02-26 15:16:23
【问题描述】:

我们最近开始将Behave (github link) 用于新python Web 服务的BDD。

问题

在测试失败时,我们有什么方法可以获取有关失败原因的详细信息?他们抛出AssertionError,但他们从不显示到底出了什么问题。例如进入断言的期望值和实际值。

我们一直在努力寻找这样的现有功能,但我想它不存在。当然,这个问题的一个很好的答案是提示和提示如何通过修改源代码来实现此行为,以及此功能是否存在于其他类似的 BDD 框架中,例如 jBehave、NBehave 或 Cucumber?

示例

今天,当测试失败时,输出显示:

  Scenario: Logout when not logged in                  # features\logout.feature:6
    Given I am not logged in                               # features\steps\logout.py:5
    When I log out                                     # features\steps\logout.py:12
    Then the response status should be 401             # features\steps\login.py:18
      Traceback (most recent call last):
        File "C:\pro\venv\lib\site-packages\behave\model.py", line 1037, in run
          match.run(runner.context)
        File "C:\pro\venv\lib\site-packages\behave\model.py", line 1430, in run
          self.func(context, *args, **kwargs)
        File "features\steps\login.py", line 20, in step_impl
          assert context.response.status == int(status)
      AssertionError

      Captured stdout:
      api.new_session
      api.delete_session

      Captured logging:
      INFO:urllib3.connectionpool:Starting new HTTP connection (1): localhost
      ...

我想要类似的东西:

  Scenario: Logout when not logged in                  # features\logout.feature:6
    Given I am not logged in                               # features\steps\logout.py:5
    When I log out                                     # features\steps\logout.py:12
    Then the response status should be 401             # features\steps\login.py:18

ASSERTION ERROR
Expected:   401
But got:    200

如您所见,我们通用步骤中的断言清楚地打印出来

`assert context.response.status == int(status)`

但我宁愿有这样的功能

assert(behave.equals, context.response.status, int(status)

或任何其他可以从失败的断言中生成动态消息的东西。

【问题讨论】:

    标签: cucumber bdd jbehave nbehave python-behave


    【解决方案1】:

    您可以使用另一个断言提供程序,例如PyHamcrest,而不是使用上面示例中的“原始断言”语句,它会为您提供所需的详细信息。 它会告诉你出了什么问题,比如:

    # -- file:features/steps/my_steps.py
    from hamcrest import assert_that, equal_to
    ...
        assert_that(context.response.status, equal_to(int(status)))
    

    另见:

    【讨论】:

      【解决方案2】:

      根据https://pythonhosted.org/behave/tutorial.html?highlight=debug,and,这个实现对我有用。

      使用 after_step() 挂钩可以轻松提供“错误/失败调试”功能。步骤失败时启动调试器。

      一般来说,仅在需要时启用此功能(在交互模式下)是个好主意。在此示例中,这是通过使用环境变量来完成的。

      # -- FILE: features/environment.py
      # USE: BEHAVE_DEBUG_ON_ERROR=yes     (to enable debug-on-error)
      from distutils.util import strtobool as _bool
      import os
      
      BEHAVE_DEBUG_ON_ERROR = _bool(os.environ.get("BEHAVE_DEBUG_ON_ERROR", "no"))
      
      def after_step(context, step):
          if BEHAVE_DEBUG_ON_ERROR and step.status == "failed":
              # -- ENTER DEBUGGER: Zoom in on failure location.
              # NOTE: Use IPython debugger, same for pdb (basic python debugger).
              import ipdb
              ipdb.post_mortem(step.exc_traceback)
      

      【讨论】:

      • 一个很好的建议,虽然它不能解决在错误消息中查看问题所在的问题。想象一下在远程虚拟机上运行测试作为持续集成部署的一部分。
      【解决方案3】:

      不要忘记,您始终可以在assert 语句中添加信息消息。例如:

      assert output is expected, f'{output} is not {expected}'
      

      【讨论】:

        【解决方案4】:

        我发现使用 pyhamcrest 断言比标准 Python 断言产生更好的错误报告。

        【讨论】:

          猜你喜欢
          • 2012-03-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-09-18
          • 2016-10-29
          • 2017-09-12
          相关资源
          最近更新 更多