All scripts covered in this article are hosted in the GitHub project UnitTestParser. Demo project: UnitTestDemo

Apple in Xcode11 version of unit test results file (. Xcresult file) and related commands (xccov view, etc.) have more update, please refer to the developer.apple.com/documentati… .

Unit test command

Run the following command to test the unit. Files generated during unit testing are stored in the BUILD_DIR directory.

# resultBundleVersion is optional. In order to prevent the result file from changing due to the subsequent version update, # for xcodeproj xcodebuild test project Demo. Xcodeproj scheme Demo -derivedDataPath "${BUILD_DIR}/" -destination "${SIMULATOR_PLATFORM}" -resultBundlePath "${XCRESULT_PATH}" -resultBundleVersion 3 # for xcworkspace xcodebuild test -workspace Demo.xcworkspace -scheme Demo -derivedDataPath "${BUILD_DIR}/" -destination "${SIMULATOR_PLATFORM}" -resultBundlePath "${XCRESULT_PATH}" -resultBundleVersion 3Copy the code

SIMULATOR_PLATFORM specifies the type of emulator to use, for example. For example, run platform=iOS Simulator,OS=13.4,name=iPhone 11. You can run the following command to obtain a list of available simulators

xcrun simctl list
Copy the code

XCRESULT_PATH Specifies the location of the unit test result file (.xcresult file). This parameter is newly added to the Xcode11 command line tool for direct access to the result file. With Xcode10 and earlier, we have to go to the BUILD_DIR directory to find this file. (In Xcode11, there is also a copy of the.xcresult file in the BUILD_DIR directory, but it is easier to specify the path to the resulting file.)

Unit test result data

After the unit test is executed, the resulting file, the.xccresult file, can be found in XCRESULT_PATH. After the file is opened, the directory format is:

. ├ ─ ─ Data / │ ├ ─ ─ data0 ~ XXX │ └ ─ ─ data0 ~ XXX │ └ ─ ─ Info. The plistCopy the code

Parsing these files allows you to obtain basic unit test data such as unit test profiles, code coverage, and so on.

The.xcresult file can also be opened directly in Xcode. Double-click to open it and you can see details of the unit test in Xcode.

Unit test overview statistics

Unit test overview statistics require the official xcRun XCREsulttool tool.

First, we need to get the data in JSON format and parse out the ID needed to get the unit test report from the JSON data:

#Parse it into JSON data for the next step of obtaining id
xcrun xcresulttool get --format json --path path/to/xcresult_file 
Copy the code

Getting an ID from json data requires a series of complex JSON field parsing, so I refer to Fastlane’s Xcresult parsing script instead of rewriting the ID extraction logic myself.

Once you get the ID, you can go further and get detailed unit test report data.

xcrun xcresulttool get --format json --path path/to/xcresult_file --id $id
Copy the code

After executing the preceding command, you can obtain another JSON data. If you continue to parse this JSON data, you can obtain the required data, such as total number of unit test cases, number of failed cases, number of alarms, and execution duration. Again, refer to fastlane’s Xcresult parsing script mentioned above.

To simplify this logic, the UnitTestParser project provides unitTestInfo.rb to extract the desired data directly.

ruby unitTestInfo.rb --xcresult-path=path/to/xcresult_file --output-file=/path/to/output_file
Copy the code

For example:

╰ ─ + ruby.. / UnitTestParser unitTestInfo. Rb -- -- xcresult - path = result. Xcresult - output - the file = result. TXT unit test cases: 15 failure unit testing use cases: 0 Total unit test running time: 0.48sCopy the code

At the same time, the data will be written to the result. TXT file for other tools to read.

╰─± cat result.txt 15 0 0.5Copy the code