iOS unit test automation-unit test data statistics

iOS unit test automation-unit test data statistics

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

Apple has updated the unit test result files ( .xcresult file) and related commands ( xccov viewetc.) in the Xcode11 version . Please refer to developer.apple.com/documentati ...

Unit test commands

Execute the following command to perform unit testing. The files generated during the unit test are stored in the BUILD_DIRdirectory.

# resultBundleVersion  

# 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 3
 

SIMULATOR_PLATFORMSpecify the type of simulator used, eg. The types of simulators available on different machines are different, such as platform=iOS Simulator,OS=13.4,name=iPhone 11. The types of simulators available on different machines are different. You can get the list of available simulators through the following command

xcrun simctl list
 

XCRESULT_PATHSpecify the storage path of the unit test result file ( .xcresult file). This parameter is newly added to the Xcode11 command line tool to facilitate the direct access to the result file. When using Xcode10 and earlier versions, we must go to the directory corresponding to BUILD_DIR to find this file. (Actually in Xcode11, the .xcresult file is in BUILD_DIR will also have a copy in the directory corresponding to , but of course it is convenient to directly specify the path of the result file.)

Unit test result data

After the unit test is executed, you can XCRESULT_PATHfind the generated result file, which is the .xccresult file. The directory format after the file is opened is:

.
  Data/
      data0~xxx
      data0~xxx
  
  Info.plist

 

By parsing these files, you can get basic unit test data such as unit test overview and code coverage.

The .xcresult file can also be opened directly in Xcode. After double-clicking to open it, you can see the details of this unit test in Xcode.

Unit test overview statistics

Unit test overview statistics require the use of official xcrun xcresulttooltools.

1. you need to get the data in json format, and parse the json data to get the id required to get the unit test report :

#  json id
xcrun xcresulttool get --format json --path path/to/xcresult_file 
 

Obtaining id from json data requires a series of complex json field parsing. Here I refer to fastlane's xcresult parsing script , instead of rewriting the id extraction logic by myself.

After obtaining the id, you can get further detailed unit test report data.

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

After executing the above command, you can get a copy of json data, continue to parse this json data, you can get the required unit test total number of use cases, number of failed use cases, number of alarms, execution time and other data. Here also refer to the xcresult parsing script of fastlane mentioned above .

In order to simplify the above logic, the UnitTestParser project provides unitTestInfo.rb to directly extract the required data.

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

For example, execute:

  ruby ../UnitTestParser/unitTestInfo.rb --xcresult-path=result.xcresult --output-file=result.txt
 15
 0
 0.48s
 

At the same time, these data will also be written into the result.txt file for easy reading by other tools.

  cat result.txt
15
0
0.48