allure-framework / allure-framework/allure-python

Allure attachment fails in after_all() method of python-behave framework

未关闭
#764 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
task:new feature theme:behave
主要语言
Python
星标
814
派生
260
PR 合并指标
30 天内没有已合并 PR

描述

[//]: # (
. Note: for support questions, please use Stackoverflow or Gitter**.
. This repository's issues are reserved for feature requests and bug reports.
.
. In case of any problems with Allure Jenkins plugin** please use the following repository
. to create an issue: https://github.com/jenkinsci/allure-plugin/issues
.
. Make sure you have a clear name for your issue. The name should start with a capital
. letter and no dot is required in the end of the sentence. An example of good issue names:
.
. - The report is broken in IE11
. - Add an ability to disable default plugins
. - Support emoji in test descriptions
)

#### I'm submitting a ...
- [x] bug report
- [ ] feature request
- [ ] support request => Please do not submit support request here, see note at the top of this template.

#### What is the current behavior?
printed error: HOOK-ERROR in after_all: KeyError: None

Actual error raised:
```python

Exception has occurred: KeyError
None
File "/Users/userA/Library/Caches/pypoetry/virtualenvs/pytool-6R8xcXyw-py3.11/lib/python3.11/site-packages/allure_commons/reporter.py", line 33, in __getitem__
return self.thread_context.__getitem__(item)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/userA/Library/Caches/pypoetry/virtualenvs/pytool-6R8xcXyw-py3.11/lib/python3.11/site-packages/allure_commons/reporter.py", line 153, in _attach
self._items[last_uuid].attachments.append(attachment)
File "/Users/userA/Library/Caches/pypoetry/virtualenvs/pytool-6R8xcXyw-py3.11/lib/python3.11/site-packages/allure_commons/reporter.py", line 158, in attach_file
file_name = self._attach(uuid, name=name, attachment_type=attachment_type,
File "/Users/userA/Library/Caches/pypoetry/virtualenvs/pytool-6R8xcXyw-py3.11/lib/python3.11/site-packages/allure_behave/listener.py", line 174, in attach_file
self.logger.attach_file(uuid4(), source, name=name, attachment_type=attachment_type, extension=extension)
File "/Users/userA/Library/Caches/pypoetry/virtualenvs/pytool-6R8xcXyw-py3.11/lib/python3.11/site-packages/pluggy/_callers.py", line 77, in _multicall
res = hook_impl.function(*args)
File "/Users/userA/Library/Caches/pypoetry/virtualenvs/pytool-6R8xcXyw-py3.11/lib/python3.11/site-packages/pluggy/_manager.py", line 115, in _hookexec
return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
File "/Users/userA/Library/Caches/pypoetry/virtualenvs/pytool-6R8xcXyw-py3.11/lib/python3.11/site-packages/pluggy/_hooks.py", line 493, in __call__
return self._hookexec(self.name, self._hookimpls, kwargs, firstresult)
File "/Users/userA/Library/Caches/pypoetry/virtualenvs/pytool-6R8xcXyw-py3.11/lib/python3.11/site-packages/allure_commons/_allure.py", line 203, in file
plugin_manager.hook.attach_file(source=source, name=name, attachment_type=attachment_type, extension=extension)
File "/Users/userA/Documents/git/addr/test-framework/pytool/test_case_layer/environment.py", line 233, in after_all
allure.attach.file(log_destination.parent, log_destination.name, allure.attachment_type.TEXT)
File "/Users/userA/Library/Caches/pypoetry/virtualenvs/pytool-6R8xcXyw-py3.11/lib/python3.11/site-packages/behave/runner.py", line 545, in run_hook
self.hooks[name](context, *args)
File "/Users/userA/Library/Caches/pypoetry/virtualenvs/pytool-6R8xcXyw-py3.11/lib/python3.11/site-packages/behave/runner.py", line 645, in run_model
self.run_hook("after_all", self.context)
File "/Users/userA/Library/Caches/pypoetry/virtualenvs/pytool-6R8xcXyw-py3.11/lib/python3.11/site-packages/behave/runner.py", line 824, in run_with_paths
return self.run_model()
File "/Users/userA/Library/Caches/pypoetry/virtualenvs/pytool-6R8xcXyw-py3.11/lib/python3.11/site-packages/behave/runner.py", line 804, in run
return self.run_with_paths()
File "/Users/userA/Library/Caches/pypoetry/virtualenvs/pytool-6R8xcXyw-py3.11/lib/python3.11/site-packages/behave/__main__.py", line 127, in run_behave
failed = runner.run()
File "/Users/userA/Library/Caches/pypoetry/virtualenvs/pytool-6R8xcXyw-py3.11/lib/python3.11/site-packages/behave/__main__.py", line 183, in main
return run_behave(config)
File "/Users/userA/Documents/git/addr/test-framework/pytool/cli/pytool_run.py", line 48, in pytool_run_with_allure
main(command.split())
File "/Users/userA/Documents/git/addr/test-framework/pytool/cli/pytool_run.py", line 223, in
pytool_run_with_allure()
File "/opt/homebrew/Cellar/python@3.11/3.11.5/Frameworks/Python.framework/Versions/3.11/lib/python3.11/runpy.py", line 88, in _run_code
exec(code, run_globals)
File "/opt/homebrew/Cellar/python@3.11/3.11.5/Frameworks/Python.framework/Versions/3.11/lib/python3.11/runpy.py", line 198, in _run_module_as_main
return _run_code(code, main_globals, None,
KeyError: None

```
#### If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem

in `environment.py`:

``` python

def after_all(context):
global log_destination
allure.attach.file(log_destination, log_destination.name, allure.attachment_type.TEXT)

```

It seems like the problem comes from https://github.com/allure-framework/allure-python/blob/master/allure-python-commons/src/reporter.py#L72. All tests where executed and the list contains only elements of type `TestResultContainer` and not of type `ExecutableItem` which lead the method to return a `None`.

The `None` is propagated cross the framework which lead to a `KeyError`.

#### What is the expected behavior?
The log to be attached and no error printed.

#### What is the motivation / use case for changing the behavior?
We want to attach the logs to the allure reports after all features where called.

#### Please tell us about your environment:

- Allure version: 2.13.2
- Test framework: behave@1.2.6
- Allure adaptor: allure-behave@2.13.2

#### Other information

[//]: # (
. e.g. detailed explanation, stacktraces, related issues, suggestions
. how to fix, links for us to have more context, eg. Stackoverflow, Gitter etc
)

贡献指南

打开贡献指南

调研方向

使用 environment.py 中展示的 after_all() 示例重现该问题,然后检查 allure-python-commons/src/reporter.py 第 72 行附近以及 allure-behave/listener.py 第 174 行附近的代码。跟踪缺失的 item 在 Behave 的 after_all hook 期间是如何传递的。完成的标准是:日志能够附加到 Allure 报告中,且不会出现 KeyError。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
testing
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
描述清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。