用户友好与JUnit XML中生成的名称和类名,以便导入到XRay

ee7vknir  于 2022-11-11  发布在  其他
关注(0)|答案(1)|浏览(145)

Using pytest-bdd with the JUnit argument to produce a nice JUnit XML report.
I then use a cURL command to import the test to XRay, the Atlassian Jira extension.
Jira is running on a server running on our network (not cloud). So I am using something like:

curl --location -X POST "https://jira.nayax.com/rest/raven/1.0/import/execution/junit?projectKey=XXX&testPlanKey=XXX-255" -H "Authorization: Basic Z3VxxxxxxzJMaxxxxxxxxx" -H "Content-Type:multipart/form-data" -F "file=@\"reports/results.xml\""

What I get are tests with names exactly as expected given the XML file. Example fragment:

<testcase classname="tests.step_defs.test_visitor_page" name="test_create_new_user[110-201]" time="1.125"/>
        <testcase classname="tests.step_defs.test_visitor_page" name="test_create_a_package[110]" time="0.306"/>
        <testcase classname="tests.step_defs.test_visitor_page" name="test_search_user_by_first_name" time="0.700"/>
        <testcase classname="tests.step_defs.test_visitor_page" name="test_scan_nfc[110-10001-TYPE_2]" time="1.399"/>
        <testcase classname="tests.step_defs.test_visitor_page" name="test_get_settings" time="0.669"/>
        <testcase classname="tests.step_defs.test_visitor_page"
                  name="test_get_redemption_machine_for_this_location[110]" time="0.680"/>
        <testcase classname="tests.step_defs.test_visitor_page" name="test_add_free_credits[110]" time="0.766"/>
        <testcase classname="tests.step_defs.test_visitor_page" name="test_buy_credits[110]" time="0.708"/>
        <testcase classname="tests.step_defs.test_visitor_page" name="test_add_free_tickets[110-yes]" time="1.051"/>
        <testcase classname="tests.step_defs.test_visitor_page" name="test_add_free_tickets[110-no]" time="1.034"/>
        <testcase classname="tests.step_defs.test_visitor_page" name="test_purchase_group_package[110]" time="0.968"/>
        <testcase classname="tests.step_defs.test_visitor_page" name="test_get_user_history[110]" time="0.676"/>
        <testcase classname="tests.step_defs.test_visitor_page" name="test_delete_billing_record_from_history[110]"

So you get Jira XRay items named:

Is there some decorator or other device I can use to get the Summary fields below (names of the items, like test_buy_credits[110] ) to be something like:

  • Create New User
  • Create a Package [110] etc.?

Writing an XSD and using an XSLT transformation would probably work, but would be great if there's a simpler way.
Per request, here's are some tests:

@allure.step
@given('Location ID is "<locationId>"', target_fixture="location_id")
def location_id_is_locationid(locationId):
    """Location ID is "<locationId>"."""
    return locationId

@allure.step
@when('I create a package', target_fixture="package_response")
def i_create_a_package(location_id):
    """I create a package"""
    package_response = Package().create(location_id=location_id, session=s)
    return package_response

@then('package is created')
def package_is_created(package_response):
    """Package is created"""
    assert (package_response.status_code == 200)

In answer to the question about my test_ functions, I have to admit that I am pretty new to this, so I only have the given-when-then implementations.
Here is a piece of my feature file:

Feature: Visitor Page
  Scenario Outline: Create new user
    Given Location ID is "<locationId>"
    When I create a new user
    Then call is successful
    And return code is <returnCode>

    Examples: User parameters
      | locationId  | returnCode  |
      | 110         | 201         |

  Scenario Outline: Create a package
    Given Location ID is "<locationId>"
    When I create a package
    Then package is created

    Examples: Package parameters
    | locationId |
    | 110        |

  Scenario Outline: Purchase package
    Given a user exists at "<locationId>"
    And I have created a package
    When user purchases the package
    Then the user will have the package

    Examples:
      | locationId |
      | 110        |

  Scenario: Search user by first name
    Given I have a first name and nfc tag
    When I search for user
    Then I retrieve user details

  Scenario Outline: Scan NFC
    Given a user exists at "<locationId>"
    And box ID is "<boxId>"
    And tag type is "<tagType>"
    When I scan the NFC
    Then nfc tag is scanned

    Examples: NFC parameters
    | locationId | boxId | tagType |
    | 110        | 10001 | TYPE_2  |

  Scenario: Get Settings
    Given I am logged in as an employee
    When I request settings
    Then I get settings
epfja78i

epfja78i1#

pytest有一个JunitXML报告器,它使用testcase元素的测试方法的名称,这些元素将成为.xml报告的一部分; AFAIK(通过快速代码检查),不可能定制这些名称,除非您从头开始创建JUnit报告器。理论上,我们可以尝试从@scenario标记获取场景名称,但我不知道一种简单的方法。
pytest-bdd有一个 cucumber JSON报告器,尽管这可能更有用; Xray也支持这种格式。要生成Cucumber JSON格式的报告,假设您有pytest-bdd,您可以执行以下操作:
pytest --cucumber-json cucumber.json
请注意,与处理Junit XML报告和Cucumber JSON相关的流程是不同的。
而对于摄取JUnit XML报告,您只需要将该报告提交给Xray,Xray将自动提供测试问题,报告上的每个testcase元素一个,cucumber和其他BDD相关框架的流程是不同的。您可以在这里看到一个示例(但针对Jira云上的Behave和Xray),以了解更多信息。关于Java related tutorial, detailing the two flows,有更多细节。
总而言之,

  • 如果您只想获得关于测试是否通过的非常高级别的信息,那么使用JunitXML报告导入结果就足够了
  • 如果你想获得关于小 cucumber 声明、状态和持续时间的完整信息,你需要使用Cucumber JSON报告。在这种情况下,Scenarios必须作为不同的测试问题存在于Xray中。你需要从Jira中导出它们(这样它们就可以用相应的问题关键字进行标记),运行它们,然后使用各自的Cucumber JSON端点将它们导入到Xray中。

Xray团队没有pytest-bdd的教程(虽然将来可能会添加),但我能够得到一个快速运行的示例。
这是我的特性文件features/addition.feature,它包含分别基于相应Story和Test问题的标记特性和场景。

@REQ_CALC-1
Feature:  As a user, I can add two numbers

@TEST_CALC-2
Scenario: simple integer addition
        Given I have entered 1 into the calculator
        And I have entered 2 into the calculator
        When I press add
        Then the result should be 3 on the screen

@TEST_CALC-3
Scenario: negative integer addition
        Given I have entered -1 into the calculator
        And I have entered 2 into the calculator
        When I press add
        Then the result should be 1 on the screen

要运行测试,
pytest --cucumber-json cucumber.json
然后,要将这些结果导入到Xray on Jira服务器/数据中心示例,

curl -H "Content-Type: application/json" -X POST -u username:password --data @"cucumber.json" https://example.jira.local/rest/raven/2.0/import/execution/cucumber

相关问题