【问题标题】:Incorrect status of failing steps Allure Behave Python失败步骤的不正确状态 Allure Behave Python
【发布时间】:2022-09-28 19:34:52
【问题描述】:

版本信息 蟒蛇:3.9 表现:1.2.6 魅力:2.13.5

我有 1 个功能和 1 个场景,3 个步骤会故意失败(它们都有 ZeroDivError)。

特征文件内容如下所示(test.feature)

Feature: Zero division 
  Scenario: people 
   Given Alex divides a number by zero
   Given Tom divides a number by zero 
   Given Mark divides a number by zero

Python中的步骤实现,

from behave import *

@given(u\'Alex divides a number by zero\')
def step_impl(context):
    try:
        a= 5
        quotient= a/0
        print(quotient)
    except:
        raise Exception(\"ZeroDivisionError has happened ALEX\")

@given(u\'Tom divides a number by zero\')
def step_impl(context):
    try:
        t= 10
        quotient= t/0
        print(quotient)
    except:
        raise Exception(\"ZeroDivisionError has happened TOM\")


@given(u\'Mark divides a number by zero\')
def step_impl(context):
    try:
        m= 15
        quotient= m/0
        print(quotient)
    except:
        raise Exception(\"ZeroDivisionError has happened MARK\")

在每一步中,我所做的就是将一个数字除以 0 并为其引发异常。

运行以下命令后在终端中看到的输出

行为特征\\test.feature

输出:TLDR -> 这里没有问题,所有步骤都失败并引发 3 个 ZeroDivisionerrors

> Feature: Zero division # features/test.feature:1
> 
>   Scenario: people                        # features/test.feature:2     
> 
>     Given Alex divides a number by zero # features/steps/test.py:4       
>       Traceback (most recent call last):
>         File \"features\\steps\\test.py\", line 8, in step_impl
>           quotient= a/0
>       ZeroDivisionError: division by zero
> 
>       During handling of the above exception, another exception occurred:
> 
>       Traceback (most recent call last):
>         File \"PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python39\\site-packages\\behave\\model.py\",
> line 1329, in run
>           match.run(runner.context)
>         File \"\\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python39\\site-packages\\behave\\matchers.py\",
> line 98, in run
>           self.func(context, *args, **kwargs)
>         File \"features\\steps\\test.py\", line 11, in step_impl
>           raise Exception(\"ZeroDivisionError has happened ALEX\")
>       Exception: ZeroDivisionError has happened ALEX
> 
>     Given Tom divides a number by zero  # features/steps/test.py:13
>       Traceback (most recent call last):
>         File \"features\\steps\\test.py\", line 17, in step_impl
>           quotient= t/0
>       ZeroDivisionError: division by zero
> 
>       During handling of the above exception, another exception occurred:
> 
>       Traceback (most recent call last):
>         File \"\\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python39\\site-packages\\behave\\model.py\",
> line 1329, in run
>           match.run(runner.context)
>         File \"\\Packages\\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python39\\site-packages\\behave\\matchers.py\",
> line 98, in run
>           self.func(context, *args, **kwargs)
>         File \"features\\steps\\test.py\", line 20, in step_impl
>           raise Exception(\"ZeroDivisionError has happened TOM\")
>       Exception: ZeroDivisionError has happened TOM
> 
>     Given Mark divides a number by zero # features/steps/test.py:23
>       Traceback (most recent call last):
>         File \"features\\steps\\test.py\", line 27, in step_impl
>           quotient= m/0
>       ZeroDivisionError: division by zero
> 
>       During handling of the above exception, another exception occurred:
> 
>       Traceback (most recent call last):
>         File \"\\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python39\\site-packages\\behave\\model.py\",
> line 1329, in run
>           match.run(runner.context)
>         File \"\\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python39\\site-packages\\behave\\matchers.py\",
> line 98, in run
>           self.func(context, *args, **kwargs)
>         File \"features\\steps\\test.py\", line 30, in step_impl
>           raise Exception(\"ZeroDivisionError has happened MARK\")
>       Exception: ZeroDivisionError has happened MARK
> 
> 
> 
> Failing scenarios:   features/test.feature:2  People
> 
> 0 features passed, 1 failed, 0 skipped 0 scenarios passed, 1 failed, 0
> skipped 0 steps passed, 3 failed, 0 skipped, 0 undefined Took 0m0.009s

自然我得到了想要的输出,所有 3 个步骤都失败了。这里没有问题

现在我运行这个命令来创建魅力报告

表现 -f allure_behave.formatter:AllureFormatter -o ALLURE/features/test.feature

接下来我运行 allure serve 命令,在浏览器中打开 allure 报告。 这就是我的问题开始的地方。

我正在链接魅力报告-> https://cosmic-narwhal-c18273.netlify.app/#

这就是为什么在类别下只引发了一个异常,即 ZeroDivisionError 发生在 ALEX 上

为什么我在测试缺陷下看不到其他失败的步骤。

它只显示场景的第一个例外

如何配置 allure 以显示每个失败的步骤及其异常。我错过了什么吗?

请协助

  • 不熟悉这些库或工具,只是在这里说您不应该使用裸露的except
  • @ddejohn 明白了,我就在这里

标签: python allure python-behave


【解决方案1】:

您的问题中有一些我不明白的地方,那就是您在“行为特征”命令的输出中有一个奇怪的行为。

我已使用 Python 3.9.5 和 Behave 1.2.6 将您的代码复制到一个新的空项目中,当我启动上述命令时,我得到以下输出:

$ behave features/test.feature
Feature: Zero division # features/test.feature:1

  Scenario: people                      # features/test.feature:2
    Given Alex divides a number by zero # steps/steps.py:4
      Traceback (most recent call last):
        File "steps\steps.py", line 8, in step_impl
          quotient = a / 0
      ZeroDivisionError: division by zero

      During handling of the above exception, another exception occurred:

      Traceback (most recent call last):
        File "c:\users\cxb0456\onedrive - telefonica\zz_otros\temporal\pruebaallure\venv\lib\site-packages\behave\model.py", line 1329, in run
          match.run(runner.context)
        File "c:\users\cxb0456\onedrive - telefonica\zz_otros\temporal\pruebaallure\venv\lib\site-packages\behave\matchers.py", line 98, in run
          self.func(context, *args, **kwargs)
        File "steps\steps.py", line 11, in step_impl
          raise Exception("ZeroDivisionError has happened ALEX")
      Exception: ZeroDivisionError has happened ALEX

    Given Tom divides a number by zero  # None
    Given Mark divides a number by zero # None


Failing scenarios:
  features/test.feature:2  people

0 features passed, 1 failed, 0 skipped
0 scenarios passed, 1 failed, 0 skipped
0 steps passed, 1 failed, 2 skipped, 0 undefined
Took 0m0.017s

此输出是我个人习惯的行为,因为如果您在其中一个步骤中出现错误,则不会执行后续步骤并显示为“已跳过”,如果有则继续测试是没有意义的是一个严重的错误(因为它会被零除)。

如果您已修改 Behave 的默认行为以强制其执行失败步骤之后的步骤,则 Behave 与 Allure 的集成可能不如预期。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-16
    • 1970-01-01
    • 1970-01-01
    • 2020-05-07
    • 1970-01-01
    相关资源
    最近更新 更多