JUnit-XML based XCUITest report
Using the terminal, you can create JUnit-XML based reports from XCUITest and upload them to Test Management.
Executing a Test Case with XCUITest
You can initiate an XCUITest project with this git repository. Use this sample code to execute a Test Case.
class SampleXCUITests: XCTestCase {
override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
// In UI tests it is usually best to stop immediately when a failure occurs.
continueAfterFailure = false
// UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method.
XCUIApplication().launch()
// In UI tests it's important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this.
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}
func testAlert() {
let app = XCUIApplication()
//bring up the alert
app.buttons["Alert"].tap()
//verify that the alert has come
XCTAssertEqual(app.alerts.element.label, "Alert")
//dismiss the alert
app.alerts.buttons["OK"].tap()
//verify that the alert is dismissed
XCTAssertEqual(app.alerts.count, 0)
}
func testText() {
let app = XCUIApplication()
//visit the text page
app.buttons["Text"].tap()
let enterText = "Hi BrowserStack!!"
//verify that the text field has come up
XCTAssert(app.textFields["Enter a text"].exists)
//write the text
app.textFields["Enter a text"].tap()
app.textFields["Enter a text"].typeText(enterText)
app.typeText("\r")
//verify that the text entered matches the text view
XCTAssertEqual(app.staticTexts.element.label, enterText)
}
}
Create JUnit-XML test report with XCUITest
Install xcpretty
that enables XCUITest to generate a Junit-XML based report.
xpretty - gem install xcpretty
Launch the simulator in Xcode and run the following code to execute the test run and generate the Junit-XML report.
"xcodebuild \xcodebuild \
test \
-project ""Sample iOS.xcodeproj"" \
-scheme ""Sample iOS"" \
-destination 'platform=iOS Simulator,name=iPhone 14 Pro' | xcpretty -r junit"
Sample JUnit-XML report from XCUITest.
<?xml version='1.0' encoding='UTF-8'?>
<testsuites name='SampleXCUITests.xctest' tests='2' failures='0'>
<testsuite name='SampleXCUITests.SampleXCUITests' tests='2' failures='0'>
<testcase classname='SampleXCUITests.SampleXCUITests' name='testAlert' time='3.392'/>
<testcase classname='SampleXCUITests.SampleXCUITests' name='testText' time='5.645'/>
</testsuite>
</testsuites>
Steps to upload the JUnit-XML test report
Open the project directory in the Terminal and load the variables.
export TEST_MANAGEMENT_API_TOKEN="*************28a42"
export TEST_MANAGEMENT_PROJECT_NAME="<Project Name>"
export JUNIT_XML_FILE_PATH="<Report Path>"
export TEST_RUN_NAME="<Test Run Name"
TEST_MANAGEMENT_API_TOKEN
from the Active API Key section in the Settings of BrowserStack Test Management.
Upload the JUnit-XML test report using curl
command.
curl -k -X POST https://test-management.browserstack.com/api/v1/import/results/xml/junit \
-u $TEST_MANAGEMENT_API_TOKEN \
-F project_name="$TEST_MANAGEMENT_PROJECT_NAME" \
-F "file_path=@$JUNIT_XML_FILE_PATH" \
-F test_run_name="$TEST_RUN_NAME"
cURL
command until you get the response back.
You can also upload JUnit-XML test report with XCUITest using CI/CD.
Access the test run report from the console generated URL
After your report is successfully uploaded, you see a message similar to the following in your terminal. This message also generates a URL with "url"
parameter that you can use to access your automated test run.
{"message":"File uploaded successfully.","url":"https://test-management.browserstack.com/projects/<project id>/test-runs/<test run id>","success":true}
View test run report in Test Management
- Log in to the BrowserStack Test Management dashboard.
- Select the relevant project that has the test report uploaded.
- Click Test Runs.
- Open the Test Run generated from automation test exectution.
- You will find all the test cases with their respective results here.
We're sorry to hear that. Please share your feedback so we can do better
Contact our Support team for immediate help while we work on improving our docs.
We're continuously improving our docs. We'd love to know what you liked
We're sorry to hear that. Please share your feedback so we can do better
Contact our Support team for immediate help while we work on improving our docs.
We're continuously improving our docs. We'd love to know what you liked
Thank you for your valuable feedback!