Customize Allure Report using pytest-allure-adaptor

点点圈 提交于 2021-02-08 04:37:28

问题


I want to customize my allure test report using pytest adaptor. For e.g adding Environment details on the Overview Page. Changing the Report Name on the Overview screen.

I tried adding the environment details in conftest.py as suggested in the documentation, but it do not work for me

def pytest_configure(config):
    allure.environment(test_server='testserver', report='My Test Report')

I also tried adding environment.properties in the allure-report folder but that also didn't work. Please let me know if I am doing something wrong here and how can I resolve this problem.


回答1:


better late than never, as said in documentation pytest_configure is

called after command line options have been parsed and all plugins and initial conftest files been loaded.

each plugin has its own pytest_configure method, and what happens here is that your pytest_configure of you conftest.py is called before the pytest_configure of allure plugin.

An easy fix is to ask your pytest_configure to be executed as late as possible (preferable the last one) by adding @pytest.hookimpl(trylast=True) as header.

@pytest.hookimpl(trylast=True)
def pytest_configure(config):
    allure.environment(test_server='testserver', report='My Test Report')



回答2:


As I discovered in the code realization of allure setting environment variables is not implemented yet in the framework for Python. allure.py module.

from allure_commons._allure import label
from allure_commons._allure import severity
from allure_commons._allure import tag
from allure_commons._allure import epic, feature, story
from allure_commons._allure import link
from allure_commons._allure import issue, testcase
from allure_commons._allure import Dynamic as dynamic
from allure_commons._allure import step
from allure_commons._allure import attach
from allure_commons.types import Severity as severity_level
from allure_commons.types import AttachmentType as attachment_type


__all__ = [
    'label',
    'severity',
    'tag',
    'epic'
    'feature',
    'story',

    'link',
    'issue',
    'testcase',

    'step'

    'dynamic'

    'severity_level',

    'attach',
    'attachment_type'
]

The developers of Allure are aware of that and are working on it. You can see the respective bug.




回答3:


You could set allure test environment data like this:

def pytest_sessionfinish(session, exitstatus):

    session.config.allure.environment(test_server='testserver', report='My Test Report')

Old allure pytest plugin is deprecated and new allure pytest plugin is not feature compatible with old one.

If you want to add environment properties, you will have to create environment.properties file and place it in alluredir folder.

def pytest_sessionfinish(session, exitstatus):
    report_dir = session.config.option.allure_report_dir # Gets the --alluredir directory path
    env_details = """a:b
                 c:d
              """

    if report_dir:
        with open('{}/{}'.format(report_dir, 'environment.properties'), 'w') as allure_env:
            allure_env.write({}.format(env_details))

This code will create environment.properties file and put it in allure results dir. When you serve that directory with allure-cli, you will see environment details.




回答4:


I never tried pytest adapter. But you convert XML report to HTML with the same tool whatever adapter is. So environment.properties should work. You can test it with https://github.com/allure-examples/allure-testng-example:

  1. mvn test
  2. create target/allure-results/environment.properties "testserver=My Test Report"
  3. mvn site
  4. open target/site/allure-maven-plugin/index.html in FireFox

If it doesn't, please check Allure version right in HTML report.



来源:https://stackoverflow.com/questions/35264335/customize-allure-report-using-pytest-allure-adaptor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!