allure-framework / allure-python

Allure integrations for Python test frameworks
https://allurereport.org/
Apache License 2.0
719 stars 235 forks source link

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

Open sebastianpfischer opened 11 months ago

sebastianpfischer commented 11 months ago

[//]: # ( . 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 ...

What is the current behavior?

printed error: HOOK-ERROR in after_all: KeyError: None

Actual error raised:


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 <module>
    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:


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:

Other information

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

delatrie commented 11 months ago

Hi, @sebastianpfischer! Thanks for the issue. Session scoped fixtures aren't implemented in allure-behave. Once they are, attachments from before_all/after_all will go there. Can't tell you the date estimate for this though, sorry.