allure-framework / allure-python

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

Fixes #771 allure-behave formatter crash with behave v1.2.7.dev5 #798

Closed ercaronte closed 4 months ago

ercaronte commented 4 months ago

This is a fix for issue https://github.com/allure-framework/allure-python/issues/771: with behave v1.2.7.dev5 an error causes the allure_behave report formatter to crash.

Context

With behave v1.2.7.dev5 the allure-behave python library crashes every run, with exceptions like the following example:

File "/Users/marcocarosi/Code/miniconda3/envs/phoenix3/lib/python3.12/site-packages/allure_behave/listener.py", line 97, in stop_test
self.stop_scenario(context['scenario'])
File "/Users/marcocarosi/Code/miniconda3/envs/phoenix3/lib/python3.12/site-packages/allure_behave/listener.py", line 100, in stop_scenario
should_run = (scenario.should_run_with_tags(self.behave_config.tags) and  # original
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/marcocarosi/Code/miniconda3/envs/phoenix3/lib/python3.12/site-packages/behave/model_core.py", line 398, in should_run_with_tags
return tag_expression.check(self.effective_tags)
       ^^^^^^^^^^^^^^^^^^^^
AttributeError: 'str' object has no attribute 'check'

That is a blocker for whoever wants to use allure with behave.

Checklist

ercaronte commented 4 months ago

Seeing the failing CI tests. There is an API change in the behave Configuration class made on 1.2.7dev1 release. Will see if I can made a fix so that the allure-behave listener supports <=1.2.6 versions as well as the new 1.2.7dev

ercaronte commented 4 months ago

Added a check on the config class attributes. Maybe not very elegant, but now all tests pass with all non-dev versions of behave.

Not sure who to ask for a review. @skhomuti or @delatrie, maybe you could help?

delatrie commented 4 months ago

Hi, @ercaronte ! Thank you for you PR.

We usually only support generally available officially released versions. However, in the case of Behave, making an exception is reasonable since 1.2.7 pre-release versions are somewhat popular, and an alternative is to use six-year-old version 1.2.6.

That said, I suggest to change the version check to be more expressive by utilizing packaging.version:

import behave
from packaging import version

# ...

if version.parse(behave.__version__) > version.parse("1.2.6"):
    # new code
else:
    # old code

The check might be slightly optimized by evaluating at the module level:

# ...

BEHAVE_1_2_7_OR_GREATER = version.parse(behave.__version__) > version.parse("1.2.6")

# ...

if BEHAVE_1_2_7_OR_GREATER:
    # ...
ercaronte commented 4 months ago

Hi @delatrie , thank you for your good feedback. I like the proposed changes, they are definitely more expressive in view of future code maintenance changes. I preferred the more optimised version at module level B-)

I tested locally on 1.2.7dev5, and for previous versions the CI tests are passing just fine.

ercaronte commented 4 months ago

Made the suggested changes, and run the 55 tests of tests/allure_behave with Behave 1.2.6 and with 1.2.7dev5. They are passing just fine with both versions.

Of course, without this change with Behave 1.2.7dev5 all tests could not even start because of the matchers API change from 1.2.7dev4.

Good suggestion!

delatrie commented 4 months ago

Nice job! Thank you for your time!