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
1条答案
按热度按时间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,有更多细节。总而言之,
Xray团队没有pytest-bdd的教程(虽然将来可能会添加),但我能够得到一个快速运行的示例。
这是我的特性文件features/addition.feature,它包含分别基于相应Story和Test问题的标记特性和场景。
要运行测试,
pytest --cucumber-json cucumber.json
然后,要将这些结果导入到Xray on Jira服务器/数据中心示例,